Discord.js client.on

17,228

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);

    }
}
Share:
17,228
Modular
Author by

Modular

Wanting to become a good coder.

Updated on June 14, 2022

Comments

  • Modular
    Modular almost 2 years

    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);
      }
    });
    
  • Oliver
    Oliver about 6 years
    The message event will be triggered when the client gets a new incoming message. Having multiple listeners for one event is, as André said, useless, and will consume more ram than necessary.