discord-command.js

3.1.4 • Public • Published

discord-command.js

npm npm GitHub

This package is a command manager for your discord bot developped for Discord.js.



Basics

Installation: npm install discord-command.js


Get the handler & the command constructor

const { handler, command } = require("discord-command.js");

Note: handler is set to a global variable



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.



Using the command constructor.

const myCommand = new command(entries, executable, options);

Or by create a command object.

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


module.exports = {
    entries: "ping",

    executable: function pingFunction({resolvable}) {
        resolvable.reply("Pong !");
    },

    options: {
        interactionsTypes: ["APPLICATION_COMMAND"]
    }
}
const { command } = require("discord-command.js");

const ping = new command(
    "ping",

    function pingFunction({resolvable}) {
        resolvable.reply("Pong !");
    },

    { interactionsTypes: ["APPLICATION_COMMAND"] }
);

module.exports = ping;


Set the bot

First of all you need to specify the client on wich your bot run on by doing

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 you will see multiple variables that the handler use, you can personalise them with the set function like so

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

handler.cache.set("default_ontimeout", onTimeout);



Using a global function

You can use a global function that works for Discord.Message and Discord.Interaction

Bot.on("messageCreate", message => handler.resolve(message));
Bot.on("interactionCreate", interaction => handler.resolve(interaction));

Using separated functions

You can using different functions too

Bot.on("messageCreate", message => handler.executeMessage(message));
Bot.on("interactionCreate", interaction => handler.executeInteraction(interaction));

Execute a command without an Interaction or Message

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




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

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
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)



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

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<any> 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


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).

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.



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)

Bot.login(/* your token */).then(() => { 
	handler.registerCommandsApplication(handler.commands)
});

Function declaration

Arguments Types Descritpion
commands Array<command> None
guilds Array<sttring> The ids of the guilds



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<command> 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<string> / Array<Array<string>>, options: { strict: boolean, strictEntries: boolean, filter: (value, index: number, array: Array<any>) => {} } / 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)

Readme

Keywords

Package Sidebar

Install

npm i discord-command.js

Weekly Downloads

1

Version

3.1.4

License

Apache-2.0

Unpacked Size

71.6 kB

Total Files

13

Last publish

Collaborators

  • jet-8401