How do I check if a user is banned or not?

冷暖自知 提交于 2019-12-24 07:40:12

问题


I have managed to make the unban command to work, but now I need to make a condition to avoid errors, and need to put the unban code inside the condition if the user is in the ban list, but I have no idea how and haven't seen anything on internet.


回答1:


You can use Guild.fetchBans() to retrieve a Collection of banned users. Keep in mind, this method returns a Promise. You can then use Collection.find() to search through the Collection.

Example:

// Async context needed for 'await' (meaning this must be within an async function).
// Assuming 'message' is a Message within the guild.

try {
  const banList = await message.guild.fetchBans();

  const bannedUser = banList.find(user => user.id === 'someID');

  if (bannedUser) await message.channel.send(`${bannedUser.tag} is banned.`);
  else await message.channel.send('That user is not banned.');
} catch(err) {
  console.error(err);
}


来源:https://stackoverflow.com/questions/56732330/how-do-i-check-if-a-user-is-banned-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!