What is an Event Listener?

What the bot "listens" for. Let's use the code example from the Discord.js website: https://discord.js.org/

const Discord = require("discord.js");

const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('token');

The two event listeners in this code are the ready event, and the message event. Each event is only fired off when "something happens", in this case, the ready event is fired when the client is ready, and the message event is fired when a message is sent.

Simply for the ready event:

When I am ready, I will fire the code in the ready event.

And for the message event:

When a message is sent, I will fire the code in the message event.

How do I know what event does what then? Read the docs. The client class gives you a list of events, each has a short description of what it does.

So what does the .on bit mean?

It's some neat Node.js thing that says "on this event", which means it will continue to listen for the event even after it's been fired once. If you were to replace .on with .once , it would only run that event once.

How do I know if the event I'm using needs parameters?

Read the docs. In the case of the message event, it requires only one parameter, which is the message that was sent/created.

In this example, we define the created message as msg.

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

You could define is as anything, like this:

client.on('message', aaaaaaaaaaaaaaaaaaaaaaaaaa => {
  if (aaaaaaaaaaaaaaaaaaaaaaaaaa.content === 'ping') {
    aaaaaaaaaaaaaaaaaaaaaaaaaa.reply('Pong!');
    }
});

You just have to make sure whatever you assign message as is the same as what you use in the rest of your event, such as above.

Help! The event I need to use requires multiple parameters!

In the case of the updateMessage event, it has two parameters that need to be defined. Be sure to enclose them in brackets1!

client.on('messageUpdate', (staleMeme, freshMeme) => { // Parameters are enclosed in brackets
  staleMeme.channel.send(`${staleMeme.content} => ${freshMeme.content}`);
});

What about no parameters?

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

Just use an empty pair of brackets!

A giant big nono coming up

const Discord = require("discord.js");

const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`); // Bot logs in
});

client.on('message', msg => {
  if (msg.content === 'ping') { // Ping
    msg.reply('Pong!'); // Pong!
  }
});

client.on('messageDelete', deletedMsg => {
  if (msg.channel.type === 'dm') return; // Ignores deleted messages in DMs
  if (msg.user.id === client.user.id) return; // Ignores bot's own deleted messages
  msg.guild.channels.get('super-secret-hidden-channel-id').send(`**${msg.user.tag} deleted a message!:**\n\n${msg.content}`);
});

client.login('token');

THIS CODE IS WRONG BECAUSE IT'S USING msg OUT OF IT'S SCOPE. You are also not using the parameter you passed in the messageDelete event.

client.on('message', msg => { // msg is now defined
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});
// msg is no longer defined rip

The correct code for the messageDelete event is as follows:

client.on('messageDelete', deletedMsg => {
  if (deletedMsg.channel.type === 'dm') return; // Ignores deleted messages in DMs
  if (deletedMsg.user.id === client.user.id) return; // Ignores bot's own deleted messages
  deletedMsg.guild.channels.get('super-secret-hidden-channel-id').send(`**${deletedMsg.user.tag} deleted a message!:**\n\n${deletedMsg.content}`);
});

This means you can do something like this and it'll still work2:

client.on('message', msg => { // msg is now defined as the message that was sent
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});
// msg is no longer defined rip

client.on('messageDelete', msg => { // ayy msg is defined again but instead as the message that was deleted
  if (msg.channel.type === 'dm') return;
  if (msg.user.id === client.user.id) return;
  msg.guild.channels.get('super-secret-hidden-channel-id').send(`**${msg.user.tag} deleted a message!:**\n\n${msg.content}`);
});
// msg not defined anymore

BONUS:

Remember how I mentioned that you can define your parameter as anything? You can define client and Discord as anything too! Just make sure everything you've

const Dicksword = require("discord.js");
const crappybot = new Dicksword.Client();

And that should be it I think.

1. It's good practise to enclose all parameters like in the example, however it is not required for events that only require one parameter like the message event.
2. I would not advise doing this for readability.

results matching ""

    No results matching ""