简体   繁体   中英

discord.js Bot quits if I mention a user when its expecting a role

I am making a command where the bot can change the color of a role by command

var red = ['#ff0000']


client.on('message', message => {
    var colorRole = message.mentions.roles.first() || message.guild.roles.cache.find(role => role.name === args[0].join(" "));
        if(message.content.startsWith === 'grimm!change red'){
                colorRole.edit({
                    color: red[0]
                });
       }})

Im having an issue where if I type the command, and then mention the role, it does not change the role color, nor does it say anything in the command prompt as well, and I have been rearranging the code.

Im also having issues with another peice of code as well

client.on('message', message => {   
    if (message.content.startsWith('grimm!test')) {
        let colorRole = message.mentions.roles.first() || message.guild.roles.cache.find(role => role.name === args[0].join(" "));
        message.channel.send(`${colorRole}`);
    
        const roleEmbed = new Discord.MessageEmbed()
            .setColor('#ff6700')
            .setAuthor(colorRole)
        
        message.channel.send(roleEmbed);
    }
})

This code was used by me to test to see if my bot could recognize a role, and it worked, but everytime I accidentaly mentioned someone, the bot would give me an error and the bot would quit, is there any way to fix both of these things? I would just like the code to be able to change the color of the role that was mentioned, and it would not quit the bot if I mention a person. Thanks guys!

To address the issue of a crash it's because when you tag a user. You are searching for their username as a role. as such, your check will return undefined & You are not handling this error so the bot cannot continue with its action so will lockup.

I have made slight modifications to your examples and the comments should clear up the changes

  const colorRole = message.mentions.roles.first() || message.guild.roles.cache.find(R => R.name === args.join(" "));
    if (!colorRole) return;   // Check if color role is undefined, if it is. Then Return aka do nothing. 
    const roleEmbed = new Discord.MessageEmbed()
      .setColor(`#ff6700`)
      .addFields(
        {name:"Role Name",value:colorRole.name, inline:true},
        {name:"Role ID",value:colorRole.id,inline:true},
        {name:"Role Positon", value:colorRole.rawPosition, inline:false}
      )

      message.channel.send(roleEmbed);

Modified the embed to give it a clear indication that the correct role has been found. keep in mind rawPosition can be useful in future error handling due to discords role hierachy.

With the second issue about updating your role color, I have added two checks.

  1. check that colorRole is an object and not undefined
  2. check that the bot has permission to edit roles

If either of these two checks fail, the bot will gracefully return and stop the command from executing & The actual updated script is

const colorRole = message.mentions.roles.first() || message.guild.roles.cache.find(R => R.name === args.join(" "));

if (!colorRole) return;   // Check if color role is undefined, if it is. Then Return aka do nothing.

if (!message.guild.me.hasPermission('MANAGE_ROLES')) return // Bot needs this position to perform this action 
  colorRole.edit({
       color:'#ff6700'
})
.then(message.channel.send("Successfully Updated Role Color"))
.catch(Err=> console.log(Err));

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