1 回答

TA貢獻1799條經(jīng)驗 獲得超6個贊
歡迎來到SO!我就是這樣做的。UserManager#Cache
是一個集合,因此您可以用來Collection#has
檢查您要查找的內(nèi)容是否存在。演示:
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)
當然,這將在緩存中進行搜索,因此如果用戶在您的機器人啟動時從未執(zhí)行過任何操作,則它將無法工作。要獲取并測試用戶是否存在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
添加回答
舉報