Creating an addon
This guide explains how to create a basic addon for our bots. Addons allow users to extend the bot's functionality without modifying the main codebase.
What is an Addon?
An addon is a file or group of files inside the addons/ folder that can:
Listen to events: React to things happening in the bot (e.g., when a message is sent or a member joins).
Add slash commands: Create commands that users can type into Discord starting with
/.
You can choose to use only events, only commands, or both in an addon. It's up to you!
Addon File Requirements
Event Listener Files:
These files can be named anything (e.g.,
myEvents.js).Use event listeners to react to bot activities, like a user sending a message.
Slash Command Files:
These files must start with
cmd_(e.g.,cmd_exampleCommand.js).Use slash commands to create commands users can trigger with
/.
How the Addon System Works
Event Handlers:
All events (like
messageCreateorguildMemberAdd) are centralized in the bot using something called aneventHandler.You can use
onto listen for these events in your addon.
Slash Command Handlers:
If you include a file that starts with
cmd_, the bot will automatically load it as a slash command.
Step 1: Creating an Event Listener (Optional)
To listen to events, create a new file in the addons/ folder. The file name can be anything, like myEventAddon.js.
Example: Listening to messageCreate
Here’s how you can listen for messages and respond when someone says !hello:
Step 2: Creating a Slash Command (Optional)
If you want to create a slash command, the file must start with cmd_, for example: cmd_greet.js.
Example: A Simple Slash Command
Here’s how you can create a /greet command:
Step 3: Combining Events and Commands
You can create both an event listener and a slash command in the same addon. Just use a separate file for each.
Example File Structure
Example: myEvents.js
Example: cmd_greet.js
Step 4: Loading Your Addon
Place your addon files in the
addons/folder.The bot will automatically detect and load event listener files and slash commands when it starts.
Advanced Example: Fully Featured Addon
Here’s an example addon that listens for events and includes a slash command:
File Structure
File: events.js
This file contains event listeners for various bot events.
File: cmd_advanced.js
This file contains a slash command definition and its execution logic.
How to Create an Addon with MongoDB Integration
Integrating MongoDB with your addon is simple and allows you to store and retrieve data from the same database used by the main bot. This guide will show you how to set up a MongoDB model in your addon and use it effectively.
Step 1: Create a MongoDB Model
Add your MongoDB models in the same folder as your addon. For example, you can create a file called exampleModel.js inside your addon folder.
File: exampleModel.js
Here’s an example model for storing example data:
Step 2: Use the Model in Your Addon
To use the model in your addon, import it just like you would in any other Node.js project. You can now interact with the database using MongoDB methods.
Here’s an example slash command to interact with the database:
Full Example Addon with MongoDB integration
File Structure
Code Recap
exampleModel.js:
events.js:
cmd_example.js:
Last updated
Was this helpful?