Contributors Forks Stargazers Issues MIT License

Discord.js Bot Template

An awesome Discord.js Bot Template to jumpstart your development! Report Bug ยท Request Feature

Table of Contents
  1. About The Project

  2. Getting Started

  3. Usage
  4. Contributing
  5. License
  6. Contact

About The Project

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

(back to top)

Built With

(back to top)

Getting Started

Below shows how you can set up the project locally.

Prerequisites

Make sure you have the following software installed

Installation

This shows how you can use the template.

  1. Create an application at https://discord.com/developers/applications
  2. Create a bot within the application and copy the bot token
  3. Click OAuth2 > URL Generator
  4. Check applications.commands & bot under Scopes
  5. Check whichever permissions your bot will require under Bot Permissions
  6. Copy the Generated URL and invite the bot to a server
  7. Clone the repo
    git clone https://github.com/TheTrustyPwo/discordjs-bot-template.git
  8. Install NPM packages
    npm install
  9. Navigate to config.js and set the bot token, bot admin IDs and the test guild ID

     module.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",
         },
     };
  10. Start the bot
    node index.js

(back to top)

Usage

Custom Slash Commands

For this tutorial, we will be creating a simple /say <message> command which will echo the message specified by the user

  1. Navigate to src/commmands
  2. Create a directory which will be the command category (For this tutorial, we’ll be using utility)
  3. Create a new Javascript file under the directory, name of the file will be the command name
  4. 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
  1. Restart the bot and your command should be registered successfully!

Registering Events

For this tutorial, we will be listening to the guildMemberAdd event and automatically give the user a role when they join the server.

  1. Navigate to src/events
  2. Create a directory named member and within the folder, create a javascript file named guildMemberAdd (File name must be exactly the same as the event name)
  3. 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
    });
}
  1. Restart the bot and the event should be successfully registered!

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

TheTrustyPwo – Pwo#0001 – [email protected]

Project Link: https://github.com/TheTrustyPwo/discordjs-bot-template

(back to top)

GitHub

View Github