Lunyae
Lunyae

Reputation: 3

Bot name as prefix

I want to be able to make the prefix the name of the bot so I can do like

 if(cmd === `${prefix}Hey`) {
            const m = await message.channel.send(`Hello`);
      }

Where you could use the bot name as prefix "(Bot name) hey" and it would answer hello back since when I try you can only do it without the space like "(bot name)hey" and it would answer hello back. I don't know if that made sense.

Upvotes: 0

Views: 364

Answers (2)

yaas
yaas

Reputation: 144

Possibly something like this in your main file :

const client = new Client(); // Just to represent the bot
client.on('message', (message) => {
  const mentionRegex = new RegExp(`^<@!?${client.user.id}>`);
  if (mentionRegex.test(message.content) ||
      message.content.startsWith(prefix)) {

    let cmd; 
    const words = message.content.split(' ');
    if (message.content.startsWith(prefix) {
      cmd = words.shift().slice(prefix.length)
    } else {
      cmd = words.splice(0, 1, '').shift();
    }

    switch (cmd) {
      case 'hey':
        message.channel.send('Hello!');
        break;
    }
  }
});

Upvotes: 0

DeAl
DeAl

Reputation: 19

This should work.

This may helping you. Docs

if (cmd.startsWith(message.client.user.username)) {
    const command = cmd.replace(message.client.user.username, "").toLowerCase();
    if (command === "hey") {
       ...
    }
}

Upvotes: 2

Related Questions