@moody-djs/prompts 1.0.2 → 1.0.4
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 +112 -5
- package/dist/index.d.ts +112 -5
- package/dist/index.js +209 -1
- package/dist/index.mjs +186 -1
- package/dist/v2/index.d.mts +95 -3
- package/dist/v2/index.d.ts +95 -3
- package/dist/v2/index.js +253 -1
- package/dist/v2/index.mjs +233 -1
- package/package.json +11 -1
- package/dist/Prompt.d.mts +0 -42
- package/dist/Prompt.d.ts +0 -42
- package/dist/Prompt.js +0 -1
- package/dist/Prompt.mjs +0 -1
- package/dist/types/prompt.types.d.mts +0 -67
- package/dist/types/prompt.types.d.ts +0 -67
- package/dist/types/prompt.types.js +0 -1
- package/dist/types/prompt.types.mjs +0 -1
- package/dist/types/util.types.d.mts +0 -3
- package/dist/types/util.types.d.ts +0 -3
- package/dist/types/util.types.js +0 -1
- package/dist/types/util.types.mjs +0 -0
- package/dist/util/errors.d.mts +0 -8
- package/dist/util/errors.d.ts +0 -8
- package/dist/util/errors.js +0 -1
- package/dist/util/errors.mjs +0 -1
- package/dist/v2/Prompt.d.mts +0 -46
- package/dist/v2/Prompt.d.ts +0 -46
- package/dist/v2/Prompt.js +0 -1
- package/dist/v2/Prompt.mjs +0 -1
- package/dist/v2/prompt.types.d.mts +0 -53
- package/dist/v2/prompt.types.d.ts +0 -53
- package/dist/v2/prompt.types.js +0 -1
- package/dist/v2/prompt.types.mjs +0 -1
package/dist/v2/index.d.mts
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as discord_js from 'discord.js';
|
|
2
|
+
import { RepliableInteraction, JSONEncodable, APIMessageTopLevelComponent, APIActionRowComponent, APIButtonComponentWithCustomId, ModalSubmitInteraction, ButtonInteraction, ModalBuilder, APIStringSelectComponent, StringSelectMenuInteraction, ButtonBuilder, StringSelectMenuBuilder } from 'discord.js';
|
|
3
|
+
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
type PromptContext<Context extends object, T = RepliableInteraction> = Context & {
|
|
6
|
+
interaction?: T;
|
|
7
|
+
previousStates: string[];
|
|
8
|
+
goBack: () => string;
|
|
9
|
+
};
|
|
10
|
+
/** A prompt state message callback */
|
|
11
|
+
interface PromptStateMessageCallback<T extends object> {
|
|
12
|
+
ephemeral: boolean;
|
|
13
|
+
components: JSONEncodable<APIPromptTopLevelComponent<T>>[] | ((ctx: PromptContext<T>) => MaybePromise<JSONEncodable<APIPromptTopLevelComponent<T>>[]>);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A type representing a prompt state that is possible
|
|
17
|
+
* @param T The type of the prompt context
|
|
18
|
+
*/
|
|
19
|
+
interface PromptState<T extends object> {
|
|
20
|
+
name: string;
|
|
21
|
+
timeout?: number;
|
|
22
|
+
onEntered?: (ctx: PromptContext<T>) => MaybePromise<string | undefined>;
|
|
23
|
+
message: PromptStateMessageCallback<T> | ((ctx: PromptContext<T>) => MaybePromise<PromptStateMessageCallback<T>>);
|
|
24
|
+
}
|
|
25
|
+
type PromptComponentCallback<Context extends object, Interaction> = string | ((ctx: PromptContext<Context, Interaction>, modalInteraction?: ModalSubmitInteraction) => MaybePromise<string | undefined>);
|
|
26
|
+
type APIPromptTopLevelComponent<T extends object> = APIMessageTopLevelComponent | APIActionRowComponent<APIPromptButtonComponent<T> | APIPromptSelectMenuComponent<T>>;
|
|
27
|
+
type APIPromptButtonComponent<T extends object> = APIButtonComponentWithCustomId & {
|
|
28
|
+
callback: PromptComponentCallback<T, ButtonInteraction>;
|
|
29
|
+
modal?: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>);
|
|
30
|
+
};
|
|
31
|
+
type APIPromptSelectMenuComponent<T extends object> = APIStringSelectComponent & {
|
|
32
|
+
callback: PromptComponentCallback<T, StringSelectMenuInteraction>;
|
|
33
|
+
modal?: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>);
|
|
34
|
+
};
|
|
35
|
+
type RegisterComponentsOptions<T extends object> = APIPromptTopLevelComponent<T> | APIPromptButtonComponent<T> | APIPromptSelectMenuComponent<T>;
|
|
36
|
+
declare class PromptButtonBuilder<Context extends object> extends ButtonBuilder {
|
|
37
|
+
callback?: PromptComponentCallback<Context, ButtonInteraction>;
|
|
38
|
+
modal?: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>);
|
|
39
|
+
setCallback(callback: PromptComponentCallback<Context, ButtonInteraction>): this;
|
|
40
|
+
getCallback(): PromptComponentCallback<Context, ButtonInteraction<discord_js.CacheType>>;
|
|
41
|
+
setModal(modal: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>)): this;
|
|
42
|
+
getModal(): ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>) | undefined;
|
|
43
|
+
}
|
|
44
|
+
declare class PromptSelectMenuBuilder<Context extends object> extends StringSelectMenuBuilder {
|
|
45
|
+
callback?: PromptComponentCallback<Context, StringSelectMenuInteraction>;
|
|
46
|
+
modal?: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>);
|
|
47
|
+
setCallback(callback: PromptComponentCallback<Context, StringSelectMenuInteraction>): this;
|
|
48
|
+
getCallback(): PromptComponentCallback<Context, StringSelectMenuInteraction<discord_js.CacheType>>;
|
|
49
|
+
setModal(modal: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>)): this;
|
|
50
|
+
getModal(): ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>) | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare class PromptError extends Error {
|
|
54
|
+
constructor(message: string);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A prompt that allows you to create a sequence of states that a user can go through
|
|
58
|
+
* with discord.js buttons and select menus.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
|
|
63
|
+
* await prompt.start(interaction);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare class Prompt<T extends object> {
|
|
67
|
+
initialState: string;
|
|
68
|
+
/** The current context of this prompt */
|
|
69
|
+
private context;
|
|
70
|
+
/** The current state */
|
|
71
|
+
private currentState?;
|
|
72
|
+
/** All components for this prompt */
|
|
73
|
+
private components;
|
|
74
|
+
/** The interaction collector */
|
|
75
|
+
private collector?;
|
|
76
|
+
/** The states represented in a map */
|
|
77
|
+
states: Map<string, PromptState<T>>;
|
|
78
|
+
/** Creates the prompt with a given set of states */
|
|
79
|
+
constructor(defaults: T, initialState: string, states: PromptState<T>[]);
|
|
80
|
+
/**
|
|
81
|
+
* Starts the prompt with a given interaction
|
|
82
|
+
* @param interaction The interaction starting off the sequence
|
|
83
|
+
* @returns {boolean} If true, the prompt started successfully
|
|
84
|
+
*/
|
|
85
|
+
start(interaction: RepliableInteraction): Promise<boolean>;
|
|
86
|
+
private changeState;
|
|
87
|
+
private handleCollect;
|
|
88
|
+
private prepareMessageOptions;
|
|
89
|
+
private getNewStateFromCallback;
|
|
90
|
+
private registerComponents;
|
|
91
|
+
private unwrap;
|
|
92
|
+
private replyOrEdit;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { type APIPromptButtonComponent, type APIPromptSelectMenuComponent, type APIPromptTopLevelComponent, Prompt, PromptButtonBuilder, type PromptContext, PromptError, PromptSelectMenuBuilder, type PromptState, type PromptStateMessageCallback, type RegisterComponentsOptions };
|
package/dist/v2/index.d.ts
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as discord_js from 'discord.js';
|
|
2
|
+
import { RepliableInteraction, JSONEncodable, APIMessageTopLevelComponent, APIActionRowComponent, APIButtonComponentWithCustomId, ModalSubmitInteraction, ButtonInteraction, ModalBuilder, APIStringSelectComponent, StringSelectMenuInteraction, ButtonBuilder, StringSelectMenuBuilder } from 'discord.js';
|
|
3
|
+
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
type PromptContext<Context extends object, T = RepliableInteraction> = Context & {
|
|
6
|
+
interaction?: T;
|
|
7
|
+
previousStates: string[];
|
|
8
|
+
goBack: () => string;
|
|
9
|
+
};
|
|
10
|
+
/** A prompt state message callback */
|
|
11
|
+
interface PromptStateMessageCallback<T extends object> {
|
|
12
|
+
ephemeral: boolean;
|
|
13
|
+
components: JSONEncodable<APIPromptTopLevelComponent<T>>[] | ((ctx: PromptContext<T>) => MaybePromise<JSONEncodable<APIPromptTopLevelComponent<T>>[]>);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A type representing a prompt state that is possible
|
|
17
|
+
* @param T The type of the prompt context
|
|
18
|
+
*/
|
|
19
|
+
interface PromptState<T extends object> {
|
|
20
|
+
name: string;
|
|
21
|
+
timeout?: number;
|
|
22
|
+
onEntered?: (ctx: PromptContext<T>) => MaybePromise<string | undefined>;
|
|
23
|
+
message: PromptStateMessageCallback<T> | ((ctx: PromptContext<T>) => MaybePromise<PromptStateMessageCallback<T>>);
|
|
24
|
+
}
|
|
25
|
+
type PromptComponentCallback<Context extends object, Interaction> = string | ((ctx: PromptContext<Context, Interaction>, modalInteraction?: ModalSubmitInteraction) => MaybePromise<string | undefined>);
|
|
26
|
+
type APIPromptTopLevelComponent<T extends object> = APIMessageTopLevelComponent | APIActionRowComponent<APIPromptButtonComponent<T> | APIPromptSelectMenuComponent<T>>;
|
|
27
|
+
type APIPromptButtonComponent<T extends object> = APIButtonComponentWithCustomId & {
|
|
28
|
+
callback: PromptComponentCallback<T, ButtonInteraction>;
|
|
29
|
+
modal?: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>);
|
|
30
|
+
};
|
|
31
|
+
type APIPromptSelectMenuComponent<T extends object> = APIStringSelectComponent & {
|
|
32
|
+
callback: PromptComponentCallback<T, StringSelectMenuInteraction>;
|
|
33
|
+
modal?: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>);
|
|
34
|
+
};
|
|
35
|
+
type RegisterComponentsOptions<T extends object> = APIPromptTopLevelComponent<T> | APIPromptButtonComponent<T> | APIPromptSelectMenuComponent<T>;
|
|
36
|
+
declare class PromptButtonBuilder<Context extends object> extends ButtonBuilder {
|
|
37
|
+
callback?: PromptComponentCallback<Context, ButtonInteraction>;
|
|
38
|
+
modal?: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>);
|
|
39
|
+
setCallback(callback: PromptComponentCallback<Context, ButtonInteraction>): this;
|
|
40
|
+
getCallback(): PromptComponentCallback<Context, ButtonInteraction<discord_js.CacheType>>;
|
|
41
|
+
setModal(modal: ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>)): this;
|
|
42
|
+
getModal(): ModalBuilder | ((interaction: ButtonInteraction) => MaybePromise<ModalBuilder | null>) | undefined;
|
|
43
|
+
}
|
|
44
|
+
declare class PromptSelectMenuBuilder<Context extends object> extends StringSelectMenuBuilder {
|
|
45
|
+
callback?: PromptComponentCallback<Context, StringSelectMenuInteraction>;
|
|
46
|
+
modal?: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>);
|
|
47
|
+
setCallback(callback: PromptComponentCallback<Context, StringSelectMenuInteraction>): this;
|
|
48
|
+
getCallback(): PromptComponentCallback<Context, StringSelectMenuInteraction<discord_js.CacheType>>;
|
|
49
|
+
setModal(modal: ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>)): this;
|
|
50
|
+
getModal(): ModalBuilder | ((interaction: StringSelectMenuInteraction) => MaybePromise<ModalBuilder | null>) | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare class PromptError extends Error {
|
|
54
|
+
constructor(message: string);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A prompt that allows you to create a sequence of states that a user can go through
|
|
58
|
+
* with discord.js buttons and select menus.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
|
|
63
|
+
* await prompt.start(interaction);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare class Prompt<T extends object> {
|
|
67
|
+
initialState: string;
|
|
68
|
+
/** The current context of this prompt */
|
|
69
|
+
private context;
|
|
70
|
+
/** The current state */
|
|
71
|
+
private currentState?;
|
|
72
|
+
/** All components for this prompt */
|
|
73
|
+
private components;
|
|
74
|
+
/** The interaction collector */
|
|
75
|
+
private collector?;
|
|
76
|
+
/** The states represented in a map */
|
|
77
|
+
states: Map<string, PromptState<T>>;
|
|
78
|
+
/** Creates the prompt with a given set of states */
|
|
79
|
+
constructor(defaults: T, initialState: string, states: PromptState<T>[]);
|
|
80
|
+
/**
|
|
81
|
+
* Starts the prompt with a given interaction
|
|
82
|
+
* @param interaction The interaction starting off the sequence
|
|
83
|
+
* @returns {boolean} If true, the prompt started successfully
|
|
84
|
+
*/
|
|
85
|
+
start(interaction: RepliableInteraction): Promise<boolean>;
|
|
86
|
+
private changeState;
|
|
87
|
+
private handleCollect;
|
|
88
|
+
private prepareMessageOptions;
|
|
89
|
+
private getNewStateFromCallback;
|
|
90
|
+
private registerComponents;
|
|
91
|
+
private unwrap;
|
|
92
|
+
private replyOrEdit;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { type APIPromptButtonComponent, type APIPromptSelectMenuComponent, type APIPromptTopLevelComponent, Prompt, PromptButtonBuilder, type PromptContext, PromptError, PromptSelectMenuBuilder, type PromptState, type PromptStateMessageCallback, type RegisterComponentsOptions };
|
package/dist/v2/index.js
CHANGED
|
@@ -1 +1,253 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/v2/index.ts
|
|
22
|
+
var v2_exports = {};
|
|
23
|
+
__export(v2_exports, {
|
|
24
|
+
Prompt: () => Prompt,
|
|
25
|
+
PromptButtonBuilder: () => PromptButtonBuilder,
|
|
26
|
+
PromptError: () => PromptError,
|
|
27
|
+
PromptSelectMenuBuilder: () => PromptSelectMenuBuilder
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(v2_exports);
|
|
30
|
+
|
|
31
|
+
// src/v2/Prompt.ts
|
|
32
|
+
var import_discord2 = require("discord.js");
|
|
33
|
+
|
|
34
|
+
// src/v2/prompt.types.ts
|
|
35
|
+
var import_discord = require("discord.js");
|
|
36
|
+
var PromptButtonBuilder = class extends import_discord.ButtonBuilder {
|
|
37
|
+
static {
|
|
38
|
+
__name(this, "PromptButtonBuilder");
|
|
39
|
+
}
|
|
40
|
+
callback;
|
|
41
|
+
modal;
|
|
42
|
+
setCallback(callback) {
|
|
43
|
+
this.callback = callback;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
getCallback() {
|
|
47
|
+
return this.callback;
|
|
48
|
+
}
|
|
49
|
+
setModal(modal) {
|
|
50
|
+
this.modal = modal;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
getModal() {
|
|
54
|
+
return this.modal;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var PromptSelectMenuBuilder = class extends import_discord.StringSelectMenuBuilder {
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "PromptSelectMenuBuilder");
|
|
60
|
+
}
|
|
61
|
+
callback;
|
|
62
|
+
modal;
|
|
63
|
+
setCallback(callback) {
|
|
64
|
+
this.callback = callback;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
getCallback() {
|
|
68
|
+
return this.callback;
|
|
69
|
+
}
|
|
70
|
+
setModal(modal) {
|
|
71
|
+
this.modal = modal;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
getModal() {
|
|
75
|
+
return this.modal;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// src/v2/Prompt.ts
|
|
80
|
+
var PromptError = class _PromptError extends Error {
|
|
81
|
+
static {
|
|
82
|
+
__name(this, "PromptError");
|
|
83
|
+
}
|
|
84
|
+
constructor(message) {
|
|
85
|
+
super(message);
|
|
86
|
+
Error.captureStackTrace?.(this, _PromptError);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var Prompt = class {
|
|
90
|
+
/** Creates the prompt with a given set of states */
|
|
91
|
+
constructor(defaults, initialState, states) {
|
|
92
|
+
this.initialState = initialState;
|
|
93
|
+
const previousStates = [];
|
|
94
|
+
const goBack = /* @__PURE__ */ __name(() => {
|
|
95
|
+
return this.context.previousStates.pop() ?? this.initialState;
|
|
96
|
+
}, "goBack");
|
|
97
|
+
this.context = { ...defaults, previousStates, goBack };
|
|
98
|
+
this.states = new Map(states.map((s) => [s.name, s]));
|
|
99
|
+
}
|
|
100
|
+
static {
|
|
101
|
+
__name(this, "Prompt");
|
|
102
|
+
}
|
|
103
|
+
/** The current context of this prompt */
|
|
104
|
+
context;
|
|
105
|
+
/** The current state */
|
|
106
|
+
currentState;
|
|
107
|
+
/** All components for this prompt */
|
|
108
|
+
components = new import_discord2.Collection();
|
|
109
|
+
/** The interaction collector */
|
|
110
|
+
collector;
|
|
111
|
+
/** The states represented in a map */
|
|
112
|
+
states;
|
|
113
|
+
/**
|
|
114
|
+
* Starts the prompt with a given interaction
|
|
115
|
+
* @param interaction The interaction starting off the sequence
|
|
116
|
+
* @returns {boolean} If true, the prompt started successfully
|
|
117
|
+
*/
|
|
118
|
+
async start(interaction) {
|
|
119
|
+
this.context.interaction = interaction;
|
|
120
|
+
return this.changeState(this.initialState, interaction);
|
|
121
|
+
}
|
|
122
|
+
async changeState(newState, interaction) {
|
|
123
|
+
const state = this.states.get(newState);
|
|
124
|
+
if (!state) return Promise.reject(new PromptError(`State ${newState} not found.`));
|
|
125
|
+
this.context.previousStates.push(this.currentState?.name ?? this.initialState);
|
|
126
|
+
this.currentState = state;
|
|
127
|
+
const shouldChangeState = await state.onEntered?.(this.context);
|
|
128
|
+
if (shouldChangeState) return this.changeState(shouldChangeState, interaction);
|
|
129
|
+
this.components.clear();
|
|
130
|
+
const messageOptions = await this.prepareMessageOptions(state);
|
|
131
|
+
const msg = await this.replyOrEdit(interaction, messageOptions);
|
|
132
|
+
if (this.collector) this.collector.stop();
|
|
133
|
+
this.collector = msg.createMessageComponentCollector({
|
|
134
|
+
time: state.timeout || 12e4,
|
|
135
|
+
filter: /* @__PURE__ */ __name((i) => i.user.id === interaction.user.id, "filter")
|
|
136
|
+
});
|
|
137
|
+
this.collector.on("collect", this.handleCollect.bind(this));
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
async handleCollect(interaction) {
|
|
141
|
+
const component = this.components.get(interaction.customId);
|
|
142
|
+
if (!component) throw new PromptError(`Couldn't find component with customId: ${interaction.customId}`);
|
|
143
|
+
if (component.modal) {
|
|
144
|
+
const modal = await this.unwrap(component.modal, interaction);
|
|
145
|
+
if (!modal) {
|
|
146
|
+
await interaction.deferUpdate();
|
|
147
|
+
this.collector?.stop();
|
|
148
|
+
this.context.interaction = interaction;
|
|
149
|
+
const newState3 = await this.getNewStateFromCallback(component) ?? this.currentState?.name;
|
|
150
|
+
if (!newState3) return interaction.deleteReply();
|
|
151
|
+
return void this.changeState(newState3, interaction);
|
|
152
|
+
}
|
|
153
|
+
const snowflake = import_discord2.SnowflakeUtil.generate().toString();
|
|
154
|
+
modal.setCustomId(snowflake);
|
|
155
|
+
await interaction.showModal(modal);
|
|
156
|
+
const response = await interaction.awaitModalSubmit({
|
|
157
|
+
time: 3e5,
|
|
158
|
+
filter: /* @__PURE__ */ __name((i) => i.user.id === interaction.user.id && i.customId === snowflake, "filter")
|
|
159
|
+
}).catch(() => null);
|
|
160
|
+
if (!response) return;
|
|
161
|
+
await response.deferUpdate();
|
|
162
|
+
this.collector?.stop();
|
|
163
|
+
this.context.interaction = response;
|
|
164
|
+
const newState2 = await this.getNewStateFromCallback(component, response) ?? this.currentState?.name;
|
|
165
|
+
if (!newState2) return interaction.deleteReply();
|
|
166
|
+
return void this.changeState(newState2, interaction);
|
|
167
|
+
}
|
|
168
|
+
await interaction.deferUpdate();
|
|
169
|
+
this.collector?.stop();
|
|
170
|
+
this.context.interaction = interaction;
|
|
171
|
+
const newState = await this.getNewStateFromCallback(component) ?? this.currentState?.name;
|
|
172
|
+
if (!newState) return interaction.deleteReply();
|
|
173
|
+
return void this.changeState(newState, interaction);
|
|
174
|
+
}
|
|
175
|
+
async prepareMessageOptions(state) {
|
|
176
|
+
const messageData = await this.unwrap(state.message, this.context);
|
|
177
|
+
const components = await this.unwrap(messageData.components, this.context);
|
|
178
|
+
this.registerComponents(components);
|
|
179
|
+
const messageOptions = {
|
|
180
|
+
components,
|
|
181
|
+
fetchReply: true,
|
|
182
|
+
flags: (messageData.ephemeral ? import_discord2.MessageFlags.Ephemeral : 0) | import_discord2.MessageFlags.IsComponentsV2
|
|
183
|
+
};
|
|
184
|
+
return messageOptions;
|
|
185
|
+
}
|
|
186
|
+
async getNewStateFromCallback(component, modalInteraction) {
|
|
187
|
+
if (!component.callback) return void 0;
|
|
188
|
+
if (typeof component.callback === "string") return component.callback;
|
|
189
|
+
if (component.modal) {
|
|
190
|
+
if (!modalInteraction) throw new PromptError("No modal submit interaction found when required.");
|
|
191
|
+
return component.callback(
|
|
192
|
+
this.context,
|
|
193
|
+
modalInteraction
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return component.callback(
|
|
197
|
+
this.context
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
registerComponents(components) {
|
|
201
|
+
for (const builder of components) {
|
|
202
|
+
if (builder instanceof PromptButtonBuilder) {
|
|
203
|
+
const id = import_discord2.SnowflakeUtil.generate().toString();
|
|
204
|
+
builder.setCustomId(id);
|
|
205
|
+
this.components.set(id, {
|
|
206
|
+
...builder.toJSON(),
|
|
207
|
+
custom_id: id,
|
|
208
|
+
callback: builder.getCallback(),
|
|
209
|
+
modal: builder.getModal()
|
|
210
|
+
});
|
|
211
|
+
} else if (builder instanceof PromptSelectMenuBuilder) {
|
|
212
|
+
const id = import_discord2.SnowflakeUtil.generate().toString();
|
|
213
|
+
builder.setCustomId(id);
|
|
214
|
+
this.components.set(id, {
|
|
215
|
+
...builder.toJSON(),
|
|
216
|
+
custom_id: id,
|
|
217
|
+
callback: builder.getCallback(),
|
|
218
|
+
modal: builder.getModal()
|
|
219
|
+
});
|
|
220
|
+
} else if (builder instanceof import_discord2.SectionBuilder && builder.accessory instanceof PromptButtonBuilder) {
|
|
221
|
+
const id = import_discord2.SnowflakeUtil.generate().toString();
|
|
222
|
+
builder.accessory.setCustomId(id);
|
|
223
|
+
this.components.set(id, {
|
|
224
|
+
...builder.accessory.toJSON(),
|
|
225
|
+
custom_id: id,
|
|
226
|
+
callback: builder.accessory.getCallback(),
|
|
227
|
+
modal: builder.accessory.getModal()
|
|
228
|
+
});
|
|
229
|
+
} else if ("components" in builder && Array.isArray(builder.components)) {
|
|
230
|
+
this.registerComponents(builder.components);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async unwrap(value, ...args) {
|
|
235
|
+
if (typeof value === "function") {
|
|
236
|
+
return value(...args);
|
|
237
|
+
}
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
async replyOrEdit(interaction, options) {
|
|
241
|
+
if (interaction.replied || interaction.deferred) {
|
|
242
|
+
return interaction.editReply({ components: options.components });
|
|
243
|
+
}
|
|
244
|
+
return interaction.reply(options);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
248
|
+
0 && (module.exports = {
|
|
249
|
+
Prompt,
|
|
250
|
+
PromptButtonBuilder,
|
|
251
|
+
PromptError,
|
|
252
|
+
PromptSelectMenuBuilder
|
|
253
|
+
});
|