0.2.1 • Published 2 years ago

@fataussiefatboy/discord.js-commands v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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);
paramtypedescription
clientClientDiscord.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>
parametertypedescription
commandsDiscordCommand | 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>
parameterstypedescription
directoryPathLikeThe 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
parameterstypedescription
commandDiscordCommandResolvableResolvable 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>
parametertypedescription
componentsDiscordComponent | 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>
parameterstypedescription
directoryPathLikeThe 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
parameterstypedescription
componentDiscordComponentResolvableResolvable 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'@type ApplicationCommandType

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@type boolean


Methods

Executes when the DiscordCommand is used.

DiscordCommand.execute(interaction: Interaction): Promise<any> | any;
parameterstypedescription
interactionInteractionThe 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.@type DiscordCommandOptionResolver


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;
parameterstypeoptionaldescription
_namestringfalseThe name of the option.
_typeApplicationCommandOptionTypetrueThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe 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;
parameterstypedescription
_namestringThe name of the option.

@returns The ChannelOptionData of a channel option if found.


Types & Interfaces

DiscordCommandResolvable

Resolvable value types for a valid DiscordCommand.

DiscordComponentResolvable

Resolvable value types for a valid DiscordComponent.

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

== UNDER CONSTRUCTION ==

0.2.1

2 years ago

0.2.0

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago