1

I'm working on a bot for a client, and I'm making a warning system that can be triggered by (prefix) warn <user mention>. I have the code below.

I can run the command once, and it will add the Warning 1 role, but subsequent commands don't add warnings 2 or 3.

if (mentionedUser.roles.cache.has(warn1)){
    if (mentionedUser.roles.cache.has(warn2)){
        message.channel.send('Already has 2 warnings.');
        mentionedUser.addRole(warn3);
    }
    else{
        message.channel.send('Already has 1 warning.')
        mentionedUser.addRole(warn2);
    }
}
else{
    mentionedUser.roles.add(warn1);
    message.channel.send('Warned the user.');
}
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • Looks like "mentionedUser.roles.cache.has(warn1)" is always false. Are you sure it's the good verification ? I dont know discord but why the role would be in cache ? – Leyff da Feb 12 '21 at 20:30
  • You could check these exemples: https://stackoverflow.com/questions/45317305/find-out-if-someone-has-a-role if it helps – Leyff da Feb 12 '21 at 20:35
  • 1
    @Leyffda discord.js v12 uses cache. in v11 it would have been `mentionedUser.roles.has(warn1)` – Anden Wieseler Feb 13 '21 at 05:07

1 Answers1

0

It looks like you're mixing v11 and v12 syntax. You add warn1 using roles.add (v12) but add warn2 using user.addRole (v11). If warn1 works, you're using discord.js v12, so you should use mentionedUser.roles.add everywhere:

// role IDs
const warn1 = '808376848172974143';
const warn2 = '808376936006418524';
const warn3 = '808377003580457000';
const mute = '808377222884753459';
const beenMuted = '810008640802258975';
const mentionedUser = message.mentions.members.first();

if (mentionedUser.roles.cache.has(warn1)) {
    if (mentionedUser.roles.cache.has(warn2)) {
        message.channel.send('Already has 2 warnings.');
        mentionedUser.roles.add(warn3);
    }
    else {
        message.channel.send('Already has 1 warning.')
        mentionedUser.roles.add(warn2);
    }
}
else {
    mentionedUser.roles.add(warn1);
    message.channel.send('Warned the user.');
}

With the code above the roles are added correctly:

enter image description here

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • I tried this option, and it didn't work for me. [here's the full thing if you want to check it out](https://pastebin.com/urxykWgT). please point out where I went wrong. – Anden Wieseler Feb 13 '21 at 04:54
  • @AndenWieseler Can you show me how you define the role variables? `warn1`, `warn2`, etc. – Zsolt Meszaros Feb 13 '21 at 11:57
  • `const warn1 = message.guild.roles.cache.get('808376848172974143') const warn2 = message.guild.roles.cache.get('808376936006418524'); const warn3 = message.guild.roles.cache.get('808377003580457000'); const mute = message.guild.roles.cache.get('808377222884753459'); const beenMuted = message.guild.roles.cache.get('810008640802258975');` – Anden Wieseler Feb 13 '21 at 18:24
  • Oh, I see, all you need is the ID of the role, so you need to use them like `const warn1 = '808376848172974143'`, etc. I've updated my answer. – Zsolt Meszaros Feb 13 '21 at 19:19
  • @AndenWieseler Did it work with the role IDs? – Zsolt Meszaros Feb 14 '21 at 21:53