From 53ea4063ac7c7e91f33eb76c35088f3d078f4f59 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua <33934311+tguichaoua@users.noreply.github.com> Date: Sun, 17 Oct 2021 16:34:54 +0200 Subject: [PATCH] doc: update doc (#14) * doc(Readme): update description * update the 'how to use' section * package: update description and keywords * Readme: fix typo --- README.md | 168 +++++++++++++++++++++++++++++++++++++++------------ package.json | 6 +- 2 files changed, 135 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index dd6e366..6208c00 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,20 @@ - - + + + + + +
-> A Slash Command handler for discord.js v13 +djs-slash is a command handler for discord.js v13 that provides an easy command definition with file. + +**Features**: +- type checking (with TypeScript) +- slash command +- context menu command ## Install @@ -24,26 +33,90 @@ npm i djs-slash ## How to use -Commands will be load at runtime from the `commands` folder. Files contains the command. Folder are used to create sub command and sub command group. +Project architecture example +``` +├ commands/ (this folder contains all commands) +| ├ slash/ (slash command) +| | ├ ping.ts +| | └ cmd/ +| | ├ subcommand.ts +| | └ subcommandgroup/ +| | └ subcommand.ts +| ├ user/ (context menu commands on user) +| | ├ hi.ts +| | └ ban.ts +| └ message/ (context menu commands on message) +| ├ delete.ts +| └ repeat.ts +└ index.ts +``` -The name of the command is the name of the file (without the extension). +### Load commands +```ts +const commands = SlashCommandManager.create({ + commandFolder: './commands' // This is the path to the folder where command are stored relative to the entry point of the application (ie the index.ts file) +}); +// If no parameter is provided, './commands' is used as default path +const commands = SlashCommandManager.create(); ``` -└ commands/ - ├ ping.ts - └ cmd/ - ├ subcommand.ts - └ subcommandgroup/ - └ subcommand.ts + + +### Full script example + +```ts +import { Client, Intents } from "discord.js"; +import { SlashCommandManager } from "djs-slash"; + +const GUILD_ID = '000000000000000000' +const BOT_TOKEN = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; + +// Create the djs client +const client = new Client({ intents: [Intents.FLAGS.GUILDS] }); + +// Load all commands from ./commands/ +const commands = SlashCommandManager.create(); + +client.once("ready", async (client) => { + // Registering commands on a guild + const guild = await client.guilds.fetch(GUILD_ID); + await guild.commands.set(commands.toApplicationCommandData()); + + // Registering commands globally + await client.application.commands.set(commands.toApplicationCommandData()); +}); + +client.on("interactionCreate", async (interaction) => { + // Pass the interaction to the SlashCommandManager to execute the command + if (await commands.execute(interaction)) { + // The interaction trigger a command + } else { + // The interaction didn't trigger a command. + // Either the interaction is not a command interaction + // either it didn't correspond to a registred command. + } +}); + +(async () => { + await client.login(BOT_TOKEN); +})(); ``` -Will create the following commands: +### Define a command -- `/ping` -- `/cmd subcommand` -- `/cmd subcommandgroup subcommand` +#### Slash command +Slash command files must be placed in a sub-folder named `slash` inside the command folder. +The name of the file is the name of the command. +To create sub-command and sub-command group use folder. Folder name is the name of the command/group. -### Command file +ping example: +```ts +import { SlashCommand } from 'djs-slash'; + +export default SlashCommand.define("Reply with 'pong'", {}, async (interaction) => { + await interaction.reply({ content: ':ping_pong: pong !', ephemeral: true }); +}); +``` ```ts import { SlashCommand } from "djs-slash"; @@ -86,38 +159,59 @@ export default SlashCommand.define( ); ``` -### index.ts +`SlashCommand.define(description, options, callback)` +- `description` `string`: the description of the command. +- `options` `{ defaultPermission, options }` + - [`defaultPermission`](https://discord.js.org/#/docs/main/stable/typedef/ApplicationCommandData) `boolean`: whether the command is enabled by default when the app is added to a guild. + - [`options`](https://discord.js.org/#/docs/main/stable/typedef/ApplicationCommandOptionData) `object`: the options of the command, key is the name of the options. + - `description` `string`: the description of the option. + - `required` `boolean`: whether the option is required. + - `type`: the type of the option. + - `choices`: the choices of the option for the user to pick from. + - `channelTypes`: when the option type is channel, the allowed types of channels that can be selected. +- `callback` `(interaction, options) => void | Promise