Advanced Discord.js Bot Template (With database and extensions!)
March 2022 - Locking this, it's out of date and broken, keep getting notifs for it. - Zave
Advanced Modular Discord.js Bot Tempate
Introduction
So for a while now I've been working on a bot named Lilac2. I built I really in depth base that allows me to do a lot of things with the bot more easily. I've been suggested I strip it down and make a template using the boilerplate code I created.
Requirements
- Firestore database with service account and key
- Discord bot account
Setup
- Setup a Firestore database and generate a service account with read/write permissions.
- Generate a key for the account, and export it as JSON.
- In bot/database/key.js:
module.exports = /* firestore json key */
- In bot/config.js, set bot.token to your bot's token, and set any other properties you'd like to change, such as name, owner, developers, etc.
- If you have done this all correctly, run the bot! (Entry point is bot/index.js if you're not using the bash script provided)
Adding Extensions
This bot supports extensions. These are like modules for the bot, and each one can hold commands or peform actions I have included a standard extension with some commands in bot/extensions.
To add your own:
- Create a JS file in bot/extensions
- Add:
module.exports = function(context) { this.name = 'Extension name here!' // required this.description = 'Extension description here!' // required /* whatever code you want the extension to do in here */ }
Adding Commands
- First, you'll you need to either create a new extension or go to the one you'd like to add commands to.
- If it does not exist, add a
this.commands = {}
to the extension's exported function. Ex:
module.exports = function(context) { this.name = 'Extension name here!' // required this.description = 'Extension description here!' // required /* whatever code you want the extension to do in here */ this.commands = {} }
- Add a property to
this.commands
for a command! For example:
this.commands = { 'my-cmd': { /* description and callback are required in a command */ description: 'lol this is my command' callback: message => { /* what to do when a user uses the command */ } } }
Optional properties for commands
You can add special properties to commands to make them behave differently. For example:
'my-cmd': { cooldown: 5000, // cooldown of 5000 ms (5 secs) minArgs: 1, // minimum args the commnd takes maxArgs: 1, // maximum args the commands takes permissions: ['MANAGE_MESSAGES'] // permissions needed to use command arguments: ['arg-1'] // the arguments names callback: (message, arguments) => { console.log(arguments['arg-1']) } }
All properties
description
A string about the purpose of a command. REQUIRED.callback
What to do when the command is executed by a user. REQUIRED.cooldown
Amount of milleseconds the user must wait before using the command again.hidden
Hides the command from being displayed in the included help command.true | false
disabled
Prevents the command from being loaded into the bot, both hiding it and preventing it from being used.true | false
developerOnly
Whether the command is only for developers. If true, the command cannot be used by anyone not inconfig.developers
.true | false
arguments
This is an array of the names for the arguments of a command. Ex:['arg1', 'arg2']
minArgs
The minimum amount of arguments for a command. By default 0.maxArgs
The maximum amount of arguments for a command. By default 0.permissions
An array of permissions required to use the command. Uses Discord.js permission flags.
Adding Discord.js Event Listeners in Extensions
Extensions can also include their own discord.js event listeners, such as client.on('message', () => {})
, however they have an alternate implentation. For example, if I want to add a message event in an extension:
module.exports = function(context) { this.name = 'foo' this.description = 'bar' context.botOn('message', message => { /* do stuff here */ }) }
This works with ALL Discord.js events and can be used multiple times. It is identical in format to regular client.on
, but allows you to use them in extensions themselves.
Example Bots
Help
The fastest way to get help using this template is to join the Lilac2 Discord. This template is based upon the Lilac2 bot's source.
it doesent work
impressive
👍 :D
wowsies, just one thing, how to firebase?
@TsunamiOrSumth https://firebase.google.com/docs/firestore
Should have everything you need. If you struggle just get in contact with me on Discord in the link in the tutorial and I can give you more detailed help.
pog. firebase pog.
(Also, thx for teaching me node a few months ago, i know so much more now :) )