How does features and events work?

Welcome to Events & Features section!

Features

A feature can include multiple event listeners and other systems to handle how your bot works. You can create a "features" folder and place all your features in that folder.

The files will be automatically imported and ran. Here's an example:

features/server-logs.js
const { Feature } = require('sl-commands')

new Feature()
  /** This will be executed once when the handler starts */
  .setInit(({ client, handler }) => {
    console.log('This will be ran once.')
  })
  /** This will be executed continuously with the interval of your choice */
  .addTimed(({ client, handler }) => {
    console.log('This will be ran every 5 seconds.')
  }, 5000) // 1000 = 1 second

Events

But if you want just one event listener and nothing more, you can create an event in an "events" folder. The files imported will be ran whenever the event is emitted. You can create an event using this structure:

events/welcome.js
const { Event } = require('sl-commands')

/** 
 * In this case the event will be ran whenever thE
 * "guildMemberAdd" event is emitted. 
 */

/** 
 * The first argument must be a keyof DiscordJS ClientEvents 
 * or SLCommands HandlerEvents
 */
new Event(
  'guildMemberAdd', 
  ({ client, handler }, member) => {
    /** Access the guild that they joined */
    let { guild } = member
  
    /** Get the channel named "welcome" */
    let channel = guild.channel.cache.find(
      (channel) => channel.name === 'welcome'
    )
  
    /** Ensuring the channel exists */
    if (!channel) return
  
    /** Send the welcome message */
    channel.send(`Welcome ${member}!`)
  },
  false // Here you can set if the event will be ran just once or not.
)

This can be used to handle SLCommands events too! You can see more here:

Last updated