简体   繁体   中英

Checking if someone has a role with Discord.js

I'm trying to make a discord bot with discord.js but I have run into this error. I know there are other articles about this, but I just can't get this to work. I need to check if the user has an admin/mod role before running the command.

        case 'clear':
        if(!author.roles.cache.has('686001018277855308'))  return msg.reply('you dont have the permissions to do this.');
        else if(author.roles.cache.has('686001018277855308')){

            if(!args[1]) return msg.reply('please add the number of messages to clear in the command');
            else{
                msg.channel.bulkDelete(args[1]);
                msg.reply(args[1] + ' messages cleared!');
            }
        }

    break;

I just can't get this to work. I got the following error:

C:\Program Files\nodejs\node.exe index.js froggo is online: c.\Users\frenc\OneDrive\Documents\GitHub\froggobot\index:js.53 if(.author.roles.cache.has('686001018277855308')) return msg;reply('you dont have the permissions to do this.'); ^

TypeError: Cannot read property 'cache' of undefined

If you need any other info, please ask in the comments.

One more question, is it possible to save this process as a single command and use it in other files without retyping the whole thing?

My suggestion is that you just try to check if the user can delete messages sent by other people via a permissions check.

Here is what I would do:

case 'clear':
    if(!msg.member.permissions.has('MANAGE_MESSAGES')) return msg.reply('you dont have the permissions to do this.');
    if(!args[1]) return msg.reply('please add the number of messages to clear in the command');

    msg.channel.bulkDelete(args[1]);
    msg.reply(args[1] + ' messages cleared!');
break;

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