Security, Tech & Programming
Discord.js Bot Starter Template

Discord.js Bot Starter Template

This code will run a basic discord bot that you can split into commands and then keep everything organized in modules.

Files:

  • package.json
  • index.js
  • .env
  • src/commands/index.js
  • src/commands/ping.js (a sample command)

You can add new commands in src/commands/ folder by copying the sample.js file and modifying it to suit your module. Then you have to edit the src/commands/index.js file to include that new command. That’s it.

Install dependencies

Don’t forget to run npm install to load dependencies.

Preloaded dependencies are:

  • dotenv

Files with code:

package.json

{
  "name": "discord-starter",
  "version": "1.0.0",
  "description": "discord bot starter bootstrap",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "socalledhacker",
  "license": "UNLICENSED",
  "dependencies": {
    "dotenv": "^16.0.0",
  }
}

.env

Change the value of token to the one that you got from discord developer section for your bot.

Also change the trigger to anything that you want to use

DISCORD_TOKEN=ABCDEFGHIJKLBLABLABLA
TRIGGER=!mytrigger

index.js

You might want to change the intents: value based on your bot intent. Should work as it is for basic bots.

// load the .env file
import "dotenv/config";
// load discord.js
import { Client, Intents, Collection } from "discord.js";
// import custom commands
import * as botCommands from "./src/commands/index.js";

// for bot commands
const trigger = process.env.TRIGGER;
const trigger_length = trigger.length;

// create discord client // Bot instance
const Bot = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
// login to discord server
Bot.login(process.env.DISCORD_TOKEN);
// console.log when successfully logged in
Bot.on("ready", () => {
  console.log(`Logged in as ${Bot.user.tag}!`);
});

// setup bot commands
Bot.commands = new Collection();
Object.keys(botCommands).forEach((key) => {
  Bot.commands.set(botCommands[key].name, botCommands[key]);
});

// handle messages from channel
Bot.on("messageCreate", async (message) => {
  if (!message.content.startsWith(trigger)) return;
  const args = message.content.split(/ +/);
  // get the first word (lowercase) and remove the prefix
  let command = args.shift().toLowerCase().slice(trigger_length);

  if (!Bot.commands.has(command)) return;

  // removes the command trigger only
  // let commandString = args.slice(1).join(" ")
  let commandString = args.join(" ");

  try {
    Bot.commands.get(command)({ Bot, commandString, message });
  } catch (error) {
    console.log(error);
    message.reply("there was an error trying to execute that command!");
  }
});

src/commands/index.js

You will include all new commands to load in this file too. To not load a command module, simply remove it from here.

import ping from './ping.js'
export {
    ping,
}

src/commands/ping.js

export default function ping({Bot, commandString, message}) {
    const delay = Date.now() - message.createdAt
    message.reply(`**pong** *(delay: ${delay}ms)*`)
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Hire Me!