@fataussiefatboy/discord.js-commands v0.2.1
discord.js-commands
Getting Started
Install the NPM package
yarn add @fataussiefatboy/discord.js-commands
-- or --
npm install @fataussiefatboy/discord.js-commands
Create a new CommandHandler instance
// bot.js
const { InteractionHandler } = require("@fataussiefatboy/discord.js-commands");
const { Client } = require("discord.js");
const client = new Client();
const interactions = new InteractionHandler(client);
Construct your DiscordCommand's
// ping.js
const { SlashCommand } = require("@fataussiefatboy/discord.js-commands");
class PingCommand extends SlashCommand {
constructor() {
super({
name: 'ping',
description: 'Latency between sending and receiving packets from Discords API',
dm_permission: true
});
}
execute(interaction) {
return interaction.reply({ content: `\` ${interaction.client.ws.ping}ms \` latency!` });
}
}
module.exports = PingCommand;
Register your command
// bot.js
const { InteractionHandler } = require("@fataussiefatboy/discord.js-commands");
const { Client } = require("discord.js");
const client = new Client();
const interactions = new InteractionHandler(client);
// ...
const { PingCommand } = require("./ping.js");
interactions.registerCommands(new PingCommand());
Construct your DiscordComponent's
// accept.js
const { ButtonComponent } = require("@fataussiefatboy/discord.js-commands");
class AcceptButton extends ButtonComponent {
constructor() {
super({
custom_id: 'accept',
style: 'SUCCESS'
});
}
execute(interaction) {
return interaction.editReply({ content: 'Thanks for accepting!' });
}
}
module.exports = AcceptButton;
// deny.js
const { ButtonComponent } = require("@fataussiefatboy/discord.js-commands");
class DenyButton extends ButtonComponent {
constructor() {
super({
custom_id: 'deny',
style: 'DANGER'
});
}
execute(interaction) {
return interaction.editReply({ content: 'It\'s okay for denying us ;-;' });
}
}
module.exports = DenyButton;
Register your components
// bot.js
const { InteractionHandler } = require("@fataussiefatboy/discord.js-commands");
const { Client } = require("discord.js");
const client = new Client();
const interactions = new InteractionHandler(client);
// ...
const { AcceptButton } = require("./accept.js");
const { DenyButton } = require("./deny.js");
interactions.registerComponents([ new AcceptButton(), new DenyButton() ]);
Login the discord.js client
client.login('token_was_here');
Run the script
> node bot.js
Thats it, your command should become globally active and testable after one hour and your components should now be callable inside message payloads. Happy coding <3
InteractionHandler
Constructor
new InteractionHandler(client: Client);
param | type | description |
---|---|---|
client | Client | Discord.js Client |
Properties
client
Discord.js Client instancing this handler.@type
Client
commands
DiscordCommand instances mapped by their command name.@type
Collection<string, DiscordCommand>
components
DiscordComponent instances mapped by their custom id.@type
Collection<string, DiscordComponent>
Methods
Register DiscordCommands.
If an array, all entries will attempt to be registered.
InteractionHandler.registerCommands(commands: DiscordCommand | DiscordCommand[]): DiscordCommand | null | Array<DiscordCommand | null>
parameter | type | description |
---|---|---|
commands | DiscordCommand | DiscordCommand[] | DiscordCommand(s) to register. |
@returns The command(s) successfully registered.
@emits Client#error
- When commands cannot be successfully registered.
Registers all DiscordCommand files stored in a directory.
InteractionHandler.registerCommandsIn(directory: PathLike): Array<DiscordCommand | null>
parameters | type | description |
---|---|---|
directory | PathLike | The directory with stored DiscordCommands |
@returns An array of the successfully registered commands.
@emits Client#error
- When commands cannot be successfully registered.
Unregisters a DiscordCommand.
InteractionHandler.unregisterComponent(component: DiscordCommandResolvable): boolean
parameters | type | description |
---|---|---|
command | DiscordCommandResolvable | Resolvable of the DiscordCommand to unregister. |
@returns A boolean of if the command was successfully unregistered.
Register DiscordComponents.
If an array, all entries will attempt to be registered.
InteractionHandler.registerComponents(components: DiscordComponent | DiscordComponent[]): DiscordComponent | null | Array<DiscordComponent | null>
parameter | type | description |
---|---|---|
components | DiscordComponent | DiscordComponent[] | DiscordComponent(s) to register. |
@returns The successfully registered component(s).
@emits Client#error
- When components cannot be successfully registered.
Registers all DiscordComponent files stored in a directory.
InteractionHandler.registerComponentsIn(directory: PathLike): Array<DiscordComponent | null>
parameters | type | description |
---|---|---|
directory | PathLike | The directory with stored DiscordComponents |
@returns An array of the successfully registered components.
@emits Client#error
- When components cannot be successfully registered.
Unregisters a DiscordComponent.
InteractionHandler.unregisterComponent(component: DiscordComponentResolvable): boolean
parameters | type | description |
---|---|---|
component | DiscordComponentResolvable | Resolvable of the DiscordComponent to unregister. |
@returns A boolean of if the component was successfully unregistered.
DiscordCommand
@abstract
Constructor
class Command extends DiscordCommand {}
// Base class of all command types
Properties
name
The DiscordCommands name@type
string
@readonly
name_localizations?
List of localized names for the DiscordCommand mapped by their LocaleString.@type
?Record<LocaleString, string>
type?
Type of the DiscordCommand, defaults to
'CHAT_INPUT'
@typeApplicationCommandType
default_member_permissions?
The default guild permission(s) of this DiscordCommand.@type
?Array<PermissionString | PermissionFlags> | PermissionString | PermissionFlags
dm_permission?
Whether this ChatInputCommand can be used in direct messages with the bot, defaults to
true
@typeboolean
Methods
Executes when the DiscordCommand is used.
DiscordCommand.execute(interaction: Interaction): Promise<any> | any;
parameters | type | description |
---|---|---|
interaction | Interaction | The interaction passed through the InteractionHandler. |
ChatInputCommand
@extends DiscordCommand @abstract
Constructor
class Command extends ChatInputCommand {}
// Base class of all commands passed through the '/' menu on discord.
Properties
All properites of DiscordCommand
description
The ChatInputCommands description@type
string
description_localizations?
List of localized descriptions for the ChatInputCommand mapped by their LocaleString.@type
?Record<LocaleString, string>
options?
Array of ApplicationCommandOptions to pass to the discord
/
menu.@typeDiscordCommandOptionResolver
ContextMenuCommand
@extends DiscordCommand @abstract
Constructor
class Command extends ContextMenuCommand {}
// Base class of all commands passed through the 'right click/hold' menu on discord.
Properties
All properites of DiscordCommand
type
Whether the ContextMenuCommand is a Message or User ContextMenu@type
'MESSAGE' | 'USER'
DiscordCommandOptionResolver
Constructor
new DiscordCommandOptionResolver(options: ChatInputCommandOptionData[] | [])
Properties
data
The original ChatInputCommandOptionData passed.@type
Readonly<ChatInputCommandOptionData[] | []>
@readonly
Methods
Gets the data of the first ChatInputCommandOption that matches the name and type, if defined.
DiscordCommandOptionResolver.get(_name: string, _type?: ApplicationCommandOptionType): CommandOptionData | null;
parameters | type | optional | description |
---|---|---|---|
_name | string | false | The name of the option. |
_type | ApplicationCommandOptionType | true | The type of the option. |
@returns The CommandOptionData of the option.
Gets the name of the ChatInputCommands first Subcommand.
DiscordCommandOptionResolver.getSubcommand(): string | null;
@returns The subcommands name.
Gets the name of the ChatInputCommands first SubcommandGroup.
DiscordCommandOptionResolver.getSubcommandGroup(): string | null;
@returns The subcommand groups name.
Gets an option of type STRING
.
DiscordCommandOptionResolver.getString(_name: string): StringOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The StringOptionData of a string option if found.
Gets an option of type NUMBER
.
DiscordCommandOptionResolver.getNumber(_name: string): NumberOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The NumberOptionData of a number option if found.
Gets an option of type INTEGER
.
DiscordCommandOptionResolver.getInteger(_name: string): IntegerOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The IntegerOptionData of a integer option if found.
Gets an option of type BOOLEAN
.
DiscordCommandOptionResolver.getBoolean(_name: string): BooleanOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The BooleanOptionData of a boolean option if found.
Gets an option of type USER
.
DiscordCommandOptionResolver.getUser(_name: string): UserOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The UserOptionData of a user option if found.
Gets an option of type ROLE
.
DiscordCommandOptionResolver.getRole(_name: string): RoleOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The RoleOptionData of a role option if found.
Gets an option of type MENTIONABLE
.
DiscordCommandOptionResolver.getMentionable(_name: string): MentionableOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The MentionableOptionData of a mentionable option if found.
Gets an option of type CHANNEL
.
DiscordCommandOptionResolver.getChannel(_name: string): ChannelOptionData | null;
parameters | type | description |
---|---|---|
_name | string | The name of the option. |
@returns The ChannelOptionData of a channel option if found.
Types & Interfaces
DiscordCommandResolvable
Resolvable value types for a valid DiscordCommand.
- string
- DiscordCommand
- { name: string, type?: ApplicationCommandType }
DiscordComponentResolvable
Resolvable value types for a valid DiscordComponent.
- string
- DiscordComponent
- { custom_id: string, type?: MessageComponentType }
LocaleString
List of acceptable Discord API Localizations.
- Danish
- German
- EnglishUK
- EnglishUS
- Spanish
- French
- Croatian
- Italian
- Lithuanian
- Hungarian
- Dutch
- Norwegian
- Polish
- PortugueseBrazilian
- RomanianRomania
- Finnish
- Swedish
- Vietnamese
- Turkish
- Czech
- Greek
- Bulgarian
- Russian
- Ukrainian
- Hindi
- Thai
- ChineseChina
- Japanese
- ChineseTaiwan
- Korean
ChatInputCommandOptions
CommandOptionData
The base option data parsed to all options. | property | optional | description | :------------------------ | :------- | :--- | type | false | The type of the option | name | false | Name of the option | name_localizations | true | Localized names of the option | description | false | Description of the option | description_localizations | true | Localized descriptions of the option
StringOptionData
Option that requires a string input. @extends CommandOptionData