简体   繁体   中英

Ban command discord.js v12

I made a ban command in discord.js v12. However whenever i run the command I get an error. Here is my code:

const Discord = require('discord.js');

module.exports = {
    name: "ban",
    description: "Kicks a member from the server",

    async run (client, message, args) {

        if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send('You can\'t use that!')
        if(!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send('I don\'t have the right permissions.')

        const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);

        if(!args[0]) return message.channel.send('Please specify a user');

        if(!member) return message.channel.send('Can\'t seem to find this user. Sorry \'bout that :/');
        if(!member.bannable) return message.channel.send('This user can\'t be banned. It is either because they are a mod/admin, or their highest role is higher than mine');

        if(member.id === message.author.id) return message.channel.send('Bruh, you can\'t ban yourself!');

        let reason = args.slice(1).join(" ");

        if(!reason) reason = 'Unspecified';

        member.ban(`${reason}`).catch(err => { 
          message.channel.send('Something went wrong')
            console.log(err)
        })

        const banembed = new Discord.MessageEmbed()
        .setTitle('Member Banned')
        .setThumbnail(member.user.displayAvatarURL())
        .addField('User Banned', member)
        .addField('Kicked by', message.author)
        .addField('Reason', reason)
        .setFooter('Time kicked', client.user.displayAvatarURL())
        .setTimestamp()

        message.channel.send(banembed);


    }
}

As I had mentioned earlier I keep getting an error whenever I run code. Bit new to coding and I was'nt able to resolve the problem my self. Here is the error I recive:

Response: Internal Server Error
    at RequestHandler.execute (/home/runner/Utki-the-bot/node_modules/discord.js/src/rest/RequestHandler.js:158:11)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 500,
  method: 'put',
  path: '/guilds/751424392420130907/bans/155149108183695360'
}

Can you help me out? Thanks in advance!

The error is not yours. The error maybe is occasioned by Discord servers or by discord.js. You can wait until it gets solved. Maybe some minutes.

I want show you how i made my ban and temp ban command in discord.js v12

I hope what this code can help you and thanks for read

const reasonBan = msg.slice(3)
const msg = message.content.split(" ")
const args = msg.slice(1)

  if (cmd === "bt!ban")
  {
    
    const person = message.mentions.users.first()

    if(message.member.hasPermission("ADMINISTRATOR") || message.member.hasPermission("BAN_MEMBERS"))
    {
      if(args.length === 0)
      {
        embed
          .setAuthor("BAN COMMAND" , client.user.avatarURL())
          .setDescription(`**Tienes que especificar dar los argumentos del comando de la siguiente manera:**
          
          \`bt!ban <usuario> <tiempo> <razón> \` `)
          .setColor("RANDOM")
          .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
        return message.channel.send(embed)
      }
      else
      {
        if(!person)
        {
          embed
            .setAuthor("BAN COMMAND" , client.user.avatarURL())
            .setDescription(`**${message.author}, tienes que mencionar a alguien a quién banear.**`)
            .setColor("RANDOM")
            .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
          return message.channel.send(embed)
        }
        else
        {
          if(person.id === message.author.id)
          {
            embed
              .setAuthor("BAN COMMAND" , client.user.avatarURL())
              .setDescription(`**${message.author}, No puedes banearte a ti mismo**`)
              .setColor("RANDOM")
              .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
            return message.channel.send(embed)
          }
          else if(person.id === message.guild.me.id)
          {
            embed
              .setAuthor("BAN COMMAND" , client.user.avatarURL())
              .setDescription(`**${message.author}, No puedes banearme a mi. >:c**`)
              .setColor("RANDOM")
              .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
            return message.channel.send(embed)
          }
          else
          {
            const isBannable = message.guild.member(person)
            

            if(isBannable.bannable)
            {
              try 
              {
                isBannable.ban(
                  {
                  reason: 
                    reasonBan.length === 0 ?
                    "Razón no espeficada":
                    reasonBan.join(" ")
                })
                .then(() => 
                {
                  embed
                    .setAuthor("BAN COMMAND" , client.user.avatarURL())
                    .setDescription(`**${message.author}, el usuario ${person}, ha sido baneado correctamente.**
                    
                    **Razón : ${reasonBan === 0? "Razón no espeficada" : reasonBan.join(" ")}**
                    **Tiempo: ${args[1]}**`)
                    .setColor("RANDOM")
                    .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
                  return message.channel.send(embed).then(() =>
                  {
                    setTimeout(() => {

                      try 
                      {
                        message.guild.fetchBans().then( bans =>
                          {
                            message.guild.members.unban(person)
                          })
                        embed
                          .setAuthor("BAN COMMAND" , client.user.avatarURL())
                          .setDescription(`**${message.author}, el usuario ${person}, ha sido desbaneado correctamente.
                            
                          **Razón : ${reasonBan === 0? "Razón no espeficada" : reasonBan.join(" ")}
                          Tiempo: ${args[1]}**`)
                          .setColor("RANDOM")
                          .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
                
                        return message.channel.send(embed)
                      } 
                      catch (err) 
                      {
                        embed
                          .setAuthor("BAN COMMAND" , client.user.avatarURL())
                          .setDescription(`**${message.author}, Ha ocurrido un error al intentar quitar el ban de ${person}.**`)
                          .setColor("RANDOM")
                          .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
                
                        return message.channel.send(embed)
                      }
                    }, ms(args[1]))
                  })
                })
              } 
              catch (error) 
              {
                isBannable.ban(
                  {
                    reason: 
                      reasonBan.length === 0 ?
                      "Razón no espeficada":
                      reasonBan.join(" ")
                  }
                )
                .then(() => 
                {
                  embed
                    .setAuthor("BAN COMMAND" , client.user.avatarURL())
                    .setDescription(`**${message.author}, el usuario ${person}, ha sido baneado correctamente.**
                    
                    **Razón: ${reasonBan === 0? "Razón no espeficada" : reasonBan.join(" ")}**`)
                    .setColor("RANDOM")
                    .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
                  return message.channel.send(embed)
                })
              }
            }
            else
            {
              embed
                .setAuthor("BAN COMMAND" , client.user.avatarURL())
                .setDescription(`**${message.author}, el usuario tiene más permisos que yo o no está en el server.**`)
                .setColor("RANDOM")
                .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
        
              return message.channel.send(embed)
            }
          }
        }
      }
    }
    else
    {
      embed
        .setAuthor("BAN COMMAND" , client.user.avatarURL())
        .setDescription(`${message.author} ,No tienes permisos para ejecutar este comando.`)
        .setColor("RANDOM")
        .setFooter("Ban Command" , "https://cdn.discordapp.com/emojis/824094234801602590.png?v=1")
    
      return message.channel.send(embed) 
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM