oopscord.js 0.0.25 → 0.0.27

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 (39) hide show
  1. package/bun.lock +264 -0
  2. package/dist/structures/Constants.d.ts +2 -0
  3. package/dist/structures/Constants.d.ts.map +1 -1
  4. package/dist/structures/Constants.js +1 -0
  5. package/dist/structures/DiscordApp.d.ts +79 -2
  6. package/dist/structures/DiscordApp.d.ts.map +1 -1
  7. package/dist/structures/DiscordApp.js +215 -56
  8. package/dist/structures/components/BaseButton.d.ts +35 -1
  9. package/dist/structures/components/BaseButton.d.ts.map +1 -1
  10. package/dist/structures/components/BaseButton.js +70 -24
  11. package/dist/structures/components/BaseModal.d.ts +104 -6
  12. package/dist/structures/components/BaseModal.d.ts.map +1 -1
  13. package/dist/structures/components/BaseModal.js +161 -28
  14. package/dist/structures/components/BaseSelectMenu.d.ts +73 -3
  15. package/dist/structures/components/BaseSelectMenu.d.ts.map +1 -1
  16. package/dist/structures/components/BaseSelectMenu.js +117 -47
  17. package/dist/structures/decorators/Constants.d.ts +2 -0
  18. package/dist/structures/decorators/Constants.d.ts.map +1 -1
  19. package/dist/structures/decorators/Constants.js +3 -1
  20. package/dist/structures/decorators/DiscordModuleDecorator.d.ts +27 -0
  21. package/dist/structures/decorators/DiscordModuleDecorator.d.ts.map +1 -1
  22. package/dist/structures/decorators/DiscordModuleDecorator.js +27 -0
  23. package/dist/structures/decorators/MessageComponentDecorator.d.ts +73 -6
  24. package/dist/structures/decorators/MessageComponentDecorator.d.ts.map +1 -1
  25. package/dist/structures/decorators/MessageComponentDecorator.js +71 -3
  26. package/dist/utils/Util.d.ts +3 -0
  27. package/dist/utils/Util.d.ts.map +1 -1
  28. package/dist/utils/Util.js +15 -2
  29. package/package.json +4 -3
  30. package/dist/structures/Client.d.ts +0 -6
  31. package/dist/structures/Client.d.ts.map +0 -1
  32. package/dist/structures/Client.js +0 -43
  33. package/dist/structures/decorators/DiscordModule.d.ts +0 -20
  34. package/dist/structures/decorators/DiscordModule.d.ts.map +0 -1
  35. package/dist/structures/decorators/DiscordModule.js +0 -56
  36. package/dist/utils/InteractionUtil.d.ts +0 -5
  37. package/dist/utils/InteractionUtil.d.ts.map +0 -1
  38. package/dist/utils/InteractionUtil.js +0 -10
  39. package/tsconfig.json +0 -110
@@ -1,10 +1,108 @@
1
- import { ModalBuilder } from "discord.js";
1
+ import { ModalBuilder, ModalSubmitFields, ModalSubmitInteraction, RepliableInteraction } from "discord.js";
2
+ import { ModalOptions } from "..";
3
+ export type LabelValueOverride = {
4
+ type: 'label';
5
+ customId: string;
6
+ label?: string;
7
+ description?: string;
8
+ };
9
+ export type TextDisplayValueOverride = {
10
+ type: 'text_display';
11
+ customId: string;
12
+ content: string;
13
+ };
14
+ export type StringSelectValueOverride = {
15
+ type: 'string_select';
16
+ customId: string;
17
+ options: {
18
+ label: string;
19
+ value: string;
20
+ description?: string;
21
+ emoji?: string;
22
+ }[];
23
+ };
24
+ export type RadioGroupValueOverride = {
25
+ type: 'radio_group';
26
+ customId: string;
27
+ required?: boolean;
28
+ options: {
29
+ label: string;
30
+ value: string;
31
+ description?: string;
32
+ default?: boolean;
33
+ }[];
34
+ };
35
+ export type CheckboxGroupValueOverride = {
36
+ type: 'checkbox_group';
37
+ customId: string;
38
+ minValues?: number;
39
+ maxValues?: number;
40
+ required?: boolean;
41
+ options: {
42
+ label: string;
43
+ value: string;
44
+ description?: string;
45
+ default?: boolean;
46
+ }[];
47
+ };
48
+ export type CheckboxValueOverride = {
49
+ type: 'checkbox';
50
+ customId: string;
51
+ default?: boolean;
52
+ };
53
+ export type TextInputValueOverride = {
54
+ type: 'text_input';
55
+ customId: string;
56
+ value: string;
57
+ };
58
+ export type ModalValueOverride = StringSelectValueOverride | TextInputValueOverride | LabelValueOverride | TextDisplayValueOverride | RadioGroupValueOverride | CheckboxGroupValueOverride | CheckboxValueOverride;
59
+ /**
60
+ * The base class for modals created with the {@link Modal} decorator. This class is used to create a modal from the options and components defined in the class, and to override the default options and component values with new options and values when creating the modal.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import { ModalSubmitInteraction } from "discord.js";
65
+ * import { Modal, BaseModal, Interaction, Run } from "oopscord.js";
66
+ *
67
+ * @Modal({
68
+ * customId: "my_modal",
69
+ * title: "My Modal"
70
+ * })
71
+ * @ModalLabel({
72
+ * customId: "my_label",
73
+ * label: "This is a label",
74
+ * component: { type: "text_input", customId: "my_text_input", style: TextInputStyle.Short, placeholder: "Enter something...", required: true }
75
+ * })
76
+ * class MyModal extends BaseModal {
77
+ * constructor(@Interaction readonly interaction: ModalSubmitInteraction, @TextInputFieldInjection("my_text_input") readonly textInputValue: string) {
78
+ * super();
79
+ * }
80
+ *
81
+ * @Run
82
+ * async run() {
83
+ * await this.interaction.reply(`You entered: ${this.textInputValue}`);
84
+ * }
85
+ * }
86
+ * ```
87
+ */
2
88
  export declare class BaseModal {
3
- static modal: ModalBuilder | null;
89
+ private static modal;
90
+ private static options;
91
+ private static components;
92
+ /** Returns a new {@link ModalBuilder} that was created from the modal options and components. */
4
93
  static getModal(): ModalBuilder;
5
- static withDefaultValues(...values: {
6
- customId: string;
7
- value: string;
8
- }[]): ModalBuilder;
94
+ /**
95
+ * Returns a new {@link ModalBuilder} with the specified options and component values merged with the default options and components defined in the class.
96
+ *
97
+ * @param options The options to merge with the default options.
98
+ * @param values The component values to merge with the default components. The `customId` property is used to match the values with the correct components, and the `type` property is used to determine which type of component the value is for.
99
+ */
100
+ static override(options?: Partial<ModalOptions>, ...values: ModalValueOverride[]): ModalBuilder;
101
+ /**
102
+ * Shows modal to user and recieve the input.
103
+ * @param itr The interaction to show the modal to. Must be a command or button interaction.
104
+ * @returns The fields submitted by the user in the modal.
105
+ */
106
+ static getInput(itr: Exclude<RepliableInteraction, ModalSubmitInteraction>): Promise<ModalSubmitFields<import("discord.js").CacheType>>;
9
107
  }
10
108
  //# sourceMappingURL=BaseModal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseModal.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseModal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoD,YAAY,EAAoB,MAAM,YAAY,CAAC;AAG9G,qBAAa,SAAS;IAClB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAAQ;IAEzC,MAAM,CAAC,QAAQ;IAuBf,MAAM,CAAC,iBAAiB,CAAC,GAAG,MAAM,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE;CA8B5E"}
1
+ {"version":3,"file":"BaseModal.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseModal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsH,YAAY,EAAE,iBAAiB,EAAE,sBAAsB,EAAqB,oBAAoB,EAAiE,MAAM,YAAY,CAAC;AACjT,OAAO,EAAsF,YAAY,EAAE,MAAM,IAAI,CAAC;AAItH,MAAM,MAAM,kBAAkB,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAC1G,MAAM,MAAM,wBAAwB,GAAG;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAClG,MAAM,MAAM,yBAAyB,GAAG;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,CAAA;AACtK,MAAM,MAAM,uBAAuB,GAAG;IAAE,IAAI,E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C,MAAM,CAAC,UAAU,CAA4B;IAErD,iGAAiG;IACjG,MAAM,CAAC,QAAQ;IAmBf;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,OAAO,GAAE,OAAO,CAAC,YAAY,CAAM,EAAE,GAAG,MAAM,EAAE,kBAAkB,EAAE;IAqGpF;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,oBAAoB,EAAE,sBAAsB,CAAC;CA4B7E"}
@@ -1,50 +1,183 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.BaseModal = void 0;
4
7
  const discord_js_1 = require("discord.js");
5
8
  const __1 = require("..");
9
+ const node_crypto_1 = __importDefault(require("node:crypto"));
10
+ const Util_1 = require("../../utils/Util");
11
+ /**
12
+ * The base class for modals created with the {@link Modal} decorator. This class is used to create a modal from the options and components defined in the class, and to override the default options and component values with new options and values when creating the modal.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { ModalSubmitInteraction } from "discord.js";
17
+ * import { Modal, BaseModal, Interaction, Run } from "oopscord.js";
18
+ *
19
+ * @Modal({
20
+ * customId: "my_modal",
21
+ * title: "My Modal"
22
+ * })
23
+ * @ModalLabel({
24
+ * customId: "my_label",
25
+ * label: "This is a label",
26
+ * component: { type: "text_input", customId: "my_text_input", style: TextInputStyle.Short, placeholder: "Enter something...", required: true }
27
+ * })
28
+ * class MyModal extends BaseModal {
29
+ * constructor(@Interaction readonly interaction: ModalSubmitInteraction, @TextInputFieldInjection("my_text_input") readonly textInputValue: string) {
30
+ * super();
31
+ * }
32
+ *
33
+ * @Run
34
+ * async run() {
35
+ * await this.interaction.reply(`You entered: ${this.textInputValue}`);
36
+ * }
37
+ * }
38
+ * ```
39
+ */
6
40
  class BaseModal {
7
41
  static modal = null;
42
+ static options;
43
+ static components = [];
44
+ /** Returns a new {@link ModalBuilder} that was created from the modal options and components. */
8
45
  static getModal() {
9
46
  if (this.modal)
10
- return this.modal;
11
- const modal = new discord_js_1.ModalBuilder();
47
+ return discord_js_1.ModalBuilder.from(this.modal.toJSON());
12
48
  const options = Reflect.getMetadata(__1.MODAL_OPTIONS_KEY, this);
13
49
  const components = Reflect.getMetadata(__1.MODAL_COMPONENTS_KEY, this) ?? [];
14
- if (!options)
50
+ if (!options || !components)
15
51
  throw new TypeError("Failed to get the modal.");
16
52
  // Reverse the components because they are added to the modal in reverse order due to the way the decorators work.
17
53
  const reversedComponents = [...components].reverse();
18
- modal
19
- .setTitle(options.title)
20
- .setCustomId(options.customId);
21
- modal.components.push(...reversedComponents);
22
- this.modal = modal;
23
- return modal;
54
+ this.options = options;
55
+ this.components = [...reversedComponents];
56
+ this.modal = create(options, reversedComponents.map(c => c.component));
57
+ return discord_js_1.ModalBuilder.from(this.modal.toJSON());
24
58
  }
25
- static withDefaultValues(...values) {
26
- const modal = discord_js_1.ModalBuilder.from(this.getModal().toJSON());
27
- modal.components.push(...modal.components.map(row => {
28
- if (row instanceof discord_js_1.ActionRowBuilder) {
29
- const actionRow = discord_js_1.ActionRowBuilder.from(row);
30
- actionRow.setComponents(actionRow.components.map(component => {
31
- if (component instanceof discord_js_1.TextInputBuilder) {
32
- const matchingValue = values.find(v => v.customId === component.data.custom_id);
33
- if (matchingValue) {
34
- if ("value" in component.data) {
35
- const updatedComponent = discord_js_1.TextInputBuilder.from(component.toJSON());
36
- updatedComponent.setValue(matchingValue.value);
37
- return updatedComponent;
38
- }
59
+ /**
60
+ * Returns a new {@link ModalBuilder} with the specified options and component values merged with the default options and components defined in the class.
61
+ *
62
+ * @param options The options to merge with the default options.
63
+ * @param values The component values to merge with the default components. The `customId` property is used to match the values with the correct components, and the `type` property is used to determine which type of component the value is for.
64
+ */
65
+ static override(options = {}, ...values) {
66
+ const currentOptions = this.options ?? Reflect.getMetadata(__1.MODAL_OPTIONS_KEY, this);
67
+ const currentComponents = this.components.length > 0 ? this.components : Reflect.getMetadata(__1.MODAL_COMPONENTS_KEY, this)?.reverse();
68
+ if (!currentOptions)
69
+ throw new TypeError("Failed to get the modal options.");
70
+ const newOptions = Util_1.Util.mergeDefault(currentOptions, options);
71
+ const newComponents = currentComponents.map(c => {
72
+ const { customId, component } = c;
73
+ if (component instanceof discord_js_1.LabelBuilder) {
74
+ const updatedComponent = new discord_js_1.LabelBuilder(component.toJSON());
75
+ const matchingLabelValue = values.find(v => v.customId === customId && v.type === 'label');
76
+ if (matchingLabelValue) {
77
+ if (matchingLabelValue.label)
78
+ updatedComponent.setLabel(matchingLabelValue.label);
79
+ if (matchingLabelValue.description)
80
+ updatedComponent.setDescription(matchingLabelValue.description);
81
+ }
82
+ if (component.data.component instanceof discord_js_1.TextInputBuilder) {
83
+ const matchingTextInputValue = values.find(v => v.customId === component.data.component.data.custom_id && v.type === 'text_input');
84
+ if (matchingTextInputValue) {
85
+ if ("value" in component.data.component.data) {
86
+ const updatedTextInput = discord_js_1.TextInputBuilder.from(component.data.component.toJSON());
87
+ updatedTextInput.setValue(matchingTextInputValue.value);
88
+ updatedComponent.setTextInputComponent(updatedTextInput);
39
89
  }
40
90
  }
41
- return component;
42
- }));
43
- return actionRow;
91
+ }
92
+ else if (component.data.component instanceof discord_js_1.StringSelectMenuBuilder) {
93
+ const matchingStringSelectValue = values.find(v => v.customId === component.data.component.data.custom_id && v.type === 'string_select');
94
+ if (matchingStringSelectValue) {
95
+ const updatedComponent = discord_js_1.StringSelectMenuBuilder.from(component.data.component.toJSON());
96
+ updatedComponent.setOptions(matchingStringSelectValue.options);
97
+ component.setStringSelectMenuComponent(updatedComponent);
98
+ }
99
+ }
100
+ else if (component.data.component instanceof discord_js_1.RadioGroupBuilder) {
101
+ const matchingRadioGroupValue = values.find(v => v.customId === component.data.component.data.custom_id && v.type === 'radio_group');
102
+ if (matchingRadioGroupValue) {
103
+ const updatedComponent = new discord_js_1.RadioGroupBuilder(component.data.component.toJSON());
104
+ if ("required" in matchingRadioGroupValue)
105
+ updatedComponent.setRequired(matchingRadioGroupValue.required);
106
+ updatedComponent.setOptions(matchingRadioGroupValue.options);
107
+ component.setRadioGroupComponent(updatedComponent);
108
+ }
109
+ }
110
+ else if (component.data.component instanceof discord_js_1.CheckboxGroupBuilder) {
111
+ const matchingCheckboxGroupValue = values.find(v => v.customId === component.data.component.data.custom_id && v.type === 'checkbox_group');
112
+ if (matchingCheckboxGroupValue) {
113
+ const updatedComponent = new discord_js_1.CheckboxGroupBuilder(component.data.component.toJSON());
114
+ if (typeof matchingCheckboxGroupValue.minValues == "number")
115
+ updatedComponent.setMinValues(matchingCheckboxGroupValue.minValues);
116
+ if (typeof matchingCheckboxGroupValue.maxValues == "number")
117
+ updatedComponent.setMaxValues(matchingCheckboxGroupValue.maxValues);
118
+ if ("required" in matchingCheckboxGroupValue)
119
+ updatedComponent.setRequired(matchingCheckboxGroupValue.required);
120
+ updatedComponent.setOptions(matchingCheckboxGroupValue.options);
121
+ component.setCheckboxGroupComponent(updatedComponent);
122
+ }
123
+ }
124
+ else if (component.data.component instanceof discord_js_1.CheckboxBuilder) {
125
+ const matchingCheckboxValue = values.find(v => v.customId === component.data.component.data.custom_id && v.type === 'checkbox');
126
+ if (matchingCheckboxValue) {
127
+ const updatedComponent = new discord_js_1.CheckboxBuilder(component.data.component.toJSON());
128
+ if ("default" in matchingCheckboxValue)
129
+ updatedComponent.setDefault(matchingCheckboxValue.default);
130
+ component.setCheckboxComponent(updatedComponent);
131
+ }
132
+ }
133
+ return { customId, component: updatedComponent };
134
+ }
135
+ else if (component instanceof discord_js_1.TextDisplayBuilder) {
136
+ const updatedComponent = new discord_js_1.TextDisplayBuilder(component.toJSON());
137
+ const matchingTextDisplayValue = values.find(v => v.customId === customId && v.type === 'text_display');
138
+ if (matchingTextDisplayValue) {
139
+ updatedComponent.setContent(matchingTextDisplayValue.content);
140
+ }
141
+ return { customId, component: updatedComponent };
44
142
  }
45
- return row;
46
- }));
143
+ return { customId, component };
144
+ });
145
+ const modal = create(newOptions, [...newComponents].map(c => c.component));
47
146
  return modal;
48
147
  }
148
+ /**
149
+ * Shows modal to user and recieve the input.
150
+ * @param itr The interaction to show the modal to. Must be a command or button interaction.
151
+ * @returns The fields submitted by the user in the modal.
152
+ */
153
+ static getInput(itr) {
154
+ return new Promise((r, j) => {
155
+ if (itr.replied || itr.deferred)
156
+ return j("Interaction already replied.");
157
+ const id = node_crypto_1.default.createHash("md5")
158
+ .update(Math.floor(Math.random() * (99999999 - 10000000) + 10000000).toString())
159
+ .digest("hex");
160
+ itr.showModal(this.getModal().setCustomId(id));
161
+ let listener = async (itr) => {
162
+ if (!itr.isModalSubmit() || itr.customId != id)
163
+ return;
164
+ await itr.deferUpdate();
165
+ r(itr.fields);
166
+ itr.client.off("interactionCreate", listener);
167
+ };
168
+ itr.client.on("interactionCreate", listener);
169
+ setTimeout(() => {
170
+ itr.client.off("interactionCreate", listener);
171
+ j("Timed out");
172
+ }, 180 * 1000);
173
+ });
174
+ }
49
175
  }
50
176
  exports.BaseModal = BaseModal;
177
+ function create(options, components) {
178
+ const modal = new discord_js_1.ModalBuilder()
179
+ .setTitle(options.title)
180
+ .setCustomId(options.customId);
181
+ modal.components.push(...components);
182
+ return modal;
183
+ }
@@ -1,12 +1,82 @@
1
1
  import { ChannelSelectMenuBuilder, MentionableSelectMenuBuilder, RoleSelectMenuBuilder, StringSelectMenuBuilder, UserSelectMenuBuilder } from "discord.js";
2
+ import { ChannelSelectMenuOptions, MentionableSelectMenuOptions, RoleSelectMenuOptions, StringSelectMenuOptions, UserSelectMenuOptions } from "..";
3
+ /**
4
+ * The base class for select menus created with the {@link StringSelectMenu}, {@link UserSelectMenu}, {@link RoleSelectMenu}, {@link ChannelSelectMenu}, and {@link MentionableSelectMenu} decorators. This class is used to create a select menu from the options defined in the class, and to override the default options with new options when creating the select menu.
5
+ *
6
+ * @remarks
7
+ * The select menu type is determined by the decorator used to create the select menu class, and can be one of the following:
8
+ * - `string` for {@link StringSelectMenu}
9
+ * - `user` for {@link UserSelectMenu}
10
+ * - `role` for {@link RoleSelectMenu}
11
+ * - `channel` for {@link ChannelSelectMenu}
12
+ * - `mentionable` for {@link MentionableSelectMenu}
13
+ * @example
14
+ * ```ts
15
+ * import { StringSelectMenuInteraction } from "discord.js";
16
+ * import { StringSelectMenu, BaseSelectMenu, Interaction, Run } from "oopscord.js";
17
+ *
18
+ * @StringSelectMenu({
19
+ * customId: "my_select_menu",
20
+ * options: [
21
+ * { label: "Option 1", value: "option_1" },
22
+ * { label: "Option 2", value: "option_2" },
23
+ * { label: "Option 3", value: "option_3" }
24
+ * ]
25
+ * })
26
+ * class MySelectMenu extends BaseSelectMenu {
27
+ * constructor(@Interaction readonly interaction: StringSelectMenuInteraction) {
28
+ * super();
29
+ * }
30
+ *
31
+ * @Run
32
+ * async run() {
33
+ * await this.interaction.reply(`You selected: ${this.interaction.values.join(", ")}`);
34
+ * }
35
+ * }
36
+ * ```
37
+ */
2
38
  export declare class BaseSelectMenu {
3
- static selelctMenu: StringSelectMenuBuilder | UserSelectMenuBuilder | RoleSelectMenuBuilder | ChannelSelectMenuBuilder | MentionableSelectMenuBuilder | null;
39
+ private static genericSelectMenu;
40
+ private static options;
41
+ /** Returns a new {@link SelectMenuBuilder} that was created from the select menu options. */
4
42
  static getSelectMenu(): StringSelectMenuBuilder | UserSelectMenuBuilder | ChannelSelectMenuBuilder | RoleSelectMenuBuilder | MentionableSelectMenuBuilder;
5
- static withValues(...values: {
43
+ /**
44
+ * Returns a new {@link SelectMenuBuilder} with the specified options merged with the default options defined in the class, and the specified values merged with the default values defined in the class if the options include a `options` property (for StringSelectMenu) or `defaultUsers`/`defaultRoles`/`defaultChannels` property (for UserSelectMenu, RoleSelectMenu, ChannelSelectMenu, and MentionableSelectMenu).
45
+ *
46
+ * @param options The options to merge with the default options.
47
+ * @param values The component values to merge with the default components. The `customId` property is used to match the values with the correct components, and the `type` property is used to determine which type of component the value is for.
48
+ */
49
+ static override<K extends SelectMenuKind>(options?: Partial<OptionsFromKind<K>>, ...values: {
6
50
  label: string;
7
51
  value: string;
8
52
  description?: string;
9
53
  emoji?: string;
10
- }[]): StringSelectMenuBuilder;
54
+ }[]): BuilderFor<K>;
11
55
  }
56
+ interface SelectMenuMap {
57
+ string: {
58
+ builder: StringSelectMenuBuilder;
59
+ options: StringSelectMenuOptions;
60
+ };
61
+ user: {
62
+ builder: UserSelectMenuBuilder;
63
+ options: UserSelectMenuOptions;
64
+ };
65
+ role: {
66
+ builder: RoleSelectMenuBuilder;
67
+ options: RoleSelectMenuOptions;
68
+ };
69
+ channel: {
70
+ builder: ChannelSelectMenuBuilder;
71
+ options: ChannelSelectMenuOptions;
72
+ };
73
+ mentionable: {
74
+ builder: MentionableSelectMenuBuilder;
75
+ options: MentionableSelectMenuOptions;
76
+ };
77
+ }
78
+ type SelectMenuKind = keyof SelectMenuMap;
79
+ type OptionsFromKind<K extends SelectMenuKind> = SelectMenuMap[K]['options'];
80
+ type BuilderFor<K extends SelectMenuKind> = SelectMenuMap[K]['builder'];
81
+ export {};
12
82
  //# sourceMappingURL=BaseSelectMenu.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseSelectMenu.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseSelectMenu.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,wBAAwB,EAAiB,4BAA4B,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAGpM,qBAAa,cAAc;IACvB,MAAM,CAAC,WAAW,EAAE,uBAAuB,GAAG,qBAAqB,GAAG,qBAAqB,GAAG,wBAAwB,GAAG,4BAA4B,GAAG,IAAI,CAAQ;IAEpK,MAAM,CAAC,aAAa;IAkDpB,MAAM,CAAC,UAAU,CAAC,GAAG,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE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,EAAE;QAAE,OAAO,EAAE,wBAAwB,CAAC;QAAC,OAAO,EAAE,wBAAwB,CAAA;KAAE,CAAC;IAClF,WAAW,EAAE;QAAE,OAAO,EAAE,4BAA4B,CAAC;QAAC,OAAO,EAAE,4BAA4B,CAAA;KAAE,CAAC;CACjG;AAED,KAAK,cAAc,GAAG,MAAM,aAAa,CAAC;AAU1C,KAAK,eAAe,CAAC,CAAC,SAAS,cAAc,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC7E,KAAK,UAAU,CAAC,CAAC,SAAS,cAAc,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC"}
@@ -3,61 +3,131 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseSelectMenu = void 0;
4
4
  const discord_js_1 = require("discord.js");
5
5
  const __1 = require("..");
6
+ const Util_1 = require("../../utils/Util");
7
+ /**
8
+ * The base class for select menus created with the {@link StringSelectMenu}, {@link UserSelectMenu}, {@link RoleSelectMenu}, {@link ChannelSelectMenu}, and {@link MentionableSelectMenu} decorators. This class is used to create a select menu from the options defined in the class, and to override the default options with new options when creating the select menu.
9
+ *
10
+ * @remarks
11
+ * The select menu type is determined by the decorator used to create the select menu class, and can be one of the following:
12
+ * - `string` for {@link StringSelectMenu}
13
+ * - `user` for {@link UserSelectMenu}
14
+ * - `role` for {@link RoleSelectMenu}
15
+ * - `channel` for {@link ChannelSelectMenu}
16
+ * - `mentionable` for {@link MentionableSelectMenu}
17
+ * @example
18
+ * ```ts
19
+ * import { StringSelectMenuInteraction } from "discord.js";
20
+ * import { StringSelectMenu, BaseSelectMenu, Interaction, Run } from "oopscord.js";
21
+ *
22
+ * @StringSelectMenu({
23
+ * customId: "my_select_menu",
24
+ * options: [
25
+ * { label: "Option 1", value: "option_1" },
26
+ * { label: "Option 2", value: "option_2" },
27
+ * { label: "Option 3", value: "option_3" }
28
+ * ]
29
+ * })
30
+ * class MySelectMenu extends BaseSelectMenu {
31
+ * constructor(@Interaction readonly interaction: StringSelectMenuInteraction) {
32
+ * super();
33
+ * }
34
+ *
35
+ * @Run
36
+ * async run() {
37
+ * await this.interaction.reply(`You selected: ${this.interaction.values.join(", ")}`);
38
+ * }
39
+ * }
40
+ * ```
41
+ */
6
42
  class BaseSelectMenu {
7
- static selelctMenu = null;
43
+ static genericSelectMenu;
44
+ static options;
45
+ /** Returns a new {@link SelectMenuBuilder} that was created from the select menu options. */
8
46
  static getSelectMenu() {
9
- if (this.selelctMenu)
10
- return this.selelctMenu;
11
- let selectMenu;
47
+ if (this.genericSelectMenu)
48
+ return this.genericSelectMenu.copy();
12
49
  const type = Reflect.getMetadata(__1.SELECT_MENU_TYPE_KEY, this);
13
50
  const options = Reflect.getMetadata(__1.SELECT_MENU_OPTIONS_KEY, this);
14
51
  if (!options)
15
52
  throw new TypeError("Failed to get the select menu.");
16
- switch (type) {
17
- case "string":
18
- selectMenu = new discord_js_1.StringSelectMenuBuilder()
19
- .addOptions(options.options);
20
- break;
21
- case "user":
22
- selectMenu = new discord_js_1.UserSelectMenuBuilder()
23
- .setDefaultUsers(options.defaultUsers ?? []);
24
- break;
25
- case "role":
26
- selectMenu = new discord_js_1.RoleSelectMenuBuilder()
27
- .setDefaultRoles(options.defaultRoles ?? []);
28
- break;
29
- case "channel":
30
- selectMenu = new discord_js_1.ChannelSelectMenuBuilder()
31
- .setChannelTypes(options.channelTypes ?? [])
32
- .setDefaultChannels(options.defaultChannels ?? []);
33
- break;
34
- case "mentionable":
35
- const opts = options;
36
- selectMenu = new discord_js_1.MentionableSelectMenuBuilder()
37
- .addDefaultUsers(opts.defaultUsers ?? [])
38
- .addDefaultRoles(opts.defaultRoles ?? []);
39
- break;
40
- default:
41
- throw new TypeError("Invalid select menu type.");
53
+ this.genericSelectMenu = new GenericSelectMenu(type);
54
+ this.genericSelectMenu.create(options);
55
+ this.options = options;
56
+ return this.genericSelectMenu.copy();
57
+ }
58
+ /**
59
+ * Returns a new {@link SelectMenuBuilder} with the specified options merged with the default options defined in the class, and the specified values merged with the default values defined in the class if the options include a `options` property (for StringSelectMenu) or `defaultUsers`/`defaultRoles`/`defaultChannels` property (for UserSelectMenu, RoleSelectMenu, ChannelSelectMenu, and MentionableSelectMenu).
60
+ *
61
+ * @param options The options to merge with the default options.
62
+ * @param values The component values to merge with the default components. The `customId` property is used to match the values with the correct components, and the `type` property is used to determine which type of component the value is for.
63
+ */
64
+ static override(options = {}, ...values) {
65
+ if (!this.genericSelectMenu) {
66
+ this.getSelectMenu();
67
+ }
68
+ const currentOptions = (this.options ?? Reflect.getMetadata(__1.SELECT_MENU_OPTIONS_KEY, this));
69
+ if (!currentOptions)
70
+ throw new TypeError("Failed to get the select menu options.");
71
+ if (values.length > 0 && !this.genericSelectMenu.isStringSelect())
72
+ throw new TypeError("Values can only be overridden for StringSelectMenu.");
73
+ const newOptions = values.length > 0 ? Util_1.Util.mergeDefault(currentOptions, { ...options, options: values }) : Util_1.Util.mergeDefault(currentOptions, options);
74
+ return this.genericSelectMenu.create(newOptions);
75
+ }
76
+ }
77
+ exports.BaseSelectMenu = BaseSelectMenu;
78
+ const SelectMenuBuilders = {
79
+ string: discord_js_1.StringSelectMenuBuilder,
80
+ user: discord_js_1.UserSelectMenuBuilder,
81
+ role: discord_js_1.RoleSelectMenuBuilder,
82
+ channel: discord_js_1.ChannelSelectMenuBuilder,
83
+ mentionable: discord_js_1.MentionableSelectMenuBuilder,
84
+ };
85
+ class GenericSelectMenu {
86
+ kind;
87
+ selectMenu = null;
88
+ constructor(kind) {
89
+ this.kind = kind;
90
+ }
91
+ isStringSelect() { return this.kind === "string"; }
92
+ isUserSelect() { return this.kind === "user"; }
93
+ isRoleSelect() { return this.kind === "role"; }
94
+ isChannelSelect() { return this.kind === "channel"; }
95
+ isMentionableSelect() { return this.kind === "mentionable"; }
96
+ create(options) {
97
+ const Builder = SelectMenuBuilders[this.kind];
98
+ const menu = new Builder();
99
+ if (this.isStringSelect() && "options" in options) {
100
+ menu.addOptions(options.options);
42
101
  }
43
- selectMenu.setCustomId(options.customId);
44
- if ("placeholder" in options)
45
- selectMenu.setPlaceholder(options.placeholder);
102
+ else if (this.isUserSelect() && "defaultUsers" in options) {
103
+ menu.setDefaultUsers(options.defaultUsers ?? []);
104
+ }
105
+ else if (this.isRoleSelect() && "defaultRoles" in options) {
106
+ menu.setDefaultRoles(options.defaultRoles ?? []);
107
+ }
108
+ else if (this.isChannelSelect() && "channelTypes" in options) {
109
+ menu.setChannelTypes(options.channelTypes ?? [])
110
+ .setDefaultChannels(options.defaultChannels ?? []);
111
+ }
112
+ else if (this.isMentionableSelect() && "defaultUsers" in options && "defaultRoles" in options) {
113
+ menu.addDefaultUsers(options.defaultUsers ?? [])
114
+ .addDefaultRoles(options.defaultRoles ?? []);
115
+ }
116
+ menu.setCustomId(options.customId);
117
+ if (options.placeholder)
118
+ menu.setPlaceholder(options.placeholder);
46
119
  if (options.disabled)
47
- selectMenu.setDisabled(options.disabled);
48
- if (typeof options.minValues == 'number')
49
- selectMenu.setMinValues(options.minValues);
50
- if (typeof options.maxValues == 'number')
51
- selectMenu.setMaxValues(options.maxValues);
52
- this.selelctMenu = selectMenu;
53
- return selectMenu;
120
+ menu.setDisabled(options.disabled);
121
+ if (typeof options.minValues === 'number')
122
+ menu.setMinValues(options.minValues);
123
+ if (typeof options.maxValues === 'number')
124
+ menu.setMaxValues(options.maxValues);
125
+ this.selectMenu = menu;
126
+ return Builder.from(menu.toJSON());
54
127
  }
55
- static withValues(...values) {
56
- if (this.getSelectMenu().data.type != discord_js_1.ComponentType.StringSelect)
57
- throw new TypeError("withValues is only available for StringSelectMenu.");
58
- const selectMenu = discord_js_1.StringSelectMenuBuilder.from(this.getSelectMenu().toJSON());
59
- selectMenu.setOptions(values);
60
- return selectMenu;
128
+ copy() {
129
+ if (!this.selectMenu)
130
+ throw new TypeError("Select menu has not been created yet.");
131
+ return this.selectMenu.constructor.from(this.selectMenu.toJSON());
61
132
  }
62
133
  }
63
- exports.BaseSelectMenu = BaseSelectMenu;
@@ -20,10 +20,12 @@ export declare const MESSAGE_COMPONENT_MODULE_COMPONENTS_KEY: unique symbol;
20
20
  export declare const BUTTON_OPTIONS_KEY: unique symbol;
21
21
  export declare const SELECT_MENU_TYPE_KEY: unique symbol;
22
22
  export declare const SELECT_MENU_OPTIONS_KEY: unique symbol;
23
+ export declare const SELECT_MENU_VALUES_INDEX_KEY: unique symbol;
23
24
  export declare const MODAL_OPTIONS_KEY: unique symbol;
24
25
  export declare const MODAL_COMPONENTS_KEY: unique symbol;
25
26
  export declare const MODAL_TEXT_INPUT_VALUE_INDEX_KEY: unique symbol;
26
27
  export declare const MODAL_SELECT_MENU_VALUE_INDEX_KEY: unique symbol;
28
+ export declare const MODAL_FILE_UPLOAD_VALUE_INDEX_KEY: unique symbol;
27
29
  export declare enum ModuleType {
28
30
  COMMAND = 0,
29
31
  MESSAGE_COMPONENT = 1
@@ -1 +1 @@
1
- {"version":3,"file":"Constants.d.ts","sourceRoot":"","sources":["../../../src/structures/decorators/Constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,eAAiB,CAAC;AAE9C,eAAO,MAAM,2BAA2B,eAAqB,CAAC;AAE9D,eAAO,MAAM,iCAAiC,eAA8B,CAAC;AAE7E,eAAO,MAAM,0BAA0B,eAAuB,CAAC;AAC/D,eAAO,MAAM,+BAA+B,eAAwC,CAAC;AACrF,eAAO,MAAM,oBAAoB,eAAiB,CAAC;AAEnD,eAAO,MAAM,gBAAgB,eAAiB,CAAC;AAC/C,eAAO,MAAM,uBAAuB,eAAwB,CAAC;AAC7D,eAAO,MAAM,mBAAmB,eAAoB,CAAC;AACrD,eAAO,MAAM,yBAAyB,eAA0B,CAAC;AACjE,eAAO,MAAM,mBAAmB,eAAoB,CAAC;AACrD,eAAO,MAAM,6BAA6B,eAA8B,CAAC;AACzE,eAAO,MAAM,uBAAuB,eAAwB,CAAC;AAE7D,eAAO,MAAM,qBAAqB,eAA8B,CAAC;A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