Skip to content

Commit c036365

Browse files
authored
Merge pull request #8 from Mimickal/djs-v14
2 parents 54a613e + 446161b commit c036365

File tree

5 files changed

+737
-801
lines changed

5 files changed

+737
-801
lines changed

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
src="https://www.gnu.org/graphics/lgplv3-with-text-154x68.png">
55
</a>
66

7+
_**NOTE: version 2.x of this library supports discord.js v14. If you still need
8+
v13 support, use an older 1.x version of this library.**_
9+
710
This is a data structure that lets you define Discord.js slash commands,
811
register them with Discord's API, and route Discord.js Interaction events to
912
your handler for that command.
1013

1114
Currently Discord.js separates slash command creation into three different,
1215
weirdly disjoined processes. They want you to:
13-
1. [Define your commands with a builder](https://github.com/discordjs/builders/blob/main/docs/examples/Slash%20Command%20Builders.md),
16+
1. [Define your commands with a builder](https://github.com/discordjs/discord.js/tree/main/packages/builders),
1417
which is only used to construct the data of an HTTP call.
15-
1. [Deploy them with a separate HTTP PUT call](https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script),
18+
1. [Deploy them with a separate HTTP PUT call](https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands),
1619
which uses an entirely separate library that directly relies on the Discord API
17-
1. [Set up a fairly complicated file structure for each command](https://github.com/discordjs/builders/blob/main/docs/examples/Slash%20Command%20Builders.md),
20+
1. [Set up a fairly complicated file structure for each command](https://discordjs.guide/creating-your-bot/slash-commands.html),
1821
which still requires you to write your own router and juggle your own handlers
1922

2023
This library simplifies this process by letting you do this all in one place.
@@ -202,29 +205,23 @@ const commands = new SlashCommandRegistry()
202205
);
203206
```
204207

205-
## Other stuff from `@discordjs/builders`
208+
## Helpers
206209

207210
The Discord.js builders package [has a lot of neat
208-
helper functions](https://discordjs.guide/popular-topics/builders.html). The
209-
command registry passes all of these functions through, so they can be included
210-
directly (preventing the need to add / import `@discordjs/builders`).
211+
helper functions](https://discord.js.org/docs/packages/builders/main#/docs/discord.js/14.9.0/general/welcome).
212+
The command registry passes all of these functions through, so they can be
213+
included directly (preventing the need to add / import `@discordjs/builders`).
211214

212215
```js
213216
const { bold, hyperlink, time } = require('discord-command-registry');
214217
```
215218

216219
## Dependencies
217220

218-
This library is built using the following libraries. You will, of course, need
219-
Node and Discord.js, but you don't need any of the others. This library
220-
downloads these dependencies for you, and you interact with them through this
221-
library.
221+
This library is built using the following libraries:
222222

223-
- Node 16.6.0 (or above)
224-
- [discord.js 13.x](https://discord.js.org/#/docs/main/13.3.1/general/welcome)
225-
- [@discordjs/builders](https://www.npmjs.com/package/@discordjs/builders)
226-
- [@discordjs/rest 0.1.0-canary.0](https://www.npmjs.com/package/@discordjs/rest)
227-
- [discord-api-types v9](https://www.npmjs.com/package/discord-api-types)
223+
- Node 16.9.0 (or above)
224+
- [discord.js 14.x](https://discord.js.org/#/docs/discord.js/14.9.0/general/welcome)
228225

229226
## License
230227

index.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* <https://www.gnu.org/licenses/lgpl-3.0.en.html> for more information.
99
******************************************************************************/
1010

11-
/**
12-
* @external CommandInteraction
13-
* @see https://discord.js.org/#/docs/main/stable/class/CommandInteraction
14-
*/
1511
/**
1612
* The function called during command execution.
1713
*
@@ -22,7 +18,7 @@
2218

2319
const {
2420
Application,
25-
Interaction,
21+
BaseInteraction,
2622
Snowflake,
2723
CommandInteraction,
2824
} = require('discord.js');
@@ -38,6 +34,8 @@ const {
3834
SlashCommandSubcommandGroupBuilder,
3935
} = require('@discordjs/builders');
4036

37+
const API_VERSION = '10';
38+
4139
/**
4240
* Sets a handler function called when this command is executed.
4341
*
@@ -63,7 +61,8 @@ SlashCommandSubcommandGroupBuilder.prototype.setHandler = setHandler;
6361

6462
/**
6563
* A collection of Discord.js commands that registers itself with Discord's API
66-
* and routes Discord.js Interaction events to the appropriate command handlers.
64+
* and routes Discord.js {@link BaseInteraction} events to the appropriate
65+
* command handlers.
6766
*/
6867
class SlashCommandRegistry {
6968

@@ -96,11 +95,11 @@ class SlashCommandRegistry {
9695
* Creates a new {@link SlashCommandRegistry}.
9796
*/
9897
constructor() {
99-
this.#rest = new REST({ version: '9' });
98+
this.#rest = new REST({ version: API_VERSION });
10099
}
101100

102101
/**
103-
* Defines a new command from a builder.
102+
* Defines a new slash command from a builder.
104103
* Commands defined here can also be registered with Discord's API.
105104
*
106105
* @param {SlashCommandBuilder|Function<SlashCommandBuilder>} input
@@ -211,24 +210,24 @@ class SlashCommandRegistry {
211210
}
212211

213212
/**
214-
* Attempts to execute the given Discord.js Interaction using the most
215-
* specific handler provided. For example, if an individual subcommand does
216-
* not have a handler but the parent command does, the parent's handler will
217-
* be called. If no builder matches the interaction, the default handler is
218-
* called (if provided).
213+
* Attempts to execute the given Discord.js {@link BaseInteraction} using
214+
* the most specific handler provided. For example, if an individual
215+
* subcommand does not have a handler but the parent command does, the
216+
* parent's handler will be called. If no builder matches the interaction,
217+
* the default handler is called (if provided).
219218
*
220219
* This function is a no-op if:
221-
* - The interaction is not a supported {@link Interaction} type. We
220+
* - The interaction is not a supported {@link BaseInteraction} type. We
222221
* currently support:
223222
* - {@link CommandInteraction}
224223
* - {@link ContextMenuInteraction}
225224
* - No builder matches the interaction and no default handler is set.
226225
*
227226
* This function is set up so it can be directly used as the handler for
228-
* Discord.js' `interactionCreate` event (but you may consider a thin wrapper
229-
* for logging).
227+
* Discord.js' `interactionCreate` event (but you may consider a thin
228+
* wrapper for logging).
230229
*
231-
* @param {Interaction} interaction A Discord.js Interaction object.
230+
* @param {BaseInteraction} interaction A Discord.js interaction object.
232231
* @return {Promise<*>} Fulfills based on command execution.
233232
* @resolve The value returned from the {@link Handler}.
234233
* @reject
@@ -238,12 +237,11 @@ class SlashCommandRegistry {
238237
* - Any Error that occurs during handler execution.
239238
*/
240239
async execute(interaction) {
241-
// TODO maybe allow "non-strict" interaction matching?
242-
if (!(interaction instanceof Interaction)) {
243-
throw new Error('given value was not a Discord.js Interaction');
240+
if (!(typeof interaction?.isCommand === 'function')) {
241+
throw new Error(`given value was not a Discord.js command`);
244242
}
245243

246-
if (!interaction.isCommand() && !interaction.isContextMenu()) {
244+
if (!interaction.isCommand?.() && !interaction.isContextMenuCommand?.()) {
247245
return;
248246
}
249247

@@ -357,7 +355,7 @@ class SlashCommandRegistry {
357355
*/
358356
async function getApplication(interaction, opt_name, required=false) {
359357
const app_id = interaction.options.getString(opt_name, required);
360-
return new REST({ version: '10' })
358+
return new REST({ version: API_VERSION })
361359
.setToken('ignored')
362360
.get(`/applications/${app_id}/rpc`) // NOTE: undocumented endpoint!
363361
.then(data => new Application(interaction.client, data))

0 commit comments

Comments
 (0)