Skip to content
Sign upLog in
← Back to Community

Advanced Discord.js Bot Template (With database and extensions!)

Profile icon
Zavexeon

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

  1. Setup a Firestore database and generate a service account with read/write permissions.
  2. Generate a key for the account, and export it as JSON.
  3. In bot/database/key.js:
module.exports = /* firestore json key */
  1. 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.
  2. 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:

  1. Create a JS file in bot/extensions
  2. 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

  1. First, you'll you need to either create a new extension or go to the one you'd like to add commands to.
  2. 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 = {} }
  1. 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

  1. description A string about the purpose of a command. REQUIRED.
  2. callback What to do when the command is executed by a user. REQUIRED.
  3. cooldown Amount of milleseconds the user must wait before using the command again.
  4. hidden Hides the command from being displayed in the included help command. true | false
  5. disabled Prevents the command from being loaded into the bot, both hiding it and preventing it from being used. true | false
  6. developerOnly Whether the command is only for developers. If true, the command cannot be used by anyone not in config.developers. true | false
  7. arguments This is an array of the names for the arguments of a command. Ex: ['arg1', 'arg2']
  8. minArgs The minimum amount of arguments for a command. By default 0.
  9. maxArgs The maximum amount of arguments for a command. By default 0.
  10. 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.

This post has been locked
Voters
Profile icon
MSB-GAMERZGAMER
Profile icon
tazkeron2007
Profile icon
Brokenexperien1
Profile icon
DeepakSharma53
Profile icon
MCCOASTERS
Profile icon
FauxElements
Profile icon
MelvinMellow
Profile icon
LiliumMaid
Profile icon
dudeactualdev
Profile icon
CodingCactus
Comments
hotnewtop
Profile icon
EpicGamer007

pog. firebase pog.

(Also, thx for teaching me node a few months ago, i know so much more now :) )

Profile icon
Chezical

I need help

Profile icon
Chezical

@Chezical
I don't know how to set it up and I need some ones help to do it

Profile icon
noxitb

it doesent work

Profile icon
Zavexeon

@noxitb
It indeed does! I've tested it out before posting, and it's based upon a bot that has been running for months now. Do you need any help? It does require some setup.

Profile icon
noxitb

yeah i would like some help

@Zavexeon

Profile icon
Zavexeon

@noxitb
Alrighty, could you invite me to the repl?

Profile icon
CodingCactus

impressive

Profile icon
JBloves27

👍 :D

Profile icon
TsunamiOrSumth

wowsies, just one thing, how to firebase?

Profile icon
Zavexeon

@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.