3.1.4 • Published 2 years ago

discord-command.js v3.1.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

discord-command.js

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

1. Load a command


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

2. How to create 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 })

ParameterTypeDescription
channelDisord.TextBasedChannelsThe channel where the command has been invoked
messageDiscord.MessageThe message that triggered the command
contentstringthe content of the message (without the command)
interactionDiscord.InteractionThe interaction that triggered the command
resolvableDiscord.Message or Discord.InteractionInteraction or a Message depends on wich has triggered the command
contentstringThe content of the message without the command
argsstring[]The given arguments
botDiscord.ClientThe client of the bot (can be undefined if the bot was not defined before)
commandcommandThe current command (except on static commands)


options : { description, categories, childrens, interactionsTypes, interactionsOnly, timeout, onTimeout, }

ParameterTypeDescription
descriptionstringThe description of the command
categoriesstring[]The categories of the command
childrenscommand[]The childrens/subcommands of the command
interactionsTypesstring[]The types of interaction that could triggered this command
interactionOnlybooleanIf the the command can be triggered only with interactions
timeoutnumberthe waiting time (in ms) before executing an other time this command
onTimeoutcommand.executablethe function that gonna be executed if the command been time out
universalTimeoutbooleanif 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;

3. Configure the handler


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

4. Execute the commands


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

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

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

ParameterTypeDescription
objobj or commandA command or an object that fit the command constructor
categoriesstring[] or nullAll 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

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

PropertyTypeDescription
contentArray<any>The content of the current queu
maxSizenumberThe maximum size of the queu
currentlyPlayingbooleanIf the queu is playing an audio ressource
lastSonganyThe last song the queu has played
isLoopbooleanIf the queu is in a loop
connectionfalse or Voice.VoiceConnectionFalse is the connection to the voice channel is not yet created
audioPlayerVoice.AudioPlayerThe player that gonna play the song into the channel
MethodsArgumentsDescription
additem: any, force: booleanAdd 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
playressource: Voice.AudioRessource, voiceChannel: Discord.VoiceChannel/nullMake the bot play a ressource into a voice channel
createVoiceConnectionvoiceChannel: Discord.VoiceChannel, options: {selfMute, selfDeaf, group, debug}Create a voice connection to a voice channel
hasVoiceConnectionCheck if the queu have a voice connection
regenerateAudioPlayeroptions: Voice.CreateAudioPlayerOptions/undefinedRegenerate the audioPlayer of the queu
nextReturn the incoming item into the queu
getContentindex: numberGet 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).

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)

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 |

5. Handler properties and methods.


PropertiesTypeDescription
commandcommandThe command constructor
staticCommandsobjectThe instance for statics commands
configurationobjectThe configuration of the handler
autorisedobjectThe instance for black/white list
cacheobjectThe cache of the handler
voiceobjectThe instance for voice connections
commandsArray<command>The array that contains every commands
MethodsArgumentsDescription
registerresolvable: string / command, options: { auto_categorise: boolean, recursive: boolean, filter: (name: string, path: string, isDirectory: boolean) => {} } / undefinedRegister a command to the handler
executeInteractioninteraction: Discord.Interaction, options: { client: Discord.Client } / undefinedExecute a specified command by the base of an interaction
executeMessagemessage: Discord.Message, options: { commandName: string, client: Discord.Client } / undefinedExecute the specified command by the base of a message
hasCommandcommand: string / Array<string> / Array<Array<string>>, options: { strict: boolean, strictEntries: boolean, filter: (value, index: number, array: Array<any>) => {} } / undefinedCheck if the handler has specific command
unloadcommand: string / commandUnlaod a command that was previously register. Can use * to unload all the commands.
resolveresolvable: Discord.Message / Discord.Interaction, options: { client: Discord.Client } / undefinedResolve a Discord.Message or a Discord.Interaction
parsemessage: Discord.MessageParse a message or an instance of and split it into three (command, content, arguments)
3.1.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.8

2 years ago

2.0.5

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago