Skip to content

guritso/slash-bot-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slash Bot Template

A different way to add and delete slash commands!

GitHub package.json dependency version (prod) Codacy branch grade GitHub

Example

Let's use the data of ping command in /src/Commands/ping.js:

module.exports = {
  data: {
    name: "ping",
    description: "ping! pong!"
  }
},
...

With this, we just need to use the /add guild command:

To remove the command we just need to use /delete:

Requirements

  • nodejs V16.9.x
  • discord.js V14.6.x

Usage

First of all let's install the dependencies, use:

npm install

Now you need to add your BOT_ID and your GUILD_ID on the file src/Configs/config.js, and for your token create a .env file, or change process.env.TOKEN by your token in config.js

then run:

node index.js

You bot will start with the first command add.

Lets use the bot, You can run the command:

/add name:delete type:guild

It will add the /delete command like we saw on Example.

Default commands

  • add
  • delete
  • avatar
  • ping

these commands are already setup in /src/Commands, you can add them.

When you want to add a new command don't miss the data: ... on module.exports, example:

module.exports = {
  data: {
    name: "say",
    description: "say something",
  },
  ...
}

Then go to discord and use

/add name:say type:guild

Or if you want to add on all guilds:

/add name:say type:global

Tips

If you want to add all commands on startup, remove the code from src/Structures/handler.js and add this one:

const { REST, Routes } = require("discord.js");

module.exports = {
  async execute(client) {
    const { TOKEN, GUILD_ID, BOT_ID } = client.config;
    const rest = new REST({ version: "10" }).setToken(TOKEN);

    const commandArray = new Array();
    const commands = client.commands;

    commands.forEach((cmd) => {
      commandArray.push(cmd.data);
    });
    // to guild
    await rest.put(Routes.applicationGuildCommands(BOT_ID, GUILD_ID), {
      body: commandArray,
    });
  },
};

You still can use add and delete commands, but if you delete, they will be readded when bot restart.

To add globally during start change this on // to guild comment.

// to global
await rest.put(Routes.applicationCommands(BOT_ID), {
      body: commandArray,
});

Caution! the add and delete commands will also be added globally!

Links

Slash Commands

License

MIT