Discord.js v13 Command Handler (Slash Cmds and Legacy Cmds)
Tags: discord, javascript, djs, bot
Published: 2022-01-03 09:26:42

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](https://discord.com/developers/applications)
![Image Of Discord Developers Portal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv07rtrb0c4binhr9flm.png) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ofkdcrrenpl3v41j1py.png) 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

```bash
https://github.com/edwinsanjo/discord.js-v13-command-handler.git 
cd discord.js-v13-command-handler
```
after that type 
```
npm i
```
This command installs all the required dependencies after that open the directory with your favorite IDE mine is vscode

## Editing The Config File

```js
  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
  }
```
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](https://www.mongodb.com/atlas/database) 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

```js
// 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!")
      }
}
```
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

```js
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with pong'),
    async execute(interaction) {
        interaction.reply("Pong!")
    }
};
```
Documentation is available on [discord.js guide](https://discordjs.guide/) website 

## Starting The Bot

there are 2 scripts
```bash
npm start
```
and 
```bash
npm run dev
```
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](https://railway.app/)

first create account on railways.app then go to [railway.app/starters](https://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.