admin管理员组

文章数量:1317915

I have managed to make the unban mand 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.

I have managed to make the unban mand 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.

Share Improve this question asked Jun 24, 2019 at 8:08 Mr OchoaMr Ochoa 511 gold badge1 silver badge4 bronze badges 1
  • This might help – Kobe Commented Jun 24, 2019 at 8:10
Add a ment  | 

3 Answers 3

Reset to default 4

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);
}

An update due to Discord.js' package latest updates. The new method to make sure whether a user is banned or not is similar to the previous one, though you will now want to state:

const targetId = banList.get(guildmemberID).user

With "guildmemberID" being my constant, meaning you can replace it with anything you'd like, just as such:

const guildUser = args[0]

The "args[0]" variates according to what you are aiming to do. After putting everything together, your code could look similar to the following:

const guildmemberID = args[0]

try {
  const banList = await message.guild.fetchBans();
  const targetId = banList.get(guildmemberID).user

                      if(targetId) {
                        await message.guild.members.unban(targetId);
                        await message.channel.send('This user has been unbanned!');
                      } else {
                          await message.channel.send('This user is not banned')
                      }
} catch (err) {
   console.log(err);
}

Keep in mind that you are not able to use try/catch blocks if it's not within an async function.

I hope it helped!

According to discord.js v14.15.3, the fetchBans(); method has been removed.

See: https://discord.js/docs/packages/discord.js/14.15.3/GuildBanManager:Class#fetch

Instead, try this

// To find a particular banned member
const banned_user = interaction.guild.bans.fetch("<some-user-id>");

// To get full list of banned members
const ban_list = interaction.guild.bans.fetch();

本文标签: javascriptHow do I check if a user is banned or notStack Overflow