Skip to content

Events

The event component wraps the discord.js’ built in events so you should be able to hit the ground running. There is also complete type safety so your editor will be able to provide helpful hints! As an example let’s create an event that reacts when your bot is online and ready:

import { event } from 'jellycommands';
export default event({
name: 'ready',
run() {
console.log('Hello World!');
},
});

Handling Events

When an event is emitted, it’s handler function (run) recieves specific ordered arguments. The first argument is always the event context (which includes the client and props). The remaining arguments correspond to the event type, as defined in the Discord.js docs.

Let’s look at the channelUpdate event as an example. According to the Discord.js Client documentation, this event provides two arguments: oldChannel and newChannel.

discord.js showing channelUpdate(oldChannel, newChannel)

We can then combine that to create our event handler:

import { event } from 'jellycommands';
export default event({
name: 'channelUpdate',
run(ctx, oldChannel, newChannel) {},
});

Event Context

We pass the “event context” as the first argument to the run fn, which is an object that includes:

  • client - this is your JellyCommands client
  • props - this is a shortcut to your client.props

For example, let’s destructure these from the context of the above example:

import { event } from 'jellycommands';
export default event({
name: 'channelUpdate',
run({ client, props }, oldChannel, newChannel) {
// use your client and props
},
});

Running only once

You can configure an event handler to run only once, then automatically remove itself. This once option is specific to each individual component - other event components listening for the same event will continue to run normally.

export default event({
name: 'channelUpdate',
// This handler will only run once
once: true,
run(ctx, oldChannel, newChannel) {},
});