Search code examples
javascriptdiscord.jsprefixowner

How to define a particular prefix that can only be used by the owner of the bot?


I am trying to set another prefix but only usable by me while keeping the usual prefix for other users.

client.on('message', async message => {
  const prefix = '.' 
  if(message.content.startsWith(prefix)) {
    const messageArray = message.content.split(' ');
    const cmd = messageArray[0];
    const args = messageArray.slice(1);
    const command = client.commands.get(cmd.slice(prefix.length))
                 || client.commands.get(client.aliases.get(cmd.slice(prefix.length)));
    // Command execution
});

Solution

  • You can easily assign a prefix depending on whether the user is the owner or not by using ternary operator, frequently used as alternative to an if...else statement.

    If the message author isn't the owner, the prefix will be ​​the default one otherwise it will be the owner prefix:

    const prefix = message.author.id !== OWNER_ID ? '.' : OWNER_PREFIX; 
    

    Both prefixes can be used by the owner but only the default one for users:

    const prefix = message.content.startsWith('.') ? '.'
                 : message.author.id === OWNER_ID ? OWNER_PREFIX
                 : '.';