1
votes

So im trying to make a command to mute people, and to unmute people, they work fine at adding the mute role and removing the mute role, but when I do mute someone they don't get muted and they can still talk, but i can see they have the muted role on them, and I disabled all talking permissions, and when someone sends a mute/unmute command without someones name the bot get an error saying Cannot read property 'roles' of undefined here is the code:

else if (parts[0] === Prefix + 'mute') {
        const args = message.content.trim().split(/ +/g);
        const target = message.mentions.members.first();
        const mutedRole = message.guild.roles.cache.find(
            (role) => role.name === 'Muted'
           );
           
           // if there is no `Muted` role, send an error
           if (!mutedRole)
            return message.channel.send('There is no Muted role on this server');
        target.roles.add(mutedRole);
        console.log(User + ' used the command ".mute ' + target + '"')
    
    } else if (parts[0] === Prefix + 'unmute') {
        const target = message.mentions.members.first();
        const args = message.content.trim().split(/ +/g);
        const mutedRole = message.guild.roles.cache.find(
            (role) => role.name === 'Muted'
           );
           
           // if there is no `Muted` role, send an error
           if (!mutedRole)
            return message.channel.send('There is no Muted role on this server');
        target.roles.add(mutedRole); 
        setTimeout(() => {
            target.roles.remove(mutedRole); // remove the role
          }, ms(args[1]))
          console.log(User + ' used the command ".unmute ' + target + '"')

and when I see it in the console log I don't see the name if the person being muted/unmuted but I see there ID number

1

1 Answers

0
votes

For "not muting correctly", that is likely a Discord permissions issue, so we can't help you resolve that here.

As for the command without someone's name, you try to do target.roles while target is undefined, hence the error. You need to make sure target is not undefined and, if it is, throw an error to the user (just like with mutedRole).