@hyperneutrino/djs-lite 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.ts +19 -5
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  ApplicationCommandType,
3
+ AutocompleteInteraction,
3
4
  Client,
4
5
  CommandInteraction,
5
6
  Events,
@@ -17,19 +18,28 @@ import path from "node:path";
17
18
 
18
19
  process.on("uncaughtException", (err) => console.error(err));
19
20
 
20
- type Handler<T extends CommandInteraction> = (interaction: T) => Awaitable<unknown>;
21
+ type Handler<T extends CommandInteraction | AutocompleteInteraction> = (interaction: T) => Awaitable<unknown>;
21
22
 
22
- class Command<T extends BaseApplicationCommandData, U extends CommandInteraction> {
23
+ class Command<T extends BaseApplicationCommandData, U extends CommandInteraction, Z extends boolean = false> {
23
24
  data: T;
24
25
  handler: Handler<U>;
26
+ autocomplete: Handler<AutocompleteInteraction> | null;
27
+
28
+ constructor({ handler, ...data }: T & { handler: Handler<U> } & (Z extends true ? { autocomplete?: Handler<AutocompleteInteraction> } : {})) {
29
+ if ("autocomplete" in data) {
30
+ const { autocomplete, ...rest } = data;
31
+ this.autocomplete = autocomplete as Handler<AutocompleteInteraction>;
32
+ this.data = rest as unknown as T;
33
+ } else {
34
+ this.autocomplete = null;
35
+ this.data = data as unknown as T;
36
+ }
25
37
 
26
- constructor({ handler, ...data }: T & { handler: Handler<U> }) {
27
- this.data = data as unknown as T;
28
38
  this.handler = handler;
29
39
  }
30
40
  }
31
41
 
32
- export class SlashCommand extends Command<ChatInputApplicationCommandData & { type: ApplicationCommandType.ChatInput }, ChatInputCommandInteraction> {}
42
+ export class SlashCommand extends Command<ChatInputApplicationCommandData & { type: ApplicationCommandType.ChatInput }, ChatInputCommandInteraction, true> {}
33
43
  export class UserCommand extends Command<UserApplicationCommandData, UserContextMenuCommandInteraction> {}
34
44
  export class MessageCommand extends Command<MessageApplicationCommandData, MessageContextMenuCommandInteraction> {}
35
45
 
@@ -42,6 +52,8 @@ export async function loadCommands(client: Client<true>, directory: string) {
42
52
  const userCommandHandlers = new Map<string, Handler<UserContextMenuCommandInteraction>>();
43
53
  const messageCommandHandlers = new Map<string, Handler<MessageContextMenuCommandInteraction>>();
44
54
 
55
+ const slashCommandAutocompletes = new Map<string, Handler<AutocompleteInteraction>>();
56
+
45
57
  await Promise.all(
46
58
  files.map(async (file) => {
47
59
  const { default: item } = await import(path.resolve(file.parentPath, file.name));
@@ -49,6 +61,7 @@ export async function loadCommands(client: Client<true>, directory: string) {
49
61
  if (item instanceof SlashCommand) {
50
62
  commandData.push(item.data);
51
63
  slashCommandHandlers.set(item.data.name, item.handler);
64
+ if (item.autocomplete) slashCommandAutocompletes.set(item.data.name, item.autocomplete);
52
65
  } else if (item instanceof UserCommand) {
53
66
  commandData.push(item.data);
54
67
  userCommandHandlers.set(item.data.name, item.handler);
@@ -67,6 +80,7 @@ export async function loadCommands(client: Client<true>, directory: string) {
67
80
  if (interaction.isChatInputCommand()) slashCommandHandlers.get(interaction.commandName)?.(interaction);
68
81
  else if (interaction.isUserContextMenuCommand()) userCommandHandlers.get(interaction.commandName)?.(interaction);
69
82
  else if (interaction.isMessageContextMenuCommand()) messageCommandHandlers.get(interaction.commandName)?.(interaction);
83
+ else if (interaction.isAutocomplete()) slashCommandAutocompletes.get(interaction.commandName)?.(interaction);
70
84
  });
71
85
  }
72
86
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hyperneutrino/djs-lite",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "module": "index.ts",
6
6
  "type": "module",
7
7
  "devDependencies": {