问题
I'd like my discord bot to send a user (captain) a message, when another user on the server wants to join their team. I have the DiscordID of the captain stored in my SQLite database and can retrieve it. The problem is that when I pass this variable as an argument to the client.users.get() method, it returns undefined. I've tried converting the DiscordID to a string, but it still returns undefined. When I hardcode the DiscordID it works fine.
I'm not very experienced with discord.js so I'm hoping this is just a trivial issue which can be quickly resolved. Or is this intentionally not possible to avoid bots messaging users without directly receiving a messages from them?
const client = new Discord.Client();
client.users.get('12345'); <-- This returns the user object
const capt = 12345;
client.users.get(capt); <-- returns undefined
const capt = 12345;
const captString = capt.toString();
client.users.get(captString); <-- returns undefined
回答1:
Discord IDs need to be stored as a string, because the number is too big for Node.js and would get rounded. By checking Number.MAX_SAFE_INTEGER
, you'll see that it's less than 18 digits and therefore won't properly store IDs.
Bad: 189855563893571595
Good: '189855563893571595'
来源:https://stackoverflow.com/questions/56201459/how-to-retrieve-user-without-hardcoding-id-in-discord-client-users-get-function