Creating an Event

To create a new event for your bot to listen to, all you need to do is create a new file in your events directory, which in this case is "events", and import the class named "Event" from the module. After that, all you need to do is export a new instance of the class and there you have it! A new event file!

JavaScript
TypeScript
JavaScript
// ./events/guildMemberAdd.js
const { Event } = require("cdcommands");
module.exports = new Event();
TypeScript
// ./events/guildMemberAdd.ts
import { Event } from "cdcommands";
export default new Event();

Now, again, of course this event wont do anything for you, so we need to add a couple properties to the class. We are going to assign an event name, which is the same as assigning an event name in your regular <Client>#on listener, then we will also assign a callback function, which will have your client as CDClient as the first parameter, followed by the parameters that the regular listener has.

Note: Event files must use the Event class or else they will not be loaded. A bonus of using the class is that you get powerful intellisense as if you were using a regular client listener!

JavaScript
TypeScript
JavaScript
// ./events/guildMemberAdd.js
const { Event } = require("cdcommands");
module.exports = new Event("guildMemberAdd", (client, member) => {
client.logInfo({
data: `It seems that ${member.user.username} has joined a server!`
});
})
TypeScript
// ./events/guildMemberAdd.ts
import { Event } from 'cdcommands';
import { GuildMember } from 'discord.js';
import { CDClient } from 'cdcommands/src/types/helper.types';
export default new Event('guildMemberAdd', (client: CDClient, member: GuildMember) => {
client.logInfo({
data: `It seems that ${member.user.username} has joined a server!`
});
})

The above event, once your bot logs in, should log a message along the lines of It seems that User has joined a server! every time a user joins a server that the bot is in. Since we are using the "guildMemberAdd" event in this example, the parameters in the callback function are expected to be first your client, then followed by the member that joined.

Note: A message event file is loaded by default to allow all the different checks for permissions and others to work. The loading of this event can be disabled by setting the customMessageEvent property to true in the CDCommands class, though it is not recommended as it will break a lot of your commands functionality. If you want to make your own message event you can use this template here