How to retrieve user without hardcoding ID in discord client.users.get function

自闭症网瘾萝莉.ら 提交于 2021-01-29 05:09:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!