What is wrong with this Unban command

I tried to make an Unban command but when I try to unban someone by using the user’s id, my bot just doesn’t ban them and I don’t know how to fix it.

The code:

module.exports = {
    name: 'unban',
    description: 'unban a member',
    async execute(bot, message, args, cmd, Discord) {
      let userID = args[0]
      let reason = args.slice(1).join(" ")

      if(!reason) reason = 'Unspecified'
      if(!args[0]) return message.reply("Please enter the ID of the user you want to unban.")
      if(!message.member.hasPermission(['BAN_MEMBERS', 'ADMINISTRATOR'])) return message.reply("You don't have the right permission to do that.")

      message.guild.fetchBans().then(async bans => {
        if(bans.size == 0) return message.reply("There are no Bans on this server")
        let bUser = bans.find(b => b.user.id);
        if(!bUser) return message.reply("The mentioned ID is not banned")
        message.guild.members.unban(bUser)
        message.reply("User unbanned!")
      })
    }
  }

The error:

(node:11908) UnhandledPromiseRejectionWarning: Error [BAN_RESOLVE_ID]: Couldn't resolve the user ID to unban.
at GuildMemberManager.unban (C:UsersADRIANDesktopZerOne BOTnode_modulesdiscord.jssrcmanagersGuildMemberManager.js:243:36)

Answer

The problem is let bUser = bans.find(b => b.user.id) returns the first user it finds in that collection because you don’t check if the user id is the same, you only check if b.user.id exists on the banned user. It simply returns true on the first banned user.

You should check if b.user.id === userID instead:

module.exports = {
    name: 'unban',
    description: 'unban a member',
    async execute(bot, message, args, cmd, Discord){
      let userID = args[0]
      let reason = args.slice(1).join(" ")

      if(!reason) reason = 'Unspecified'
      if(!userID) 
        return message.reply("Please enter the ID of the user you want to unban.")
      if(!message.member.hasPermission(['BAN_MEMBERS', 'ADMINISTRATOR'])) 
        return message.reply("You don't have the right permission to do that.")

      message.guild.fetchBans().then(async bans => {
        if(bans.size == 0) 
          return message.reply("There are no Bans on this server")

        let bUser = bans.find(banInfo => banInfo.user.id === userID);

        if(!bUser) 
          return message.reply("The mentioned ID is not banned")

        message.guild.members.unban(bUser.user.id)
        message.reply("User unbanned!")
      })
    }
  }

Leave a Reply

Your email address will not be published. Required fields are marked *