8.2 KiB
CommandBot
A Discord.js based framework that makes creating Discord bots with support for slash commands easy and fast.
Features
- Support for slash commands, context menu interactions
- Intuitive usage
- Flexible
- Good customization
- Permission system
- Built-in help command
- Built-in information messages
- Automatic error catching
- Large documentation in README and using JSDoc
- Written in TypeScript
Documentation
Reference for available objects and structures is available here. All descriptions can also be accessed from your code editor/IDE. TypeScript declaration files are also included (.d.ts) for some programs (like Visual Studio Code) to give autocompletion suggestions.
Table of contents
Installation
System requirements
- Node.js 16.6.0 or newer
- npm or yarn package manager
Creating a project
Registering Discord app
- Visit Discord Developer Portal and create an app
- Navigate to the Bot section and register a bot
- Navigate to OAuth 2 section, select bot and application.commands scopes and check bot permissions
- Copy the link, paste it in your web browser and add the bot to a server
Creating application
- Create empty directory
- Run
npm init -yoryarn init -y - Add the CommandBot package
// npm
npm install commandbot@latest
// yarn
yarn add commandbot@latest
- Create index.js/index.ts file (TypeScript is a recommended language)
- Import the CommandBot package
// CommonJS
const { Bot } = require("commandbot");
// ES Modules (to use ESM add "type": "module" to package.json file)
import { Bot } from "commandbot";
- Initialize the bot instance (InitOptions) (list of available intents here)
const bot = new Bot({
name: "YOUR_BOT_NAME",
globalPrefix: "!",
argumentSeparator: ",",
commandSeparator: "/",
clientOptions: {
intents: [..."DISCORD_API_INTENTS"],
},
token: "DISCORD_BOT_TOKEN",
applicationId: "APPLICATION_ID",
help: {
enabled: true,
title: "List of commands",
usage: "[command name]",
description: "Show all commands available to use in my fantastic Discord bot.",
bottomText: "Hello World",
color: "#0055ff",
visible: false,
},
});
- Create and add commands to the Bot instance (see Commands)
- Start your bot
bot.start(
port, // If passed, the application will create a HTTP server [type: number (integer)]
true // If true or undefined, the app will register all slash commands in the Discord API [type: boolean]
);
WARNING! All commands have to be added to the instance before starting the bot. Adding commands while the bot is running is not possible and can cause issues.
Commands
Creating a command
To create a command, use CommandManager.prototype.add method
Command types
- CHAT - message interactions using command prefixes or slash commands
- USER - right-click context menu interactions on users
- MESSAGE - right-click context menu interactions on messages
Chat command example:
bot.commands.add("CHAT", {
name: "greet",
parameters: [
{
name: "user",
description: "User to greet",
optional: false,
type: "user",
},
],
aliases: ["hello"],
description: "Welcome someone to your server",
usage: "[user]",
dm: false,
guilds: ["123456789874561230"],
slash: true,
visible: true,
permissions: {
resolvable: ["ADMINISTRATOR"],
checkType: "ANY",
},
function: async (i) => {
const user = await i.get("user").toObject();
return `Hello ${user.toString()}`;
},
});
Command function schema is defined here
Parameters
Types
To get an entity ID from ObjectID use the id property. You can also call toObject method to retrieve full entity object from Discord API
Defining
Pass a list of ParameterSchema objects to parameters property
Example parameter object:
{
name: "user",
description: "User to mention",
optional: false,
type: "user"
}
Reading input value
To read parameter values use InputManager.prototype.get() (passed in the first argument of a command function)
Example:
function: (i) => {
const userObj = i.get('member', 'user')
if(userObj) {
const user = userObj.toObject();
if(user) {
return `Hello, ${user.toString()}`
}
else {
throw new Error('User not found')
}
}
}
Events
CommandBot is using EventEmitter that is built into Node.js. You can listen to events using the on method. Event types are available here.
Messages
System messages
System messages can be composed and sent automatically when a certain action happens. All message types are available here. Their configuration is stored in Bot.prototype.messages property. Each of these messages can be customized with SystemMessageAppearance objects. There is also a global deleteTimeout property so messages can automatically be deleted after a given time.
Help message
To configure it use the help property in the bot's constructor and pass there a HelpMessageParams object.
WARNING! You can't customize these messages after starting the bot. Changing these properties while the bot is running will have no effect.
Issues
Since this package is created by only 1 person it may contain some bugs or behave weirdly. If you notice any problem, typo etc, please create an issue in the Issues tab on GitHub.
Thank you.
Created with ❤️ by GRZANA