Event Handling
NYXBCommands can automatically handle your event listening in multiple files and folders. You can even setup "dynamic validations" to only run some events when a specific condition is met. More on that in the next section of the documentation. Here is an example of how to setup a basic event listening system:
Typescript
import path from 'node:path'
import { Client, IntentsBitField, Partials } from 'discord.js'
import NYXB from '@nyxb/commands'
require('dotenv/config')
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.DirectMessages,
IntentsBitField.Flags.MessageContent,
],
partials: [Partials.Channel],
})
client.on('ready', () => {
console.log('The bot is ready')
new NYXB({
client,
mongoUri: process.env.MONGO_URI || '',
commandsDir: path.join(__dirname, 'commands'),
events: {
// Where the events are stored
dir: path.join(__dirname, 'events'),
},
})
})
client.login(process.env.TOKEN)
File/Folder Structure
All files inside of the guildMemberAdd
folder will be executed when the event is emitted. The function inside of your event files should include all of the standard parameters for the event, plus an additional NYXBCommands
instance parameter at the end:
Typescript
import { Message } from 'discord.js'
import NYXB from '@nyxb/commands'
export default (message: Message, instance: NYXB) => {
console.log(message.content)
}
Command ValidationsSyntax validationsYou can provide your own syntax validations that will be ran against every command. This function should throw an error if the syntax is incorrect.Event HandlerDynamic ValidationsThis is a system to only run specific event files when a specific condition is met. For example you might only want to listen for the "messageCreate" event whenever a human sent a message and not a bot. You can use dynamic validations to achieve this without adding complexity to your code.