commandkit 0.1.8 → 0.1.9-dev.20231213080753

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.
package/dist/index.d.mts CHANGED
@@ -66,7 +66,7 @@ interface CommandProps {
66
66
  /**
67
67
  * Props for autocomplete command run functions.
68
68
  */
69
- interface AutocompleteCommandProps extends CommandProps {
69
+ interface AutocompleteProps extends CommandProps {
70
70
  /**
71
71
  * The current autocomplete command interaction object.
72
72
  */
@@ -105,11 +105,11 @@ interface MessageContextMenuCommandProps extends CommandProps {
105
105
  /**
106
106
  * Props for command validation functions.
107
107
  */
108
- interface ValidationFunctionProps {
108
+ interface ValidationProps {
109
109
  /**
110
110
  * The current command interaction object.
111
111
  */
112
- interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction;
112
+ interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction | AutocompleteInteraction;
113
113
  /**
114
114
  * The Discord.js client object that CommandKit is handling.
115
115
  */
@@ -257,7 +257,8 @@ declare class CommandKit {
257
257
  * The handler to run when a button is clicked. This handler is called with the interaction as the first argument.
258
258
  * If the first argument is null, it means that the interaction collector has been destroyed.
259
259
  */
260
- type CommandKitButtonBuilderInteractionCollectorDispatch = (interaction: ButtonInteraction | null) => Awaitable<void>;
260
+ type CommandKitButtonBuilderInteractionCollectorDispatch = (interaction: ButtonInteraction) => Awaitable<void>;
261
+ type CommandKitButtonBuilderOnEnd = () => Awaitable<void>;
261
262
  type CommandKitButtonBuilderInteractionCollectorDispatchContextData = {
262
263
  /**
263
264
  * The message to listen for button interactions on.
@@ -299,8 +300,8 @@ declare class ButtonKit extends ButtonBuilder {
299
300
  * button.onClick(null);
300
301
  * ```
301
302
  */
302
- onClick(handler: null): this;
303
- onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
303
+ onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data?: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
304
+ onEnd(handler: CommandKitButtonBuilderOnEnd): this;
304
305
  }
305
306
 
306
307
  interface CommandKitConfig {
@@ -375,4 +376,4 @@ declare function createSignal<T = unknown>(value?: CommandKitSignalInitializer<T
375
376
  */
376
377
  declare function createEffect(callback: CommandKitEffectCallback): void;
377
378
 
378
- export { AutocompleteCommandProps, ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, ContextMenuCommandProps, MessageContextMenuCommandProps, ReloadType, SlashCommandProps, UserContextMenuCommandProps, ValidationFunctionProps, createEffect, createSignal, defineConfig, getConfig };
379
+ export { AutocompleteProps, ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitButtonBuilderOnEnd, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, ContextMenuCommandProps, MessageContextMenuCommandProps, ReloadType, SlashCommandProps, UserContextMenuCommandProps, ValidationProps, createEffect, createSignal, defineConfig, getConfig };
package/dist/index.d.ts CHANGED
@@ -66,7 +66,7 @@ interface CommandProps {
66
66
  /**
67
67
  * Props for autocomplete command run functions.
68
68
  */
69
- interface AutocompleteCommandProps extends CommandProps {
69
+ interface AutocompleteProps extends CommandProps {
70
70
  /**
71
71
  * The current autocomplete command interaction object.
72
72
  */
@@ -105,11 +105,11 @@ interface MessageContextMenuCommandProps extends CommandProps {
105
105
  /**
106
106
  * Props for command validation functions.
107
107
  */
108
- interface ValidationFunctionProps {
108
+ interface ValidationProps {
109
109
  /**
110
110
  * The current command interaction object.
111
111
  */
112
- interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction;
112
+ interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction | AutocompleteInteraction;
113
113
  /**
114
114
  * The Discord.js client object that CommandKit is handling.
115
115
  */
@@ -257,7 +257,8 @@ declare class CommandKit {
257
257
  * The handler to run when a button is clicked. This handler is called with the interaction as the first argument.
258
258
  * If the first argument is null, it means that the interaction collector has been destroyed.
259
259
  */
260
- type CommandKitButtonBuilderInteractionCollectorDispatch = (interaction: ButtonInteraction | null) => Awaitable<void>;
260
+ type CommandKitButtonBuilderInteractionCollectorDispatch = (interaction: ButtonInteraction) => Awaitable<void>;
261
+ type CommandKitButtonBuilderOnEnd = () => Awaitable<void>;
261
262
  type CommandKitButtonBuilderInteractionCollectorDispatchContextData = {
262
263
  /**
263
264
  * The message to listen for button interactions on.
@@ -299,8 +300,8 @@ declare class ButtonKit extends ButtonBuilder {
299
300
  * button.onClick(null);
300
301
  * ```
301
302
  */
302
- onClick(handler: null): this;
303
- onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
303
+ onClick(handler: CommandKitButtonBuilderInteractionCollectorDispatch, data?: CommandKitButtonBuilderInteractionCollectorDispatchContextData): this;
304
+ onEnd(handler: CommandKitButtonBuilderOnEnd): this;
304
305
  }
305
306
 
306
307
  interface CommandKitConfig {
@@ -375,4 +376,4 @@ declare function createSignal<T = unknown>(value?: CommandKitSignalInitializer<T
375
376
  */
376
377
  declare function createEffect(callback: CommandKitEffectCallback): void;
377
378
 
378
- export { AutocompleteCommandProps, ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, ContextMenuCommandProps, MessageContextMenuCommandProps, ReloadType, SlashCommandProps, UserContextMenuCommandProps, ValidationFunctionProps, createEffect, createSignal, defineConfig, getConfig };
379
+ export { AutocompleteProps, ButtonKit, CommandData, CommandKit, CommandKitButtonBuilderInteractionCollectorDispatch, CommandKitButtonBuilderInteractionCollectorDispatchContextData, CommandKitButtonBuilderOnEnd, CommandKitConfig, CommandKitEffectCallback, CommandKitSignal, CommandKitSignalInitializer, CommandKitSignalUpdater, CommandObject, CommandOptions, CommandProps, ContextMenuCommandProps, MessageContextMenuCommandProps, ReloadType, SlashCommandProps, UserContextMenuCommandProps, ValidationProps, createEffect, createSignal, defineConfig, getConfig };
package/dist/index.js CHANGED
@@ -594,8 +594,8 @@ var CommandHandler = class {
594
594
  );
595
595
  if (!targetCommand)
596
596
  return;
597
- const { data, options, run, autocompleteRun, ...rest } = targetCommand;
598
- if (isAutocomplete && !autocompleteRun)
597
+ const { data, options, run, autocomplete, ...rest } = targetCommand;
598
+ if (isAutocomplete && !autocomplete)
599
599
  return;
600
600
  const commandObj = {
601
601
  data: targetCommand.data,
@@ -640,7 +640,7 @@ var CommandHandler = class {
640
640
  client: this.#data.client,
641
641
  handler: this.#data.commandkitInstance
642
642
  };
643
- await targetCommand[isAutocomplete ? "autocompleteRun" : "run"](context2);
643
+ await targetCommand[isAutocomplete ? "autocomplete" : "run"](context2);
644
644
  });
645
645
  }
646
646
  get commands() {
@@ -896,7 +896,7 @@ var CommandKit = class {
896
896
  return [];
897
897
  }
898
898
  const commands = this.#data.commandHandler.commands.map((cmd) => {
899
- const { run, ...command } = cmd;
899
+ const { run, autocomplete, ...command } = cmd;
900
900
  return command;
901
901
  });
902
902
  return commands;
@@ -943,19 +943,55 @@ var CommandKit = class {
943
943
  var import_discord2 = require("discord.js");
944
944
  var ButtonKit = class extends import_discord2.ButtonBuilder {
945
945
  #onClickHandler = null;
946
+ #onEndHandler = null;
946
947
  #contextData = null;
947
948
  #collector = null;
949
+ /**
950
+ * Sets up an inline interaction collector for this button. This collector by default allows as many interactions as possible if it is actively used.
951
+ * If unused, this expires after 24 hours or custom time if specified.
952
+ * @param handler The handler to run when the button is clicked
953
+ * @param data The context data to use for the interaction collector
954
+ * @returns This button
955
+ * @example
956
+ * ```ts
957
+ * const button = new ButtonKit()
958
+ * .setLabel('Click me')
959
+ * .setStyle(ButtonStyle.Primary)
960
+ * .setCustomId('click_me');
961
+ *
962
+ * const row = new ActionRowBuilder().addComponents(button);
963
+ *
964
+ * const message = await channel.send({ content: 'Click the button', components: [row] });
965
+ *
966
+ * button.onClick(async (interaction) => {
967
+ * await interaction.reply('You clicked me!');
968
+ * }, { message });
969
+ *
970
+ * // Remove onClick handler and destroy the interaction collector
971
+ * button.onClick(null);
972
+ * ```
973
+ */
948
974
  onClick(handler, data) {
949
975
  if (this.data.style === import_discord2.ButtonStyle.Link) {
950
976
  throw new TypeError('Cannot setup "onClick" handler on link buttons.');
951
977
  }
978
+ if (!handler) {
979
+ throw new TypeError('Cannot setup "onClick" without a handler function parameter.');
980
+ }
952
981
  this.#destroyCollector();
953
982
  this.#onClickHandler = handler;
954
- if (handler && data)
983
+ if (data)
955
984
  this.#contextData = data;
956
985
  this.#setupInteractionCollector();
957
986
  return this;
958
987
  }
988
+ onEnd(handler) {
989
+ if (!handler) {
990
+ throw new TypeError('Cannot setup "onEnd" without a handler function parameter.');
991
+ }
992
+ this.#onEndHandler = handler;
993
+ return this;
994
+ }
959
995
  #setupInteractionCollector() {
960
996
  if (!this.#contextData || !this.#onClickHandler)
961
997
  return;
@@ -992,10 +1028,10 @@ var ButtonKit = class extends import_discord2.ButtonBuilder {
992
1028
  });
993
1029
  this.#collector.on("end", () => {
994
1030
  this.#destroyCollector();
1031
+ this.#onEndHandler?.();
995
1032
  });
996
1033
  }
997
1034
  #destroyCollector() {
998
- this.#onClickHandler?.(null);
999
1035
  this.#collector?.stop("end");
1000
1036
  this.#collector?.removeAllListeners();
1001
1037
  this.#collector = null;
package/dist/index.mjs CHANGED
@@ -561,8 +561,8 @@ var CommandHandler = class {
561
561
  );
562
562
  if (!targetCommand)
563
563
  return;
564
- const { data, options, run, autocompleteRun, ...rest } = targetCommand;
565
- if (isAutocomplete && !autocompleteRun)
564
+ const { data, options, run, autocomplete, ...rest } = targetCommand;
565
+ if (isAutocomplete && !autocomplete)
566
566
  return;
567
567
  const commandObj = {
568
568
  data: targetCommand.data,
@@ -607,7 +607,7 @@ var CommandHandler = class {
607
607
  client: this.#data.client,
608
608
  handler: this.#data.commandkitInstance
609
609
  };
610
- await targetCommand[isAutocomplete ? "autocompleteRun" : "run"](context2);
610
+ await targetCommand[isAutocomplete ? "autocomplete" : "run"](context2);
611
611
  });
612
612
  }
613
613
  get commands() {
@@ -863,7 +863,7 @@ var CommandKit = class {
863
863
  return [];
864
864
  }
865
865
  const commands = this.#data.commandHandler.commands.map((cmd) => {
866
- const { run, ...command } = cmd;
866
+ const { run, autocomplete, ...command } = cmd;
867
867
  return command;
868
868
  });
869
869
  return commands;
@@ -914,19 +914,55 @@ import {
914
914
  } from "discord.js";
915
915
  var ButtonKit = class extends ButtonBuilder {
916
916
  #onClickHandler = null;
917
+ #onEndHandler = null;
917
918
  #contextData = null;
918
919
  #collector = null;
920
+ /**
921
+ * Sets up an inline interaction collector for this button. This collector by default allows as many interactions as possible if it is actively used.
922
+ * If unused, this expires after 24 hours or custom time if specified.
923
+ * @param handler The handler to run when the button is clicked
924
+ * @param data The context data to use for the interaction collector
925
+ * @returns This button
926
+ * @example
927
+ * ```ts
928
+ * const button = new ButtonKit()
929
+ * .setLabel('Click me')
930
+ * .setStyle(ButtonStyle.Primary)
931
+ * .setCustomId('click_me');
932
+ *
933
+ * const row = new ActionRowBuilder().addComponents(button);
934
+ *
935
+ * const message = await channel.send({ content: 'Click the button', components: [row] });
936
+ *
937
+ * button.onClick(async (interaction) => {
938
+ * await interaction.reply('You clicked me!');
939
+ * }, { message });
940
+ *
941
+ * // Remove onClick handler and destroy the interaction collector
942
+ * button.onClick(null);
943
+ * ```
944
+ */
919
945
  onClick(handler, data) {
920
946
  if (this.data.style === ButtonStyle.Link) {
921
947
  throw new TypeError('Cannot setup "onClick" handler on link buttons.');
922
948
  }
949
+ if (!handler) {
950
+ throw new TypeError('Cannot setup "onClick" without a handler function parameter.');
951
+ }
923
952
  this.#destroyCollector();
924
953
  this.#onClickHandler = handler;
925
- if (handler && data)
954
+ if (data)
926
955
  this.#contextData = data;
927
956
  this.#setupInteractionCollector();
928
957
  return this;
929
958
  }
959
+ onEnd(handler) {
960
+ if (!handler) {
961
+ throw new TypeError('Cannot setup "onEnd" without a handler function parameter.');
962
+ }
963
+ this.#onEndHandler = handler;
964
+ return this;
965
+ }
930
966
  #setupInteractionCollector() {
931
967
  if (!this.#contextData || !this.#onClickHandler)
932
968
  return;
@@ -963,10 +999,10 @@ var ButtonKit = class extends ButtonBuilder {
963
999
  });
964
1000
  this.#collector.on("end", () => {
965
1001
  this.#destroyCollector();
1002
+ this.#onEndHandler?.();
966
1003
  });
967
1004
  }
968
1005
  #destroyCollector() {
969
- this.#onClickHandler?.(null);
970
1006
  this.#collector?.stop("end");
971
1007
  this.#collector?.removeAllListeners();
972
1008
  this.#collector = null;
package/package.json CHANGED
@@ -1,58 +1,58 @@
1
1
  {
2
- "name": "commandkit",
3
- "description": "Beginner friendly command & event handler for Discord.js",
4
- "version": "0.1.8",
5
- "license": "MIT",
6
- "main": "./dist/index.js",
7
- "module": "./dist/index.mjs",
8
- "types": "./dist/index.d.ts",
9
- "bin": "./bin/index.mjs",
10
- "exports": {
11
- ".": {
12
- "require": "./dist/index.js",
13
- "import": "./dist/index.mjs",
14
- "types": "./dist/index.d.ts"
15
- }
16
- },
17
- "files": [
18
- "dist",
19
- "bin"
20
- ],
21
- "scripts": {
22
- "lint": "tsc",
23
- "dev": "tsup --watch",
24
- "build": "tsup",
25
- "deploy": "npm publish",
26
- "deploy-dev": "npm publish --access public --tag dev",
27
- "test": "cd ./tests && node ../bin/index.mjs dev",
28
- "test:build": "cd ./tests && node ../bin/index.mjs build",
29
- "test:prod": "cd ./tests && node ../bin/index.mjs start"
30
- },
31
- "repository": {
32
- "url": "git+https://github.com/underctrl-io/commandkit.git"
33
- },
34
- "homepage": "https://commandkit.js.org",
35
- "keywords": [
36
- "discord.js",
37
- "command handler",
38
- "event handler"
39
- ],
40
- "dependencies": {
41
- "commander": "^11.1.0",
42
- "dotenv": "^16.3.1",
43
- "ora": "^7.0.1",
44
- "rfdc": "^1.3.0",
45
- "rimraf": "^5.0.5",
46
- "tsup": "^7.2.0"
47
- },
48
- "devDependencies": {
49
- "@types/node": "^20.5.9",
50
- "discord.js": "^14.13.0",
51
- "tsconfig": "workspace:*",
52
- "tsx": "^3.12.8",
53
- "typescript": "^5.1.6"
54
- },
55
- "peerDependencies": {
56
- "discord.js": "^14"
2
+ "name": "commandkit",
3
+ "description": "Beginner friendly command & event handler for Discord.js",
4
+ "version": "0.1.9-dev.20231213080753",
5
+ "license": "MIT",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "bin": "./bin/index.mjs",
10
+ "exports": {
11
+ ".": {
12
+ "require": "./dist/index.js",
13
+ "import": "./dist/index.mjs",
14
+ "types": "./dist/index.d.ts"
57
15
  }
58
- }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "bin"
20
+ ],
21
+ "scripts": {
22
+ "lint": "tsc",
23
+ "dev": "tsup --watch",
24
+ "build": "tsup",
25
+ "deploy": "npm publish",
26
+ "deploy-dev": "npm publish --access public --tag dev",
27
+ "test": "cd ./tests && node ../bin/index.mjs dev",
28
+ "test:build": "cd ./tests && node ../bin/index.mjs build",
29
+ "test:prod": "cd ./tests && node ../bin/index.mjs start"
30
+ },
31
+ "repository": {
32
+ "url": "git+https://github.com/underctrl-io/commandkit.git"
33
+ },
34
+ "homepage": "https://commandkit.js.org",
35
+ "keywords": [
36
+ "discord.js",
37
+ "command handler",
38
+ "event handler"
39
+ ],
40
+ "dependencies": {
41
+ "commander": "^11.1.0",
42
+ "dotenv": "^16.3.1",
43
+ "ora": "^7.0.1",
44
+ "rfdc": "^1.3.0",
45
+ "rimraf": "^5.0.5",
46
+ "tsup": "^7.2.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^20.5.9",
50
+ "discord.js": "^14.13.0",
51
+ "tsconfig": "workspace:*",
52
+ "tsx": "^3.12.8",
53
+ "typescript": "^5.1.6"
54
+ },
55
+ "peerDependencies": {
56
+ "discord.js": "^14"
57
+ }
58
+ }