亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢測用戶是否存在于 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 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號