oopscord.js 0.0.25 → 0.0.26
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/structures/Constants.d.ts +2 -0
- package/dist/structures/Constants.d.ts.map +1 -1
- package/dist/structures/Constants.js +1 -0
- package/dist/structures/DiscordApp.d.ts +79 -2
- package/dist/structures/DiscordApp.d.ts.map +1 -1
- package/dist/structures/DiscordApp.js +215 -56
- package/dist/structures/components/BaseButton.d.ts +35 -1
- package/dist/structures/components/BaseButton.d.ts.map +1 -1
- package/dist/structures/components/BaseButton.js +70 -24
- package/dist/structures/components/BaseModal.d.ts +75 -6
- package/dist/structures/components/BaseModal.d.ts.map +1 -1
- package/dist/structures/components/BaseModal.js +128 -28
- package/dist/structures/components/BaseModal.test.d.ts +2 -0
- package/dist/structures/components/BaseModal.test.d.ts.map +1 -0
- package/dist/structures/components/BaseModal.test.js +109 -0
- package/dist/structures/components/BaseSelectMenu.d.ts +73 -3
- package/dist/structures/components/BaseSelectMenu.d.ts.map +1 -1
- package/dist/structures/components/BaseSelectMenu.js +117 -47
- package/dist/structures/decorators/Constants.d.ts +2 -0
- package/dist/structures/decorators/Constants.d.ts.map +1 -1
- package/dist/structures/decorators/Constants.js +3 -1
- package/dist/structures/decorators/DiscordModuleDecorator.d.ts +27 -0
- package/dist/structures/decorators/DiscordModuleDecorator.d.ts.map +1 -1
- package/dist/structures/decorators/DiscordModuleDecorator.js +27 -0
- package/dist/structures/decorators/MessageComponentDecorator.d.ts +28 -2
- package/dist/structures/decorators/MessageComponentDecorator.d.ts.map +1 -1
- package/dist/structures/decorators/MessageComponentDecorator.js +19 -3
- package/dist/utils/Util.d.ts +3 -0
- package/dist/utils/Util.d.ts.map +1 -1
- package/dist/utils/Util.js +15 -2
- package/package.json +3 -2
|
@@ -3,39 +3,85 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseButton = 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 buttons created with the {@link Button} decorator. This class is used to create a button from the options defined in the class, and to override the default options with new options when creating the button.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { ButtonInteraction, ButtonStyle } from "discord.js";
|
|
13
|
+
* import { Button, BaseButton, Interaction, Run } from "oopscord.js";
|
|
14
|
+
*
|
|
15
|
+
* @Button({
|
|
16
|
+
* customId: "my_button",
|
|
17
|
+
* label: "Click me!",
|
|
18
|
+
* style: ButtonStyle.Primary
|
|
19
|
+
* })
|
|
20
|
+
* class MyButton extends BaseButton {
|
|
21
|
+
* constructor(@Interaction readonly interaction: ButtonInteraction) {
|
|
22
|
+
* super();
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @Run
|
|
26
|
+
* async run() {
|
|
27
|
+
* await this.interaction.reply("You clicked the button!");
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
6
32
|
class BaseButton {
|
|
33
|
+
static options;
|
|
7
34
|
static button = null;
|
|
35
|
+
/** Returns {@link ButtonBuilder} that was created from the button options. */
|
|
8
36
|
static getButton() {
|
|
9
37
|
if (this.button)
|
|
10
38
|
return this.button;
|
|
11
|
-
const button = new discord_js_1.ButtonBuilder();
|
|
12
39
|
const options = Reflect.getMetadata(__1.BUTTON_OPTIONS_KEY, this);
|
|
13
40
|
if (!options)
|
|
14
41
|
throw new TypeError("Failed to get the button.");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
.setStyle(options.style)
|
|
18
|
-
.setSKUId(options.skuId);
|
|
19
|
-
return button;
|
|
20
|
-
}
|
|
21
|
-
if (options.style == discord_js_1.ButtonStyle.Link) {
|
|
22
|
-
button
|
|
23
|
-
.setStyle(options.style)
|
|
24
|
-
.setURL(options.url);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
button
|
|
28
|
-
.setStyle(options.style)
|
|
29
|
-
.setCustomId(options.customId);
|
|
30
|
-
}
|
|
31
|
-
if (options.label)
|
|
32
|
-
button.setLabel(options.label);
|
|
33
|
-
if (options.emoji)
|
|
34
|
-
button.setEmoji(options.emoji);
|
|
35
|
-
if (options.disabled)
|
|
36
|
-
button.setDisabled(options.disabled);
|
|
42
|
+
this.options = options;
|
|
43
|
+
const button = create(options);
|
|
37
44
|
this.button = button;
|
|
38
|
-
|
|
45
|
+
// Return a copy of the button to prevent accidental mutations to the original button stored in the class.
|
|
46
|
+
return discord_js_1.ButtonBuilder.from(button.toJSON());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns a new {@link ButtonBuilder} with the specified options merged with the default options defined in the class.
|
|
50
|
+
*
|
|
51
|
+
* @param options The options to merge with the default options.
|
|
52
|
+
*/
|
|
53
|
+
static override(options) {
|
|
54
|
+
const currentOptions = this.options ?? Reflect.getMetadata(__1.BUTTON_OPTIONS_KEY, this);
|
|
55
|
+
if (!currentOptions)
|
|
56
|
+
throw new TypeError("Failed to get the button options.");
|
|
57
|
+
const newOptions = Util_1.Util.mergeDefault(currentOptions, options);
|
|
58
|
+
return create(newOptions);
|
|
39
59
|
}
|
|
40
60
|
}
|
|
41
61
|
exports.BaseButton = BaseButton;
|
|
62
|
+
function create(options) {
|
|
63
|
+
const button = new discord_js_1.ButtonBuilder();
|
|
64
|
+
if (options.style == discord_js_1.ButtonStyle.Premium) {
|
|
65
|
+
button
|
|
66
|
+
.setStyle(options.style)
|
|
67
|
+
.setSKUId(options.skuId);
|
|
68
|
+
return button;
|
|
69
|
+
}
|
|
70
|
+
if (options.style == discord_js_1.ButtonStyle.Link) {
|
|
71
|
+
button
|
|
72
|
+
.setStyle(options.style)
|
|
73
|
+
.setURL(options.url);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
button
|
|
77
|
+
.setStyle(options.style)
|
|
78
|
+
.setCustomId(options.customId);
|
|
79
|
+
}
|
|
80
|
+
if (options.label)
|
|
81
|
+
button.setLabel(options.label);
|
|
82
|
+
if (options.emoji)
|
|
83
|
+
button.setEmoji(options.emoji);
|
|
84
|
+
if (options.disabled)
|
|
85
|
+
button.setDisabled(options.disabled);
|
|
86
|
+
return button;
|
|
87
|
+
}
|
|
@@ -1,10 +1,79 @@
|
|
|
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
|
+
values: {
|
|
18
|
+
label: string;
|
|
19
|
+
value: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
emoji?: string;
|
|
22
|
+
}[];
|
|
23
|
+
};
|
|
24
|
+
export type TextInputValueOverride = {
|
|
25
|
+
type: 'text_input';
|
|
26
|
+
customId: string;
|
|
27
|
+
value: string;
|
|
28
|
+
};
|
|
29
|
+
export type ModalValueOverride = StringSelectValueOverride | TextInputValueOverride | LabelValueOverride | TextDisplayValueOverride;
|
|
30
|
+
/**
|
|
31
|
+
* 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.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { ModalSubmitInteraction } from "discord.js";
|
|
36
|
+
* import { Modal, BaseModal, Interaction, Run } from "oopscord.js";
|
|
37
|
+
*
|
|
38
|
+
* @Modal({
|
|
39
|
+
* customId: "my_modal",
|
|
40
|
+
* title: "My Modal"
|
|
41
|
+
* })
|
|
42
|
+
* @ModalLabel({
|
|
43
|
+
* customId: "my_label",
|
|
44
|
+
* label: "This is a label",
|
|
45
|
+
* component: { type: "text_input", customId: "my_text_input", style: TextInputStyle.Short, placeholder: "Enter something...", required: true }
|
|
46
|
+
* })
|
|
47
|
+
* class MyModal extends BaseModal {
|
|
48
|
+
* constructor(@Interaction readonly interaction: ModalSubmitInteraction, @TextInputFieldInjection("my_text_input") readonly textInputValue: string) {
|
|
49
|
+
* super();
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* @Run
|
|
53
|
+
* async run() {
|
|
54
|
+
* await this.interaction.reply(`You entered: ${this.textInputValue}`);
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
2
59
|
export declare class BaseModal {
|
|
3
|
-
static modal
|
|
60
|
+
private static modal;
|
|
61
|
+
private static options;
|
|
62
|
+
private static components;
|
|
63
|
+
/** Returns a new {@link ModalBuilder} that was created from the modal options and components. */
|
|
4
64
|
static getModal(): ModalBuilder;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Returns a new {@link ModalBuilder} with the specified options and component values merged with the default options and components defined in the class.
|
|
67
|
+
*
|
|
68
|
+
* @param options The options to merge with the default options.
|
|
69
|
+
* @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.
|
|
70
|
+
*/
|
|
71
|
+
static override(options?: Partial<ModalOptions>, ...values: ModalValueOverride[]): ModalBuilder;
|
|
72
|
+
/**
|
|
73
|
+
* Shows modal to user and recieve the input.
|
|
74
|
+
* @param itr The interaction to show the modal to. Must be a command or button interaction.
|
|
75
|
+
* @returns The fields submitted by the user in the modal.
|
|
76
|
+
*/
|
|
77
|
+
static getInput(itr: Exclude<RepliableInteraction, ModalSubmitInteraction>): Promise<ModalSubmitFields<import("discord.js").CacheType>>;
|
|
9
78
|
}
|
|
10
79
|
//# 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,
|
|
1
|
+
{"version":3,"file":"BaseModal.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseModal.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+E,YAAY,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,oBAAoB,EAAiE,MAAM,YAAY,CAAC;AACvP,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,MAAM,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;AACrK,MAAM,MAAM,sBAAsB,GAAG;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAC7F,MAAM,MAAM,kBAAkB,GAAG,yBAAyB,GAAG,sBAAsB,GAAG,kBAAkB,GAAG,wBAAwB,CAAC;AAEpI;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAA6B;IACjD,OAAO,CAAC,MAAM,CAAC,OAAO,CAAe;IACrC,OAAO,CAAC,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;IAiEpF;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,oBAAoB,EAAE,sBAAsB,CAAC;CA4B7E"}
|
|
@@ -1,50 +1,150 @@
|
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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.values);
|
|
97
|
+
component.setStringSelectMenuComponent(updatedComponent);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { customId, component: updatedComponent };
|
|
101
|
+
}
|
|
102
|
+
else if (component instanceof discord_js_1.TextDisplayBuilder) {
|
|
103
|
+
const updatedComponent = new discord_js_1.TextDisplayBuilder(component.toJSON());
|
|
104
|
+
const matchingTextDisplayValue = values.find(v => v.customId === customId && v.type === 'text_display');
|
|
105
|
+
if (matchingTextDisplayValue) {
|
|
106
|
+
updatedComponent.setContent(matchingTextDisplayValue.content);
|
|
107
|
+
}
|
|
108
|
+
return { customId, component: updatedComponent };
|
|
44
109
|
}
|
|
45
|
-
return
|
|
46
|
-
})
|
|
110
|
+
return { customId, component };
|
|
111
|
+
});
|
|
112
|
+
const modal = create(newOptions, [...newComponents].map(c => c.component));
|
|
47
113
|
return modal;
|
|
48
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Shows modal to user and recieve the input.
|
|
117
|
+
* @param itr The interaction to show the modal to. Must be a command or button interaction.
|
|
118
|
+
* @returns The fields submitted by the user in the modal.
|
|
119
|
+
*/
|
|
120
|
+
static getInput(itr) {
|
|
121
|
+
return new Promise((r, j) => {
|
|
122
|
+
if (itr.replied || itr.deferred)
|
|
123
|
+
return j("Interaction already replied.");
|
|
124
|
+
const id = node_crypto_1.default.createHash("md5")
|
|
125
|
+
.update(Math.floor(Math.random() * (99999999 - 10000000) + 10000000).toString())
|
|
126
|
+
.digest("hex");
|
|
127
|
+
itr.showModal(this.getModal().setCustomId(id));
|
|
128
|
+
let listener = async (itr) => {
|
|
129
|
+
if (!itr.isModalSubmit() || itr.customId != id)
|
|
130
|
+
return;
|
|
131
|
+
await itr.deferUpdate();
|
|
132
|
+
r(itr.fields);
|
|
133
|
+
itr.client.off("interactionCreate", listener);
|
|
134
|
+
};
|
|
135
|
+
itr.client.on("interactionCreate", listener);
|
|
136
|
+
setTimeout(() => {
|
|
137
|
+
itr.client.off("interactionCreate", listener);
|
|
138
|
+
j("Timed out");
|
|
139
|
+
}, 180 * 1000);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
49
142
|
}
|
|
50
143
|
exports.BaseModal = BaseModal;
|
|
144
|
+
function create(options, components) {
|
|
145
|
+
const modal = new discord_js_1.ModalBuilder()
|
|
146
|
+
.setTitle(options.title)
|
|
147
|
+
.setCustomId(options.customId);
|
|
148
|
+
modal.components.push(...components);
|
|
149
|
+
return modal;
|
|
150
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseModal.test.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseModal.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const vitest_1 = require("vitest");
|
|
10
|
+
const BaseModal_1 = require("./BaseModal");
|
|
11
|
+
const decorators_1 = require("../decorators");
|
|
12
|
+
let TestModal = class TestModal extends BaseModal_1.BaseModal {
|
|
13
|
+
};
|
|
14
|
+
TestModal = __decorate([
|
|
15
|
+
(0, decorators_1.Modal)({
|
|
16
|
+
title: "Test Modal",
|
|
17
|
+
customId: "test-modal"
|
|
18
|
+
})
|
|
19
|
+
], TestModal);
|
|
20
|
+
(0, vitest_1.describe)("TestModal.getInput", () => {
|
|
21
|
+
let mockInteraction;
|
|
22
|
+
let mockClient;
|
|
23
|
+
let listenerCallback;
|
|
24
|
+
(0, vitest_1.beforeEach)(() => {
|
|
25
|
+
mockClient = {
|
|
26
|
+
on: vitest_1.vi.fn((event, callback) => {
|
|
27
|
+
if (event === "interactionCreate") {
|
|
28
|
+
listenerCallback = callback;
|
|
29
|
+
}
|
|
30
|
+
}),
|
|
31
|
+
off: vitest_1.vi.fn(),
|
|
32
|
+
};
|
|
33
|
+
mockInteraction = {
|
|
34
|
+
replied: false,
|
|
35
|
+
deferred: false,
|
|
36
|
+
showModal: vitest_1.vi.fn(),
|
|
37
|
+
client: mockClient,
|
|
38
|
+
valueOf() {
|
|
39
|
+
return "";
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
vitest_1.vi.useFakeTimers();
|
|
43
|
+
});
|
|
44
|
+
(0, vitest_1.afterEach)(() => {
|
|
45
|
+
vitest_1.vi.clearAllMocks();
|
|
46
|
+
vitest_1.vi.useRealTimers();
|
|
47
|
+
});
|
|
48
|
+
(0, vitest_1.it)("should reject if interaction is already replied", async () => {
|
|
49
|
+
mockInteraction.replied = true;
|
|
50
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
51
|
+
await (0, vitest_1.expect)(promise).rejects.toBe("Interaction already replied.");
|
|
52
|
+
});
|
|
53
|
+
(0, vitest_1.it)("should reject if interaction is already deferred", async () => {
|
|
54
|
+
mockInteraction.deferred = true;
|
|
55
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
56
|
+
await (0, vitest_1.expect)(promise).rejects.toBe("Interaction already replied.");
|
|
57
|
+
});
|
|
58
|
+
(0, vitest_1.it)("should show modal with custom ID", async () => {
|
|
59
|
+
const itr = mockInteraction;
|
|
60
|
+
const promise = await TestModal.getInput(itr);
|
|
61
|
+
(0, vitest_1.expect)(itr.showModal).toHaveBeenCalled();
|
|
62
|
+
const modalArg = itr.showModal.mock.calls[0][0];
|
|
63
|
+
(0, vitest_1.expect)(modalArg.data.custom_id).toBeDefined();
|
|
64
|
+
});
|
|
65
|
+
(0, vitest_1.it)("should resolve with fields when modal submit interaction is received", async () => {
|
|
66
|
+
const mockFields = { getTextInputValue: vitest_1.vi.fn() };
|
|
67
|
+
const customId = "test-id";
|
|
68
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
69
|
+
const modalSubmitInteraction = {
|
|
70
|
+
isModalSubmit: () => true,
|
|
71
|
+
customId,
|
|
72
|
+
deferUpdate: vitest_1.vi.fn().mockResolvedValue(undefined),
|
|
73
|
+
fields: mockFields,
|
|
74
|
+
client: mockClient,
|
|
75
|
+
};
|
|
76
|
+
listenerCallback(modalSubmitInteraction);
|
|
77
|
+
const result = await promise;
|
|
78
|
+
(0, vitest_1.expect)(result).toBe(mockFields);
|
|
79
|
+
(0, vitest_1.expect)(modalSubmitInteraction.deferUpdate).toHaveBeenCalled();
|
|
80
|
+
(0, vitest_1.expect)(mockClient.off).toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
(0, vitest_1.it)("should reject with timeout message after 180 seconds", async () => {
|
|
83
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
84
|
+
vitest_1.vi.advanceTimersByTime(180000);
|
|
85
|
+
await (0, vitest_1.expect)(promise).rejects.toBe("Timed out");
|
|
86
|
+
(0, vitest_1.expect)(mockClient.off).toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
(0, vitest_1.it)("should ignore non-modal-submit interactions", async () => {
|
|
89
|
+
const mockFields = { getTextInputValue: vitest_1.vi.fn() };
|
|
90
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
91
|
+
const regularInteraction = {
|
|
92
|
+
isModalSubmit: () => false,
|
|
93
|
+
};
|
|
94
|
+
listenerCallback(regularInteraction);
|
|
95
|
+
vitest_1.vi.advanceTimersByTime(180000);
|
|
96
|
+
await (0, vitest_1.expect)(promise).rejects.toBe("Timed out");
|
|
97
|
+
});
|
|
98
|
+
(0, vitest_1.it)("should ignore modal submit with different custom ID", async () => {
|
|
99
|
+
const promise = TestModal.getInput(mockInteraction);
|
|
100
|
+
const wrongIdInteraction = {
|
|
101
|
+
isModalSubmit: () => true,
|
|
102
|
+
customId: "wrong-id",
|
|
103
|
+
client: mockClient,
|
|
104
|
+
};
|
|
105
|
+
listenerCallback(wrongIdInteraction);
|
|
106
|
+
vitest_1.vi.advanceTimersByTime(180000);
|
|
107
|
+
await (0, vitest_1.expect)(promise).rejects.toBe("Timed out");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -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
|
|
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
|
-
|
|
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
|
-
}[]):
|
|
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,
|
|
1
|
+
{"version":3,"file":"BaseSelectMenu.d.ts","sourceRoot":"","sources":["../../../src/structures/components/BaseSelectMenu.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,4BAA4B,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAC3J,OAAO,EAAyB,wBAAwB,EAAE,4BAA4B,EAAE,qBAAqB,EAAiD,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,IAAI,CAAC;AAGzN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAoC;IACpE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAwB;IAE9C,6FAA6F;IAC7F,MAAM,CAAC,aAAa;IAiBpB;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,cAAc,EAAE,OAAO,GAAE,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAM,EAAE,GAAG,MAAM,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,GAAG,UAAU,CAAC,CAAC,CAAC;CAe3L;AAED,UAAU,aAAa;IACnB,MAAM,EAAE;QAAE,OAAO,EAAE,uBAAuB,CAAC;QAAC,OAAO,EAAE,uBAAuB,CAAA;KAAE,CAAC;IAC/E,IAAI,EAAE;QAAE,OAAO,EAAE,qBAAqB,CAAC;QAAC,OAAO,EAAE,qBAAqB,CAAA;KAAE,CAAC;IACzE,IAAI,EAAE;QAAE,OAAO,EAAE,qBAAqB,CAAC;QAAC,OAAO,EAAE,qBAAqB,CAAA;KAAE,CAAC;IACzE,OAAO,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"}
|