diff --git a/README.md b/README.md index 4601417..57ce16e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Key features - [Getting started](#getting-started) + [Installation](#installation) + [Creating bot instance](#creating-bot-instance) +- [Bot instance](#bot-instance) - [Commands](#commands) + [All parameters](#all-parameters) + [Command function](#command-function) @@ -21,9 +22,11 @@ Key features + [Command examples](#command-examples) * [Simple reply](#simple-reply) * [Example with arguments](#example-with-arguments) +- [Events](#events) - [Customization and built-in messages](#customization-and-built-in-messages) + [Help message](#help-message) + [System messages](#system-messages) +- [Complete example](#complete-example) ## Getting started ### Installation @@ -60,6 +63,14 @@ bot.start(); Optional arguments for this method - **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 +## 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 > [IMPORTANT] > All commands have to be declared above the *start* method @@ -166,6 +177,12 @@ test test @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 All configuration parameters for messages are stored in the *messages* property (example: bot.messages) ### Help message @@ -188,4 +205,45 @@ Each message can be customized using these properties - **bottomText** - *string* - text shown below the title - **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 -- **footer** - *string* - footer of the embedded content \ No newline at end of file +- **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'); +``` \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2aa4ee7..ce3a9a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discordcommands", - "version": "1.0.0-alpha", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "discordcommands", - "version": "1.0.0-alpha", + "version": "1.0.0", "license": "MIT", "dependencies": { "discord.js": "^12.5.3" diff --git a/package.json b/package.json index 5c7ca43..70fecbb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discordcommands", - "version": "1.0.0-alpha", + "version": "1.0.0", "description": "A wrapper that helps you create your own Discord bot easier.", "type": "module", "main": "dist/index.js", diff --git a/src/Bot.ts b/src/Bot.ts index 7ac1984..3b4de28 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -1,6 +1,6 @@ //IMPORTS 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 * as http from 'http'; import { SystemMessageManager } from './SystemMessage.js'; @@ -23,7 +23,7 @@ interface ConstructorOptions { class Bot { name: string; client: Client; - commands: CommandsManager; + commands: CommandManager; config: ConfigurationOptions; messages: { help: HelpMessageParams, @@ -37,7 +37,7 @@ class Bot { constructor(options: ConstructorOptions) { this.name = options.name; this.client = new Client(options.clientOptions); - this.commands = new CommandsManager(options.prefix, options.argumentSeparator); + this.commands = new CommandManager(options.prefix, options.argumentSeparator); this.config = { token: options.token, helpCommand: options.helpCommand != undefined ? options.helpCommand : true diff --git a/src/Command.ts b/src/Command.ts index b7894a7..a0a11b9 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -91,7 +91,7 @@ class Command { } } } -class CommandsManager { +class CommandManager { list: Command[]; prefix: string; argumentSeparator: string; @@ -219,4 +219,4 @@ class CommandsManager { } //EXPORTS -export { Command, CommandsManager, CommandMessageStructure, PermissionsError } \ No newline at end of file +export { Command, CommandManager, CommandMessageStructure, PermissionsError } \ No newline at end of file diff --git a/src/Help.ts b/src/Help.ts index 8296317..f847a45 100644 --- a/src/Help.ts +++ b/src/Help.ts @@ -1,5 +1,5 @@ import { ColorResolvable, MessageEmbed } from 'discord.js'; -import { Command, CommandsManager } from './Command.js'; +import { Command, CommandManager } from './Command.js'; interface HelpMessageParams { title: string, @@ -10,7 +10,7 @@ interface HelpMessageParams { } class HelpMessage extends Command { - constructor(cmdManager: CommandsManager, params: HelpMessageParams, botName?: string) { + constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) { super({ name: 'help', usage: params.usage,