DEV Community

Edwin sanjo soji
Edwin sanjo soji

Posted on

Discord.js v13 Command Handler (Slash Cmds and Legacy Cmds)

this is a Discord js command Handler by me (EDWiN#2443). this supports discord.js v13 if you found any bugs or something like that just make a pull request. Maybe this will be Helpful to you.

So Lets Get Started

Discord Developers Portal

Go to Discord Developers Portal
Image Of Discord Developers Portal here you can see all of your applications if you dont have any just create one after creating go to the bot section of the application
Then Create the Application did you see the token that is very important. then now go to the oauth tab(Url Generator Tab).select bot and application.commands and in the permissons select the permissons your bot needs i suggest that you use the administrator(8)
AMAZING PORTAL use the generated link on another tab and you will be redirected to a bot invite page select the server you want to add the bot and sucess we have sucessfully invites the bot to your server

Cloning The Repo

locate to where you want the bot to be and type theese command

https://github.com/edwinsanjo/discord.js-v13-command-handler.git 
cd discord.js-v13-command-handler
Enter fullscreen mode Exit fullscreen mode

after that type

npm i
Enter fullscreen mode Exit fullscreen mode

This command installs all the required dependencies after that open the directory with your favorite IDE mine is vscode

Editing The Config File

  module.exports = {
    prefix: "-",// The Default Prefix
    mongo: "", // For Custom Prefix and Enable/Disable Commands
    testGuildIDS: "854588598526148628", // Slash Commands
    token: "" // bot token from discord develpers portal
  }
Enter fullscreen mode Exit fullscreen mode

This is the Config File
fist of all the

prefix you can use any prefix this is a required paramater or the bot will crash

mongo you wan to put the mongodb uri here you get one for free from mongodb atlas but this is not good for production but dont worry about production will be talking about thatlater. this is a required parameter

testGuildIDS this is just option if you dont add this your slash commands will be loaded as global command(For Production takes almost 1hour to register commands) if you add test guild ids then it will be loaded as dev commands (for developers and it takes less than 5mins to load)

token the secret from discord developers portal bot section this is a secret variable

Creating Commands

Legacy Commands

// COMMAND EXAMPLE
module.exports = {
    name: "ping", // the command name on discord and help [REQUIRED] (if not added, the command may not work)
    description: "Replies with pong", // the command description used on help [REQUIRED] (if not added, the command may not work)
    category: "Information", // the command category mainly used on help [REQUIRED] (if not added, the command may not work)
    aliases: ["pong"], // the command aliases [OPTIONAL]
    cooldown: 5, // the command cooldown in seconds [OPTIONAL]
    syntax: "ping", // usage/syntax example `command.name <command.prefix>` [REQUIRED] (if not added, the command may not work)
    permissions: ["ADMINISTRATOR"], // the permissons required to use this command [OPTIONAL]
    owner: true, // make the command owner only [OPTIONAL]
      run: async (client, message, args) => {
        message.channel.send("Pong!")
      }
}
Enter fullscreen mode Exit fullscreen mode

now lets check all the parameters

name think you know what this is for Required

description Currently Only Used on Help Required Or The Help Command Wont Work

aliases the Aliases in The array Will Be used as an aliease of the commad OPTIONAL

cooldown the cooldown to use a command OPTIONAL

syntax the syntax only used on help command REQUIRED

permissions All the permissons Needed for the command to be ran

owner if true the owner could only use the command

run in this function we get 3 parameters (client, message, args) the client is the variable whihc we initialized in the index file and the message is the on message parameter args is an array of all of the parameters of the command

Slash Commands

const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with pong'),
    async execute(interaction) {
        interaction.reply("Pong!")
    }
};
Enter fullscreen mode Exit fullscreen mode

Documentation is available on discord.js guide website

Starting The Bot

there are 2 scripts

npm start
Enter fullscreen mode Exit fullscreen mode

and

npm run dev
Enter fullscreen mode Exit fullscreen mode

what the script npm start does is that it tells to clear and start the bot using the command node index.js
but the npm run dev command starts the bot with nodemon (Live Refresh)this is used for development and start is used for productionif you want you can use node . no need of these scripts but scripts saves time

Hosting

Paid Hosting Is better than free but i will show you one of my favorite free host railway

first create account on railways.app then go to railway.app/starters
then select discord bot then give the details and it will make a repo puch you code to that repo and its ready

About Me

I am Edwin Sanjo Soji a Web developer And Discord bot Developer discord.js and discord.py i like to code in javascript than python i am a big fan of isro, nasa and spacex i would like to be a space scientist.

Top comments (0)