'Why does my Discord bot keep repeating when it runs this command?

This is the code used, there is nothing (I think) that's causing it to repeat, matter of fact I added something so it stops repeating but it didn't do anything at all.

client.on('message', e =>{
    if(e.member.roles.cache.has('12345678901')){
        if(!e.content.startsWith(p) || e.author.bot) return;
        console.log('successfully unmuted')
        var ar = e.content.slice(p.length).split(/ +/)
        var cmd = ar.shift().toLowerCase();
    
        if(cmd === 'b'){
            console.log('succesfully banned')
            var member = e.mentions.users.first();
            if(member){
                var membertarget = e.guild.members.cache.get(member.id)
                membertarget.ban();
                var sbbed = new discord.MessageEmbed()
                .setColor('GREEN')
                .setTitle('success!')
                .setDescription(`<@${membertarget.user.id}> has been successfully banned!`)
            }else {
                var bbed = new discord.MessageEmbed()
                .setColor('RED')
                .setTitle('invalid user!')
                .setDescription('ban failed because there was not a valid member mentioned :(')
                e.channel.send({ embeds: [bbed] })
            }
        }
    } else {
        var rolefb = new discord.MessageEmbed()
.setColor('RED')
.setTitle('failed!')
.setDescription('sorry! you dont have a high enough role to use this command, but this can change!')
        if (e.author.bot) return;
        e.channel.send({embeds: [rolefb]})
    }

    
})

This code is supposed to just ban somebody but it keeps repeating itself whenever it fails:

issue

Code:

client.on('message', e =>{
    if (e.author.bot) return
    if(e.member.roles.cache.has('960191891473829929')){

I have edited the code, but it still doesn't work.



Solution 1:[1]

Your code runs whenever a message is posted in your server so the bot sending the message will also be counted. So all you have to do is add an if check in the start to check whether the author of the message was a bot:

if (message.author.bot) return

Solution 2:[2]

Try to add this statement to your code before if(e.member.roles.cache.has('12345678901')){

if (e.author == client.user) return;

Also, change the if(e.member.roles.cache.has('12345678901')){ to else if (e.member.roles.cache.has('12345678901')){.

I would recommend restructuring the entire code and make it into a command handler, though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caladan
Solution 2