Files
discordeno/site/docs/nodejs/CommandHandler/command-manager.md
meister03 b7b2120a6f docs(site, template): add Nodejs Bot Guide (#1998)
* Inital Docs

* Finish Design

* Finish Design

* Add FILE Structure'

* Add Not Ready Template

* Update Template

* Add Command Manager and first Command ping

* Add Command Manager and first Command ping

* Add Embeds Guide & EventManager

* INITIAL FINISH: GUIDE

* FIX TYPOS, LTS's Review, sidebar_position

* Update site/docs/nodejs/getting-started.md

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Fixed Position? Add some comments on template, add changes in review

* Add Interaction Handling, Modals

* format files

* Update site/docs/nodejs/getting-started.md

1

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Update site/docs/nodejs/CommandHandler/command-manager.md

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Fix Review things

* itoh's suggestions

The guide looks very very veery good overall I like it. This commit adds my personal style of writing.
Please review it 🙏.

PS: I hope these are not too many changes .-.

* deno fmt

Co-authored-by: meister03 <root@_HOSTNAME_>
Co-authored-by: ITOH <to@itoh.at>
Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
2022-02-15 13:54:05 +00:00

2.4 KiB

sidebar_position
sidebar_position
2

Command Manager

Currently, you probably have something like this in your code:

const Discord = require("discordeno");
// Ideally you should move to an `.env` file
const config = require("./config.json");

const client = Discord.createBot({
  events: {
    messageCreate(client, message) {
      if (message.content === "!ping") {
        client.helpers.sendMessage(message.channelId, { content: "pong" });
      }
    },
  },
  intents: ["Guilds", "GuildMessages"],
  token: config.token,
});

Discord.startBot(client);

Of course, if you add more and more commands and as your code base grows, you can lose track very quickly.

To avoid this, it is recommended to store the commands in separate folders divided into different categories.

Previously, we introduced you to our plugin structure, which has a lot of advantages.

├Plugins/
├── General/
│   ├── commands/
│   │   ├── ping.js
│   │   └── ...
├── Developer/
│   ├── commands/
│   │   ├── eval.js
│   │   └── ...
└── ...

Get this file from the nodejs template

const CommandManager = require("./template/Managers/CommandManager.js");
const manager = new CommandManager({});
manager.load({ plugin: true }); // Load the commands
client.commands = manager;

client.commands.cache.get("ping"); // Get the `ping` command

The Manager will automatically iterate through all files in the folder and then load them into the cache property, which is mapped on the command name.

Take a look at Create Command to learn how to create a command.

Handle Command

The manager also contains a handler for executing the command when a message is received.

:::important

Currently checks for permissions, cooldowns, and rate limits are not covered, but these will be added soon.

:::

Message Create Event:

module.exports = async (client, message) => {
  client.commands.isCommand(message);
};

Interaction Create Event:

module.exports = async (client, interaction) => {
  client.commands.isInteraction(interaction);
};

You can also customize the isCommand function to your use case.