首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检测不一致客户端上是否存在用户(Discord.js)

检测不一致客户端上是否存在用户(Discord.js)
EN

Stack Overflow用户
提问于 2020-12-10 14:50:04
回答 1查看 556关注 0票数 0

我一直在尝试检测Discord客户端上是否存在用户...我使用了这个:

代码语言:javascript
运行
复制
var user_id;

//on message and after assigning user_id
if(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)
}

你能帮帮我吗?我很感谢你的帮助。

谢谢你,Rako游戏

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-10 14:57:10

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

代码语言:javascript
运行
复制
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的用户是否存在

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65229721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档