Modular
Modular

Reputation: 149

Discord.js client.on

My code is this, do I also have to have the client.on('message', message => { or not?

client.on('message', message => {
  if (message.content.startsWith(prefix + "ping")) {
    message.channel.send('Pong! <:Pingsock:433019097005948938>');
  }
});

client.on('message', message => {
  if (message.content.startsWith(prefix + "avatar")) {
    message.reply(message.author.avatarURL);
  }
});

Upvotes: 3

Views: 26758

Answers (2)

Torren
Torren

Reputation: 63

You can have multiple commands under the same message event, like this.

client.on('message', message => {
  if (message.content.startsWith(prefix + "ping")) {
    message.channel.send('Pong! <:Pingsock:433019097005948938>');
  }

  if (message.content.startsWith(prefix + "avatar")) {
    message.reply(message.author.avatarURL);
  }

  // You can add more commands here with more if statements

}); // this ends the client.on that is at the start, so don't add commands below this

I think that should work. It's easy to do like it like that without client.on for every command.

Upvotes: 0

Andr&#233;
Andr&#233;

Reputation: 4497

If your question is if you should have one event per command, absolutely not.
There is a limit for how many events a bot can "subscribe", and you would use all of them with just a few commands. And also that would be terrible for performance because it will trigger all the events for every single message sent.
You should instead have one event and check which command was used.

client.on("message", message => {
    if(message.content.startsWith(prefix + "ping")){
        message.channel.send('Pong! <:Pingsock:433019097005948938>');

    } else if (message.content.startsWith(prefix + "avatar")) {
        message.reply(message.author.avatarURL);

    }
}

Upvotes: 4

Related Questions