1.0.0 -> CommandsManager to CommandManager, finalizing documentation

This commit is contained in:
mmacierzanka committed 2021-06-24 02:13:08 +02:00
1 parent 8a7ac598a6
commit a9d5914f10
6 files changed
+69 -11

No files matched your search

+59 -1
View File
@@ -12,6 +12,7 @@ Key features
- [Getting started](#getting-started) - [Getting started](#getting-started)
+ [Installation](#installation) + [Installation](#installation)
+ [Creating bot instance](#creating-bot-instance) + [Creating bot instance](#creating-bot-instance)
- [Bot instance](#bot-instance)
- [Commands](#commands) - [Commands](#commands)
+ [All parameters](#all-parameters) + [All parameters](#all-parameters)
+ [Command function](#command-function) + [Command function](#command-function)
@@ -21,9 +22,11 @@ Key features
+ [Command examples](#command-examples) + [Command examples](#command-examples)
* [Simple reply](#simple-reply) * [Simple reply](#simple-reply)
* [Example with arguments](#example-with-arguments) * [Example with arguments](#example-with-arguments)
- [Events](#events)
- [Customization and built-in messages](#customization-and-built-in-messages) - [Customization and built-in messages](#customization-and-built-in-messages)
+ [Help message](#help-message) + [Help message](#help-message)
+ [System messages](#system-messages) + [System messages](#system-messages)
- [Complete example](#complete-example)
## Getting started ## Getting started
### Installation ### Installation
@@ -60,6 +63,14 @@ bot.start();
Optional arguments for this method Optional arguments for this method
- **port** - *number* - if specified, the app will create a http server that will be listening on the specified port - **port** - *number* - if specified, the app will create a http server that will be listening on the specified port
- **token** - *string* - app token from Discord Developer Portal - **token** - *string* - app token from Discord Developer Portal
## Bot instance
The main object of this library has the following properties
- **name** - *string* - bot's name specified in the constructor
- **client** - *[Client](https://discord.js.org/#/docs/main/stable/class/Client)* - Discord.js client instance
- **commands** - *CommandManager* - base of the command system (see [Commands](#commands))
- **config** - *object* - object that stores the login token and *helpMessage* value from the constructor
- **messages** - *SystemMessageManager* - object instance containing all configuration parameters for built-in messages (see [Customization and built-in messages](#customization-and-built-in-messages))
- **on** - *object* - object containing function that trigger on specified events (see [Events](#events))
## Commands ## Commands
> [IMPORTANT] > [IMPORTANT]
> All commands have to be declared above the *start* method > All commands have to be declared above the *start* method
@@ -166,6 +177,12 @@ test
test test
@caller, Repeated "test" 5 times @caller, Repeated "test" 5 times
``` ```
## Events
There are 2 events defined in the *on* property
- **message** - *Function* - a function that will trigger for every message sent in any text channel (a [Message](https://discord.js.org/#/docs/main/stable/class/Message) object is passed to the first argument)
- **command** - *Function* - a function that will trigger for every message that gets recognized as a command (a [Message](https://discord.js.org/#/docs/main/stable/class/Message) object is passed to the first argument and a CommandMessageStructure object to the second argument)
> [IMPORTANT]
> Do not use client.on('message') event handler! This handler is a core part of the command system.
## Customization and built-in messages ## Customization and built-in messages
All configuration parameters for messages are stored in the *messages* property (example: bot.messages) All configuration parameters for messages are stored in the *messages* property (example: bot.messages)
### Help message ### Help message
@@ -188,4 +205,45 @@ Each message can be customized using these properties
- **bottomText** - *string* - text shown below the title - **bottomText** - *string* - text shown below the title
- **accentColor** - *[ColorResolvable](https://discord.js.org/#/docs/main/stable/typedef/ColorResolvable)* - color of the embedded content - **accentColor** - *[ColorResolvable](https://discord.js.org/#/docs/main/stable/typedef/ColorResolvable)* - color of the embedded content
- **showTimestamp** - *boolean* - show time and date at the bottom of the embedded content - **showTimestamp** - *boolean* - show time and date at the bottom of the embedded content
- **footer** - *string* - footer of the embedded content - **footer** - *string* - footer of the embedded content
## Complete example
```javascript
import { Bot, Command } from 'discordcommands';
const bot = new Bot({
name: "CommandBot",
prefix: "!"
});
bot.messages.help.accentColor = '#ff0000';
bot.messages.system.NOT_FOUND.accentColor = "BLUE";
//Command triggers: !ping, !pong, pingpong
bot.commands.add(new Command({
name: 'ping',
aliases: 'pong',
keywords: 'pingpong',
function: (m, a) => {
return 'Bot ping: ' + bot.client.ws.ping + 'ms';
}
}));
bot.commands.add(new Command({
name: "repeat",
function: async (m, a) => {
if(a[0] && a[1]) {
const count = parseInt(a[1]);
if(!isNaN(count)) {
for(let i = 0; i < count; i++) {
await m.channel.send(a[0]);
}
return `Repeated "${a[0]}" ${count} times`;
}
else {
throw new Error('Number of messages is incorrect');
}
}
}
}));
bot.start(3000, 'TOKEN');
```
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "discordcommands", "name": "discordcommands",
"version": "1.0.0-alpha", "version": "1.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "discordcommands", "name": "discordcommands",
"version": "1.0.0-alpha", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"discord.js": "^12.5.3" "discord.js": "^12.5.3"
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "discordcommands", "name": "discordcommands",
"version": "1.0.0-alpha", "version": "1.0.0",
"description": "A wrapper that helps you create your own Discord bot easier.", "description": "A wrapper that helps you create your own Discord bot easier.",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
+3 -3
View File
@@ -1,6 +1,6 @@
//IMPORTS //IMPORTS
import { Client, ClientOptions, Message } from 'discord.js'; import { Client, ClientOptions, Message } from 'discord.js';
import { Command, CommandsManager, CommandMessageStructure, PermissionsError } from './Command.js'; import { Command, CommandManager, CommandMessageStructure, PermissionsError } from './Command.js';
import { HelpMessage, HelpMessageParams } from './Help.js'; import { HelpMessage, HelpMessageParams } from './Help.js';
import * as http from 'http'; import * as http from 'http';
import { SystemMessageManager } from './SystemMessage.js'; import { SystemMessageManager } from './SystemMessage.js';
@@ -23,7 +23,7 @@ interface ConstructorOptions {
class Bot { class Bot {
name: string; name: string;
client: Client; client: Client;
commands: CommandsManager; commands: CommandManager;
config: ConfigurationOptions; config: ConfigurationOptions;
messages: { messages: {
help: HelpMessageParams, help: HelpMessageParams,
@@ -37,7 +37,7 @@ class Bot {
constructor(options: ConstructorOptions) { constructor(options: ConstructorOptions) {
this.name = options.name; this.name = options.name;
this.client = new Client(options.clientOptions); this.client = new Client(options.clientOptions);
this.commands = new CommandsManager(options.prefix, options.argumentSeparator); this.commands = new CommandManager(options.prefix, options.argumentSeparator);
this.config = { this.config = {
token: options.token, token: options.token,
helpCommand: options.helpCommand != undefined ? options.helpCommand : true helpCommand: options.helpCommand != undefined ? options.helpCommand : true
+2 -2
View File
@@ -91,7 +91,7 @@ class Command {
} }
} }
} }
class CommandsManager { class CommandManager {
list: Command[]; list: Command[];
prefix: string; prefix: string;
argumentSeparator: string; argumentSeparator: string;
@@ -219,4 +219,4 @@ class CommandsManager {
} }
//EXPORTS //EXPORTS
export { Command, CommandsManager, CommandMessageStructure, PermissionsError } export { Command, CommandManager, CommandMessageStructure, PermissionsError }
+2 -2
View File
@@ -1,5 +1,5 @@
import { ColorResolvable, MessageEmbed } from 'discord.js'; import { ColorResolvable, MessageEmbed } from 'discord.js';
import { Command, CommandsManager } from './Command.js'; import { Command, CommandManager } from './Command.js';
interface HelpMessageParams { interface HelpMessageParams {
title: string, title: string,
@@ -10,7 +10,7 @@ interface HelpMessageParams {
} }
class HelpMessage extends Command { class HelpMessage extends Command {
constructor(cmdManager: CommandsManager, params: HelpMessageParams, botName?: string) { constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
super({ super({
name: 'help', name: 'help',
usage: params.usage, usage: params.usage,