为了账号安全,请及时绑定邮箱和手机立即绑定

检测用户是否存在于 Discord 客户端 (Discord.js)

检测用户是否存在于 Discord 客户端 (Discord.js)

狐的传说 2023-11-11 16:14:04
我一直在尝试检测 Discord 客户端本身是否存在用户......我使用了这个:var user_id;//on message and after assigning user_idif(client.users.cache.get(user_id) !== undefined) {//checks if user does exist on the client and does action (it always thinks it's undefined for some reason)}你能帮助我吗?我感谢你所有的帮助...
查看完整描述

1 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

欢迎来到SO!我就是这样做的。UserManager#Cache是一个集合,因此您可以用来Collection#has检查您要查找的内容是否存在。演示:

const userId = "000000000000000000";

const exists = client.users.cache.has(userId);

console.log(exists); // true or false


// Advice to stop making cringe code --no worries, we've all been there before ;)

if (exists) doSomething(); // And not: if (exists === true), although it still can be used in other places

if (!exists) do SomethingElse(); // And definitely not: if (exists === false)

当然,这将在缓存中进行搜索,因此如果用户在您的机器人启动时从未执行过任何操作,则它将无法工作。要获取并测试用户是否存在UserManager#fetch

const exists = await client.users.fetch(userId); // Warning: await here. To be used in async functions, or to be replaced by .then()

// Here, the user is cached

console.log(exists); // undefined or User; can be check as in the snippet above. You can still wrap 'exists' with Boolean() if you feel more comfortable having boolean values



查看完整回答
反对 回复 2023-11-11
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信