@davipccunha/discordjs-helper v2.6.4
About
Easily create interactions with type safety
discordjs-helper allows you to more easily create and register interactions to the Discord API. It ensures type safety and avoid runtime errors. This package contains:
- Interfaces to implement and consistently define your interactions
- Decorators to help with common verifications such as users' permissions
- Extensions to discordjs' classes like BaseInteraction#replyOrFollowUp()
A complete documentation is available at https://davipccunha.github.io/discordjs-helper/docs/intro
Installation
Node.js and npm required!
Run into your project's terminal:
npm install @davipccunha/discordjs-helperExamples
Creating a new slash command
import { CustomChatInputCommand, ExtendedClient, RegisterChatInputCommand, RequireMemberPermission } from "@davipccunha/discordjs-helper";
import { ApplicationCommandType, ChatInputCommandInteraction, PermissionFlagsBits } from "discord.js";
@RegisterChatInputCommand("ping", "Ping the bot!")
@RequireMemberPermission(PermissionFlagsBits.Administrator)
export class PingCommand implements CustomChatInputCommand {
name!: string;
type!: ApplicationCommandType.ChatInput;
description!: string;
defaultPermission!: boolean;
async execute(interaction: ChatInputCommandInteraction, client: ExtendedClient): Promise<void> {
await interaction.reply('Pong!').catch(console.error);
}
}The class above defines a chat input command. It contains the information to create the command and a method execute(), that is run when a command interaction with the same name as the class' attribute name is created on Discord.
Registering slash commands
Commands should be either registered using the ExtendedClient#registerCommands() method or by decorating its class with @Register...Command(name) and then sent to Discord API using ExtendedClient#loadCommands(). Due to how Node.js loads the modules, a module that contains a command class decorated with @Register...Command must be imported in some point of your program
import path from 'path';
import { pathToFileURL } from 'url';
import { ExtendedClient, recursiveFiles } from "@davipccunha/discordjs-helper";
const client = new ExtendedClient("TOKEN GOES HERE");
// This is necessary due to how Node.js loads modules
await registerInteractions();
// This should be called after registering the commands
// This function caches the registered interactions, starts the bot and starts listening for interactions being created
client.start(true);
// This function registers the cached commands to the guilds passed as parameters (it registers to all if no parameter is passed)
client.loadCommands("GUILD ID 1", "GUILD ID 2", ...);
// This code gets all files inside the dist/interactions folder and imports them so they are loaded and interactions decorated with @Register... are correctly registered
async function registerInteractions() {
const interactions = await recursiveFiles("dist/interactions");
for (const interaction of interactions) {
const fileName = interaction.replaceAll('\\', '/');
const absolutePath = path.resolve(fileName);
const fileUrl = pathToFileURL(absolutePath).href;
await import(fileUrl);
}
};Together, the two code snippets above is all there is to get your first slash command working.
The example provided on how to register the commands is a simple way of loading all modules, regardless of how many interactions you have, instead of having to instantiate each one of them and pass them as parameter to the register methods
Reminders
To use decorators, you must enable them in your .tsconfig file {"compilerOptions": {"experimentalDecorators": true}}.
For interactions decorated with @Register..., the module in which they are defined must be imported somewhere in the main program due to how Node.js loads modules
You can always register all your interactions using one of the following methods from ExtendedClient:
- registerCommands()
- registerButtons()
- registerSelectMenus()
- registerModals()
and passing false as argument to the ExtendedClient#start() method (to disable auto-register)
Then, the package handles the rest, including when interactions are created
Found a problem?
Please let me know of any problems found by opening an issue at GitHub issue. If you have a suggestion or just want to contact me, please send an email to davipccunha@gmail.com
Donations
If you are feeling generous or would like to support this and other future open-source projects, you can donate at my Buy me a coffee page
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago