Я пытался определить, существует ли пользователь на самом клиенте Discord... Я использовал это:
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)
}
Вы можете помочь мне? Я ценю всю вашу помощь...
Спасибо, Игры Рако
Добро пожаловать в СО! Вот как бы я это сделал. UserManager#Cache — это коллекция, поэтому вы можете использовать Collection#has, чтобы проверить, существует ли то, что вы ищете. Демо:
const userId = "000000000000000000";
const exists = client.users.cache.has(userId);
console.info(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.info(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
Да, это работает, если я использую: if (exists = true){} Спасибо!
Вы пробовали это:
client.fetch(user_id)
?