An awesome Discord.js Bot Template to jumpstart your development!
Report Bug
·
Request Feature
Table of Contents
This is a Discord.js bot template written in node.js for easy discord bot development.
Here's why this template stands out:
- Custom errors and warnings logging system
- Quick and easy slash command creation (With tons of configurable options)
- Codebase is easily expandable to suit your needs
Below shows how you can set up the project locally.
Make sure you have the following software installed
- npm
npm install npm@latest -g
- NodeJS v16.6+
This shows how you can use the template.
- Create an application at https://discord.com/developers/applications
- Create a bot within the application and copy the bot token
- Click
OAuth2
>URL Generator
- Check
applications.commands
&bot
underScopes
- Check whichever permissions your bot will require under
Bot Permissions
- Copy the
Generated URL
and invite the bot to a server - Clone the repo
git clone https://github.com/TheTrustyPwo/discordjs-bot-template.git
- Install NPM packages
npm install
- Navigate to
config.js
and set the bot token, bot admin IDs and the test guild IDmodule.exports = { BOT_TOKEN: "CHANGE THIS", ADMIN_IDS: ["CHANGE THIS",], // Bot admin ID's INTERACTIONS: { SLASH: true, // Should the interactions be enabled CONTEXT: true, // Should contexts be enabled GLOBAL: false, // Should the interactions be registered globally TEST_GUILD_ID: "CHANGE THIS", // Guild ID where the interactions should be registered. [** Test you commands here first **] }, /* Bot Embed Colors */ EMBED_COLORS: { DEFAULT: "#FF8C00", SUCCESS: "#00FF00", ERROR: "#D61A3C", WARNING: "#F7E919", }, };
- Start the bot
node index.js
For this tutorial, we will be creating a simple /say <message>
command which will echo the message specified by the user
- Navigate to
src/commmands
- Create a directory which will be the command category (For this tutorial, we'll be using
utility
) - Create a new Javascript file under the directory, name of the file will be the command name
- Copy this code into the file that you have just created
const { CommandInteraction } = require("discord.js");
const { Command } = require("../../structures");
module.exports = class SayCommand extends Command {
constructor(client) {
super(client, {
name: "say", // Name of the command (Must be in lowercase)
description: "Echoes the message", // Slash command description
category: "UTILITY", // Command category; Must be defined in src/Structures/CommandCategory.js
enabled: true,
options: [ // Basically the command parameters
{
name: "message", // Name & Identifier
description: "The message to echo", // Description
type: "STRING", // Type,
// {USER|ROLE|NUMBER|STRING|BOOLEAN|INTEGER|CHANNEL|MENTIONABLE|SUB_COMMAND|SUB_COMMAND_GROUP}
// https://discordjs.guide/interactions/slash-commands.html#option-types
required: true, // If false, it does not need to be specified in order for the command to run
},
],
});
}
/**
* @param {CommandInteraction} interaction
*/
async interactionRun(interaction) {
}
}
The above code basically sets up the command framework for our /say <message>
command. The interactionRun
is the function that will run when the command is triggered, so we will be adding code to handle the command in the next step.
5. Add the following code under the interactionRun
function
const message = interaction.options.getString("message")// Gets the value from the 'message' option
await interaction.followUp(message); // Follows up the interaction response with the message, essentially echoing it
- Restart the bot and your command should be registered successfully!
For this tutorial, we will be listening to the guildMemberAdd
event and automatically give the user a role when they join the server.
- Navigate to
src/events
- Create a directory named
member
and within the folder, create a javascript file namedguildMemberAdd
(File name must be exactly the same as the event name) - Add the following code into the file
const autoRole = "YOUR AUTOROLE ID"; // AutoRole ID
/**
* @param {import("../../structures").BotClient} client
* @param {import("discord.js").GuildMember} member
*/
module.exports = async (client, member) => {
if (!member || !member.guild) return; // Undefined member or guild
const { guild } = member; // Get the guild
const role = guild.roles.cache.get(settings.autorole); // Getting the role by ID
if (role) member.roles.add(role).catch((err) => { // Give the role to the member
// Catch the error, if any
});
}
- Restart the bot and the event should be successfully registered!
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
TheTrustyPwo - Pwo#0001 - thetrustypwo@gmail.com
Project Link: https://github.com/TheTrustyPwo/discordjs-bot-template