Search code examples
javascriptnode.jsif-statementvardiscord.js

Check if command is executed


I have a problem with my restriction system for commands.
The message 'you don't have permission' will only run once (when you don't have the role). I'm aware that if you don't set a var (in this case checkrole) the code will be executed once however, I don't know how to check when a command is executed.

So I could do something like this:

if  (command === ' ') {
    checkrole = false
}

Code of interest:

if (!message.member.roles.some(r => ["someone"].includes(r.name.toLowerCase())) && checkrole === false) {
    checkrole = true;
    return message.channel.send('Sorry, but you do not have the **permissions** to do that.');
  }

(var checkrole is defined - it begins as false (var checkrole = false;))

Kind regards, Ruben


Solution

  • Usually if you want to use commands you need a prefix. Once you chose that, you know that the content of the message (Message.content) will start with prefix + command. So you could write it like this:

    // Assuming that:
    // var prefix = '-'; for example
    if (message.content.startsWith(prefix + "mycommand")) {
      checkrole = false;
    }
    

    Although this works, there are more efficient ways to use commands, especially if you'll need to use arguments and so on...
    Reading this guide might help you: it covers the basics but also more advanced stuff ;)