Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jet-8401/discord-command.js
An npm package that help you with command for your discord bot.
https://github.com/jet-8401/discord-command.js
bot commands discord discord-js javascript
Last synced: 23 days ago
JSON representation
An npm package that help you with command for your discord bot.
- Host: GitHub
- URL: https://github.com/jet-8401/discord-command.js
- Owner: Jet-8401
- License: apache-2.0
- Created: 2021-04-16T19:36:20.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-29T11:19:26.000Z (almost 2 years ago)
- Last Synced: 2024-12-20T20:20:26.344Z (about 2 months ago)
- Topics: bot, commands, discord, discord-js, javascript
- Language: JavaScript
- Homepage:
- Size: 213 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# discord-command.js
![]()
![]()
![]()
This package is a command manager for your discord bot developped for [Discord.js](https://discord.js.org/).
## Basics
### Installation: `npm install discord-command.js`
Get the handler & the command constructor
```javascript
const { handler, command } = require("discord-command.js");
```_`Note: handler is set to a global variable`_
### 1. Load a command
---```javascript
handler.register(resolvable, options);
```_`If the resolvable is a path to a folder it gonna read every other folder in it that doesn't match the filter`_
- resolvable can be:
- a path to a file/folder (relative or absolute) (1)
- an object that fit the command constructor
- a command
- options:
- **auto_categorise** : gonna set the category
of the command the same name as its parent folder
- **recursive** : if the categories is set recursivly to the
childrens of the current command
- **filter** : callback function that gonna exclude the expected values
(1) All file must return a command.
### 2. How to create a command
---Using the command constructor.
```javascript
const myCommand = new command(entries, executable, options);
```Or by create a command object.
```javascript
const myCommand = {
"entries", // obligatory
"executable", // obligatory
"options" // optional
}
```For creating a command you must have 3 keys
- entries : string|string[] `(the names of the command)`
- executable : function `(a callback function that gonna be the executable for this command (the arguments are given through destructuration))`
- options: object|null `(the options for this command)`
---
#### `executable : `function({ channel, message, content, interaction, resolvable, content, args, bot, command })| Parameter | Type | Description |
|---|:---:|---|
| channel | `Disord.TextBasedChannels` | The channel where the command has been invoked |
| message | `Discord.Message` | The message that triggered the command |
| content | `string` | the content of the message (without the command) |
| interaction | `Discord.Interaction` | The interaction that triggered the command |
| resolvable | `Discord.Message` or `Discord.Interaction` | Interaction or a Message depends on wich has triggered the command |
| content | `string` | The content of the message without the command |
| args | `string[]` | The given arguments |
| bot | `Discord.Client` | The client of the bot (can be undefined if the bot was not defined before) |
| command | `command` | The current command (except on static commands) |
###
---#### `options : ` { description, categories, childrens, interactionsTypes, interactionsOnly, timeout, onTimeout, }
| Parameter | Type | Description |
|---|:---:|---|
| description | `string` | The description of the command |
| categories | `string[]` | The categories of the command |
| childrens | `command[]` | The childrens/subcommands of the command |
| interactionsTypes | `string[]` | The types of interaction that could triggered this command |
| interactionOnly | `boolean` | If the the command can be triggered only with interactions |
| timeout | `number` | the waiting time (in ms) before executing an other time this command |
| onTimeout | `command.executable` | the function that gonna be executed if the command been time out |
| universalTimeout | `boolean` | if the timeout applied through any discord guilds/servers |### Examples of the same command
---
```javascript
module.exports = {
entries: "ping",executable: function pingFunction({resolvable}) {
resolvable.reply("Pong !");
},options: {
interactionsTypes: ["APPLICATION_COMMAND"]
}
}
``````javascript
const { command } = require("discord-command.js");const ping = new command(
"ping",function pingFunction({resolvable}) {
resolvable.reply("Pong !");
},{ interactionsTypes: ["APPLICATION_COMMAND"] }
);module.exports = ping;
```### 3. Configure the handler
---#### Set the bot
First of all you need to specify the client on wich your bot run on by doing
```javascript
handler.cache.set('client', /* Your client here */);
```
You need to specify that in case you want to get your client in an executable of a command or if you want to register slash commands.
#### Set any parameter of the configuration
The `handler` have a `configuration` variable with two function (`get & set`).
In that [folder](./src/configuration.json) you will see multiple variables that the `handler` use, you can personalise them with the `set` function like so
```javascript
handler.configuration.set("prefix", "!");
```
You must enter the key and the value that you want to applied to it `(the value must be the same type as the one before)`.
#### Set a default timeout
```javascript
handler.cache.set("default_ontimeout", onTimeout);
```
### 4. Execute the commands
---#### Using a global function
You can use a global function that works for `Discord.Message` and `Discord.Interaction`
```javascript
Bot.on("messageCreate", message => handler.resolve(message));
Bot.on("interactionCreate", interaction => handler.resolve(interaction));
```
#### Using separated functions
You can using different functions too
```javascript
Bot.on("messageCreate", message => handler.executeMessage(message));
Bot.on("interactionCreate", interaction => handler.executeInteraction(interaction));
```
#### Execute a command without an Interaction or Message
```javascript
const ping = handler.hasCommand("ping");ping.executable(); // executable is the main function of the command
```
_`Note : we don't give any arguments to the executable so we have to make sure that the command don't ask for them.`_
## Advanced
### 1. Statics commands
---Static commands are some commands that affect a certain category of command _`(like the default one)`_ and act almost like a child of theme, that's mean that the command can be called like it was a child but with a specific prefix _`("--" by default)`_.
Like for example if i create a command info that gonna tell me every utils informations of the previous/parent command i would call it like that : `-ping --info`, the utility of that is that in the info executable the `command` argument gonna point into the previous command _`(in that case "ping")`_. The other difference between a static command and a children is that the static don't gonna be a part of the command but a part of the category.#### Examples
```javascript
const infoCommand = {
entries: 'info',
executable: ({message, command}) => {
message.reply(`The categories of ${command.entries[0]} is ${command.categories.join('-')}`);
}
}handler.staticCommands.add(infoCommand);
```If i type `-ping --info` into the chat and ping have `other` and `utils` into its categories the bot would reply to me with that message : `The categories of ping is utils-other`
#### How to use
To add a command you can use `add` function in `handler.staticCommands.add(`obj`, `categories`)`
Function description
| Parameter | Type | Description |
|---|:---:|---|
| obj | `obj` or `command` | A command or an object that fit the [command constructor](#basics-chapter2) |
| categories | `string[]` or `null` | All categories that will be affected by that command
_`Note : if categories is not defined the command gonna go into the default category (will affect every categories)`_
### 2. Voice handler
---The voice handler add a queu and a bunch of functions and properties per guild to help through voice connection.
#### How to use
First you need to get the queu from a guildId by using
```javascript
handler.voice.get(guildId);
```Function Description
`Get the queu of of the current guild and if the queu is not set,`
`create it and attribute it.`
#### What is a queu ?
A `queu` is an object to manipulates voices between guilds more easily._Methods and properties of a `queu`_
| Property | Type | Description |
|---|:---:|---|
| content | `Array` | The content of the current queu |
| maxSize | `number` | The maximum size of the queu |
| currentlyPlaying | `boolean` | If the queu is playing an audio ressource |
| lastSong | `any` | The last song the queu has played |
| isLoop | `boolean` | If the queu is in a loop |
| connection | `false` or `Voice.VoiceConnection` | False is the connection to the voice channel is not yet created |
| audioPlayer | `Voice.AudioPlayer` | The player that gonna play the song into the channel |
| Methods | Arguments | Description |
|---|:---:|---|
| add | `item: any, force: boolean` | Add something to the queu. `force` param is to enabled if you want to 'break' the maximum size of the queu, that means that if the length of the curernt queu is too long to add something eles the first element on the queu gonna be deleted and the item gonna be pushed into the end. By default the maximum size is set to 100 and can be change into `handler.voice.maxSize` |
| play | `ressource: Voice.AudioRessource, voiceChannel: Discord.VoiceChannel/null` | Make the bot play a ressource into a voice channel |
| createVoiceConnection | `voiceChannel: Discord.VoiceChannel, options: {selfMute, selfDeaf, group, debug}` | Create a voice connection to a voice channel |
| hasVoiceConnection | | Check if the queu have a voice connection |
| regenerateAudioPlayer | `options: Voice.CreateAudioPlayerOptions/undefined` | Regenerate the audioPlayer of the queu |
| next | | Return the incoming item into the queu |
| getContent | `index: number` | Get the content at the given index into the queu return `boolean` if the index return something |
### 3. White/Black lists
---The `handler` have a white and black list, the white list is here to take the lead on the black.
That's mean that it gonna check the white list before the black for example if you put `@everyone` on the black list but you put your tag into the white
list you will be able to triggered a command.
In both list you can add **tags** `(Rothoven#4388)`, **roles** `(@everyone | moderator)` and **id** `(775453112064147516)`.```javascript
handler.autorised.addWhiteList(devs);
handler.autorised.addBlackList('@everyone');
handler.autorised.enabled = true;
```
That will allowed the people that are in `devs` but not everybody else.
### 4. Registering slash commands
---*For now this feature is really small and basic but it will be updated into the v4.0.0.*
So for registering some slash command you will need to use `handler.registerCommandsApplication(`commands`,` guilds`)` after login the bot. You can see here that you can specify the guilds where the commands gonna be registered. *(For thoses who wonders, the slash commands gonna be registered only if the command can be triggered by interaction)*
```javascript
Bot.login(/* your token */).then(() => {
handler.registerCommandsApplication(handler.commands)
});
```*Function declaration*
| Arguments | Types | Descritpion |
|---|:---:|---|
| commands | `Array` | `None` |
| guilds | `Array` | The ids of the guilds |
### 5. Handler properties and methods.
---
| Properties | Type | Description |
|---|:---:|---|
| command | `command` | The command constructor |
| staticCommands | `object` | The instance for statics commands |
| configuration | `object` | The configuration of the handler |
| autorised | `object` | The instance for black/white list |
| cache | `object` | The cache of the handler |
| voice | `object` | The instance for voice connections |
| commands | `Array` | The array that contains every commands |
| Methods | Arguments | Description |
|---|:---:|---|
| register | resolvable: `string / command`, options: `{ auto_categorise: boolean, recursive: boolean, filter: (name: string, path: string, isDirectory: boolean) => {} } / undefined` | Register a command to the handler |
| executeInteraction | interaction: `Discord.Interaction`, options: `{ client: Discord.Client } / undefined` | Execute a specified command by the base of an interaction |
| executeMessage | message: `Discord.Message`, options: `{ commandName: string, client: Discord.Client } / undefined` | Execute the specified command by the base of a message |
| hasCommand | command: `string / Array / Array>`, options: `{ strict: boolean, strictEntries: boolean, filter: (value, index: number, array: Array) => {} } / undefined` | Check if the handler has specific command |
| unload | command: `string / command` | Unlaod a command that was previously register. Can use `*` to unload all the commands. |
| resolve | resolvable: `Discord.Message / Discord.Interaction`, options: `{ client: Discord.Client } / undefined` | Resolve a Discord.Message or a Discord.Interaction |
| parse | message: `Discord.Message` | Parse a message or an instance of and split it into three (command, content, arguments) |