@moody-djs/prompts 1.0.2 → 1.0.3

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/v2/index.mjs CHANGED
@@ -1 +1,235 @@
1
- var P=Object.defineProperty;var a=(i,t)=>P(i,"name",{value:t,configurable:!0});import{inspect as g}from"util";import{Collection as I,MessageFlags as C,SectionBuilder as y,SnowflakeUtil as d}from"discord.js";import{ButtonBuilder as S,StringSelectMenuBuilder as h}from"discord.js";var c=class extends S{static{a(this,"PromptButtonBuilder")}callback;modal;setCallback(t){return this.callback=t,this}getCallback(){return this.callback}setModal(t){return this.modal=t,this}getModal(){return this.modal}},u=class extends h{static{a(this,"PromptSelectMenuBuilder")}callback;modal;setCallback(t){return this.callback=t,this}getCallback(){return this.callback}setModal(t){return this.modal=t,this}getModal(){return this.modal}};var p=class i extends Error{static{a(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,i)}},b=class{constructor(t,e,o){this.initialState=e;let n=[],s=a(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:n,goBack:s},this.states=new Map(o.map(r=>[r.name,r]))}static{a(this,"Prompt")}context;currentState;components=new I;collector;states;async start(t){return this.context.interaction=t,this.changeState(this.initialState,t)}async changeState(t,e){let o=this.states.get(t);if(!o)return Promise.reject(new p(`State ${t} not found.`));this.context.previousStates.push(this.currentState?.name??this.initialState),this.currentState=o;let n=await o.onEntered?.(this.context);if(n)return this.changeState(n,e);this.components.clear();let s=await this.prepareMessageOptions(o),r=await this.replyOrEdit(e,s);return this.collector&&this.collector.stop(),this.collector=r.createMessageComponentCollector({time:o.timeout||12e4,filter:a(m=>m.user.id===e.user.id,"filter")}),this.collector.on("collect",this.handleCollect.bind(this)),!0}async handleCollect(t){let e=this.components.get(t.customId);if(!e)throw new p(`Couldn't find component with customId: ${t.customId}`);if(e.modal){let n=await this.unwrap(e.modal,t);if(!n){await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let l=await this.getNewStateFromCallback(e)??this.currentState?.name;return l?void this.changeState(l,t):t.deleteReply()}let s=d.generate().toString();n.setCustomId(s),await t.showModal(n);let r=await t.awaitModalSubmit({time:3e5,filter:a(l=>l.user.id===t.user.id&&l.customId===s,"filter")}).catch(()=>null);if(!r)return;await r.deferUpdate(),this.collector?.stop(),this.context.interaction=r;let m=await this.getNewStateFromCallback(e,r)??this.currentState?.name;return m?void this.changeState(m,t):t.deleteReply()}await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let o=await this.getNewStateFromCallback(e)??this.currentState?.name;if(!o)return t.deleteReply();this.changeState(o,t)}async prepareMessageOptions(t){let e=await this.unwrap(t.message,this.context),o=await this.unwrap(e.components,this.context);return this.registerComponents(o),console.log(g(o,{depth:null})),{components:o,fetchReply:!0,flags:(e.ephemeral?C.Ephemeral:0)|C.IsComponentsV2}}async getNewStateFromCallback(t,e){if(t.callback){if(typeof t.callback=="string")return t.callback;if(t.modal){if(!e)throw new p("No modal submit interaction found when required.");return t.callback(this.context,e)}return t.callback(this.context)}}registerComponents(t){for(let e of t)if(e instanceof c){let o=d.generate().toString();e.setCustomId(o),this.components.set(o,{...e.toJSON(),custom_id:o,callback:e.getCallback(),modal:e.getModal()})}else if(e instanceof u){let o=d.generate().toString();e.setCustomId(o),this.components.set(o,{...e.toJSON(),custom_id:o,callback:e.getCallback(),modal:e.getModal()})}else if(e instanceof y&&e.accessory instanceof c){let o=d.generate().toString();e.accessory.setCustomId(o),this.components.set(o,{...e.accessory.toJSON(),custom_id:o,callback:e.accessory.getCallback(),modal:e.accessory.getModal()})}else"components"in e&&Array.isArray(e.components)&&this.registerComponents(e.components)}async unwrap(t,...e){return typeof t=="function"?t(...e):t}async replyOrEdit(t,e){return t.replied||t.deferred?t.editReply({components:e.components}):t.reply(e)}};export{b as Prompt,c as PromptButtonBuilder,p as PromptError,u as PromptSelectMenuBuilder};
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/v2/Prompt.ts
5
+ import { inspect } from "util";
6
+ import {
7
+ Collection,
8
+ MessageFlags,
9
+ SectionBuilder,
10
+ SnowflakeUtil
11
+ } from "discord.js";
12
+
13
+ // src/v2/prompt.types.ts
14
+ import {
15
+ ButtonBuilder,
16
+ StringSelectMenuBuilder
17
+ } from "discord.js";
18
+ var PromptButtonBuilder = class extends ButtonBuilder {
19
+ static {
20
+ __name(this, "PromptButtonBuilder");
21
+ }
22
+ callback;
23
+ modal;
24
+ setCallback(callback) {
25
+ this.callback = callback;
26
+ return this;
27
+ }
28
+ getCallback() {
29
+ return this.callback;
30
+ }
31
+ setModal(modal) {
32
+ this.modal = modal;
33
+ return this;
34
+ }
35
+ getModal() {
36
+ return this.modal;
37
+ }
38
+ };
39
+ var PromptSelectMenuBuilder = class extends StringSelectMenuBuilder {
40
+ static {
41
+ __name(this, "PromptSelectMenuBuilder");
42
+ }
43
+ callback;
44
+ modal;
45
+ setCallback(callback) {
46
+ this.callback = callback;
47
+ return this;
48
+ }
49
+ getCallback() {
50
+ return this.callback;
51
+ }
52
+ setModal(modal) {
53
+ this.modal = modal;
54
+ return this;
55
+ }
56
+ getModal() {
57
+ return this.modal;
58
+ }
59
+ };
60
+
61
+ // src/v2/Prompt.ts
62
+ var PromptError = class _PromptError extends Error {
63
+ static {
64
+ __name(this, "PromptError");
65
+ }
66
+ constructor(message) {
67
+ super(message);
68
+ Error.captureStackTrace?.(this, _PromptError);
69
+ }
70
+ };
71
+ var Prompt = class {
72
+ /** Creates the prompt with a given set of states */
73
+ constructor(defaults, initialState, states) {
74
+ this.initialState = initialState;
75
+ const previousStates = [];
76
+ const goBack = /* @__PURE__ */ __name(() => {
77
+ return this.context.previousStates.pop() ?? this.initialState;
78
+ }, "goBack");
79
+ this.context = { ...defaults, previousStates, goBack };
80
+ this.states = new Map(states.map((s) => [s.name, s]));
81
+ }
82
+ static {
83
+ __name(this, "Prompt");
84
+ }
85
+ /** The current context of this prompt */
86
+ context;
87
+ /** The current state */
88
+ currentState;
89
+ /** All components for this prompt */
90
+ components = new Collection();
91
+ /** The interaction collector */
92
+ collector;
93
+ /** The states represented in a map */
94
+ states;
95
+ /**
96
+ * Starts the prompt with a given interaction
97
+ * @param interaction The interaction starting off the sequence
98
+ * @returns {boolean} If true, the prompt started successfully
99
+ */
100
+ async start(interaction) {
101
+ this.context.interaction = interaction;
102
+ return this.changeState(this.initialState, interaction);
103
+ }
104
+ async changeState(newState, interaction) {
105
+ const state = this.states.get(newState);
106
+ if (!state) return Promise.reject(new PromptError(`State ${newState} not found.`));
107
+ this.context.previousStates.push(this.currentState?.name ?? this.initialState);
108
+ this.currentState = state;
109
+ const shouldChangeState = await state.onEntered?.(this.context);
110
+ if (shouldChangeState) return this.changeState(shouldChangeState, interaction);
111
+ this.components.clear();
112
+ const messageOptions = await this.prepareMessageOptions(state);
113
+ const msg = await this.replyOrEdit(interaction, messageOptions);
114
+ if (this.collector) this.collector.stop();
115
+ this.collector = msg.createMessageComponentCollector({
116
+ time: state.timeout || 12e4,
117
+ filter: /* @__PURE__ */ __name((i) => i.user.id === interaction.user.id, "filter")
118
+ });
119
+ this.collector.on("collect", this.handleCollect.bind(this));
120
+ return true;
121
+ }
122
+ async handleCollect(interaction) {
123
+ const component = this.components.get(interaction.customId);
124
+ if (!component) throw new PromptError(`Couldn't find component with customId: ${interaction.customId}`);
125
+ if (component.modal) {
126
+ const modal = await this.unwrap(component.modal, interaction);
127
+ if (!modal) {
128
+ await interaction.deferUpdate();
129
+ this.collector?.stop();
130
+ this.context.interaction = interaction;
131
+ const newState3 = await this.getNewStateFromCallback(component) ?? this.currentState?.name;
132
+ if (!newState3) return interaction.deleteReply();
133
+ return void this.changeState(newState3, interaction);
134
+ }
135
+ const snowflake = SnowflakeUtil.generate().toString();
136
+ modal.setCustomId(snowflake);
137
+ await interaction.showModal(modal);
138
+ const response = await interaction.awaitModalSubmit({
139
+ time: 3e5,
140
+ filter: /* @__PURE__ */ __name((i) => i.user.id === interaction.user.id && i.customId === snowflake, "filter")
141
+ }).catch(() => null);
142
+ if (!response) return;
143
+ await response.deferUpdate();
144
+ this.collector?.stop();
145
+ this.context.interaction = response;
146
+ const newState2 = await this.getNewStateFromCallback(component, response) ?? this.currentState?.name;
147
+ if (!newState2) return interaction.deleteReply();
148
+ return void this.changeState(newState2, interaction);
149
+ }
150
+ await interaction.deferUpdate();
151
+ this.collector?.stop();
152
+ this.context.interaction = interaction;
153
+ const newState = await this.getNewStateFromCallback(component) ?? this.currentState?.name;
154
+ if (!newState) return interaction.deleteReply();
155
+ return void this.changeState(newState, interaction);
156
+ }
157
+ async prepareMessageOptions(state) {
158
+ const messageData = await this.unwrap(state.message, this.context);
159
+ const components = await this.unwrap(messageData.components, this.context);
160
+ this.registerComponents(components);
161
+ console.log(inspect(components, { depth: null }));
162
+ const messageOptions = {
163
+ components,
164
+ fetchReply: true,
165
+ flags: (messageData.ephemeral ? MessageFlags.Ephemeral : 0) | MessageFlags.IsComponentsV2
166
+ };
167
+ return messageOptions;
168
+ }
169
+ async getNewStateFromCallback(component, modalInteraction) {
170
+ if (!component.callback) return void 0;
171
+ if (typeof component.callback === "string") return component.callback;
172
+ if (component.modal) {
173
+ if (!modalInteraction) throw new PromptError("No modal submit interaction found when required.");
174
+ return component.callback(
175
+ this.context,
176
+ modalInteraction
177
+ );
178
+ }
179
+ return component.callback(
180
+ this.context
181
+ );
182
+ }
183
+ registerComponents(components) {
184
+ for (const builder of components) {
185
+ if (builder instanceof PromptButtonBuilder) {
186
+ const id = SnowflakeUtil.generate().toString();
187
+ builder.setCustomId(id);
188
+ this.components.set(id, {
189
+ ...builder.toJSON(),
190
+ custom_id: id,
191
+ callback: builder.getCallback(),
192
+ modal: builder.getModal()
193
+ });
194
+ } else if (builder instanceof PromptSelectMenuBuilder) {
195
+ const id = SnowflakeUtil.generate().toString();
196
+ builder.setCustomId(id);
197
+ this.components.set(id, {
198
+ ...builder.toJSON(),
199
+ custom_id: id,
200
+ callback: builder.getCallback(),
201
+ modal: builder.getModal()
202
+ });
203
+ } else if (builder instanceof SectionBuilder && builder.accessory instanceof PromptButtonBuilder) {
204
+ const id = SnowflakeUtil.generate().toString();
205
+ builder.accessory.setCustomId(id);
206
+ this.components.set(id, {
207
+ ...builder.accessory.toJSON(),
208
+ custom_id: id,
209
+ callback: builder.accessory.getCallback(),
210
+ modal: builder.accessory.getModal()
211
+ });
212
+ } else if ("components" in builder && Array.isArray(builder.components)) {
213
+ this.registerComponents(builder.components);
214
+ }
215
+ }
216
+ }
217
+ async unwrap(value, ...args) {
218
+ if (typeof value === "function") {
219
+ return value(...args);
220
+ }
221
+ return value;
222
+ }
223
+ async replyOrEdit(interaction, options) {
224
+ if (interaction.replied || interaction.deferred) {
225
+ return interaction.editReply({ components: options.components });
226
+ }
227
+ return interaction.reply(options);
228
+ }
229
+ };
230
+ export {
231
+ Prompt,
232
+ PromptButtonBuilder,
233
+ PromptError,
234
+ PromptSelectMenuBuilder
235
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moody-djs/prompts",
3
3
  "description": "A simple library for creating state based prompts in discord.js",
4
- "version": "1.0.2",
4
+ "version": "1.0.3",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "test": "tsc --noEmit --skipLibCheck",
@@ -29,6 +29,16 @@
29
29
  "import": "./dist/v2/index.mjs"
30
30
  }
31
31
  },
32
+ "typesVersions": {
33
+ "*": {
34
+ "v2": [
35
+ "./dist/v2/index.d.ts"
36
+ ],
37
+ "index": [
38
+ "./dist/index.d.ts"
39
+ ]
40
+ }
41
+ },
32
42
  "peerDependencies": {
33
43
  "discord.js": "latest"
34
44
  },
package/dist/Prompt.d.mts DELETED
@@ -1,42 +0,0 @@
1
- import { RepliableInteraction } from 'discord.js';
2
- import { PromptState } from './types/prompt.types.mjs';
3
- import './types/util.types.mjs';
4
-
5
- /**
6
- * A prompt that allows you to create a sequence of states that a user can go through
7
- * with discord.js buttons and select menus.
8
- *
9
- * @example
10
- * ```ts
11
- * const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
12
- * await prompt.start(interaction);
13
- * ```
14
- */
15
- declare class Prompt<T extends object> {
16
- initialState: string;
17
- /** The current context of this prompt */
18
- private context;
19
- /** The current state */
20
- private currentState?;
21
- /** All components for this prompt */
22
- private components;
23
- /** The interaction collector */
24
- private collector?;
25
- /** The states represented in a map */
26
- states: Map<string, PromptState<T>>;
27
- /** Creates the prompt with a given set of states */
28
- constructor(defaults: T, initialState: string, states: PromptState<T>[]);
29
- /**
30
- * Starts the prompt with a given interaction
31
- * @param interaction The interaction starting off the sequence
32
- * @returns {boolean} If true, the prompt started successfully
33
- */
34
- start(interaction: RepliableInteraction): Promise<boolean>;
35
- private changeState;
36
- private handleCollect;
37
- private prepareMessageOptions;
38
- private getNewStateFromCallback;
39
- private formatComponents;
40
- }
41
-
42
- export { Prompt };
package/dist/Prompt.d.ts DELETED
@@ -1,42 +0,0 @@
1
- import { RepliableInteraction } from 'discord.js';
2
- import { PromptState } from './types/prompt.types.js';
3
- import './types/util.types.js';
4
-
5
- /**
6
- * A prompt that allows you to create a sequence of states that a user can go through
7
- * with discord.js buttons and select menus.
8
- *
9
- * @example
10
- * ```ts
11
- * const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
12
- * await prompt.start(interaction);
13
- * ```
14
- */
15
- declare class Prompt<T extends object> {
16
- initialState: string;
17
- /** The current context of this prompt */
18
- private context;
19
- /** The current state */
20
- private currentState?;
21
- /** All components for this prompt */
22
- private components;
23
- /** The interaction collector */
24
- private collector?;
25
- /** The states represented in a map */
26
- states: Map<string, PromptState<T>>;
27
- /** Creates the prompt with a given set of states */
28
- constructor(defaults: T, initialState: string, states: PromptState<T>[]);
29
- /**
30
- * Starts the prompt with a given interaction
31
- * @param interaction The interaction starting off the sequence
32
- * @returns {boolean} If true, the prompt started successfully
33
- */
34
- start(interaction: RepliableInteraction): Promise<boolean>;
35
- private changeState;
36
- private handleCollect;
37
- private prepareMessageOptions;
38
- private getNewStateFromCallback;
39
- private formatComponents;
40
- }
41
-
42
- export { Prompt };
package/dist/Prompt.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var d=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var l=(i,t)=>d(i,"name",{value:t,configurable:!0});var b=(i,t)=>{for(var e in t)d(i,e,{get:t[e],enumerable:!0})},h=(i,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of g(t))!M.call(i,n)&&n!==e&&d(i,n,{get:()=>t[n],enumerable:!(o=x(t,n))||o.enumerable});return i};var C=i=>h(d({},"__esModule",{value:!0}),i);var y={};b(y,{Prompt:()=>S});module.exports=C(y);var c=require("discord.js");var m=class i extends Error{static{l(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,i)}};var S=class{constructor(t,e,o){this.initialState=e;let n=[],r=l(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:n,goBack:r},this.states=new Map(o.map(a=>[a.name,a]))}static{l(this,"Prompt")}context;currentState;components=new c.Collection;collector;states;async start(t){return this.context.interaction=t,this.changeState(this.initialState,t)}async changeState(t,e){let o=this.states.get(t);if(!o)return Promise.reject(new m(`State ${t} not found.`));this.context.previousStates.push(this.currentState?.name??this.initialState),this.currentState=o;let n=await o.onEntered?.(this.context);if(n)return this.changeState(n,e);let r=await this.prepareMessageOptions(o),a=e.replied||e.deferred?await e.editReply({embeds:r.embeds,components:r.components,content:r.content}):await e.reply(r);return this.collector&&this.collector.stop(),this.collector=a.createMessageComponentCollector({time:o.timeout||12e4,filter:l(s=>s.user.id===e.user.id,"filter")}),this.collector.on("collect",this.handleCollect.bind(this)),!0}async handleCollect(t){let e=this.components.get(t.customId);if(!e)throw new m(`Couldn't find component with customId: ${t.customId}`);if(e.type===1||e.type===3){let n=typeof e.component=="function"?await e.component(this.context):e.component,r=typeof n.modal=="function"?await n.modal(t):n.modal;if(!r){await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let u=await this.getNewStateFromCallback(e)??this.currentState?.name;return u?void this.changeState(u,t):t.deleteReply()}let a=c.SnowflakeUtil.generate().toString();r.setCustomId(a),await t.showModal(r);let s=await t.awaitModalSubmit({time:3e5,filter:l(u=>u.user.id===t.user.id&&u.customId===a,"filter")}).catch(()=>null);if(!s)return;await s.deferUpdate(),this.collector?.stop(),this.context.interaction=s;let p=await this.getNewStateFromCallback(e,s)??this.currentState?.name;return p?void this.changeState(p,t):t.deleteReply()}await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let o=await this.getNewStateFromCallback(e)??this.currentState?.name;if(!o)return t.deleteReply();this.changeState(o,t)}async prepareMessageOptions(t){let e=typeof t.message=="object"?t.message:await t.message(this.context);return{components:await this.formatComponents(e),fetchReply:!0,flags:e.ephemeral?c.MessageFlags.Ephemeral:0,content:typeof e.content=="function"?await e.content?.(this.context):e.content,embeds:Array.isArray(e.embeds)?e.embeds:await e.embeds(this.context)}}async getNewStateFromCallback(t,e){if(typeof t.callback=="string")return t.callback;if(t.type===1||t.type===3){if(!e)throw new m("No modal submit interaction found when required.");return t.callback(this.context,e)}return t.callback(this.context)}async formatComponents(t){let e=[],o=Array.isArray(t.components)?t.components:await t.components(this.context);this.components.clear();for(let n of o){let r=new c.ActionRowBuilder;for(let a of n){let s=typeof a.component=="function"?await a.component(this.context):a.component,p=c.SnowflakeUtil.generate().toString();"button"in s?(s.button.setCustomId(p),this.components.set(p,a),r.addComponents(s.button)):(s.setCustomId(p),this.components.set(p,a),r.addComponents(s))}e.push(r)}return e}};0&&(module.exports={Prompt});
package/dist/Prompt.mjs DELETED
@@ -1 +0,0 @@
1
- var S=Object.defineProperty;var c=(m,t)=>S(m,"name",{value:t,configurable:!0});import{ActionRowBuilder as x,Collection as g,MessageFlags as M,SnowflakeUtil as u}from"discord.js";var p=class m extends Error{static{c(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,m)}};var d=class{constructor(t,e,o){this.initialState=e;let i=[],n=c(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:i,goBack:n},this.states=new Map(o.map(r=>[r.name,r]))}static{c(this,"Prompt")}context;currentState;components=new g;collector;states;async start(t){return this.context.interaction=t,this.changeState(this.initialState,t)}async changeState(t,e){let o=this.states.get(t);if(!o)return Promise.reject(new p(`State ${t} not found.`));this.context.previousStates.push(this.currentState?.name??this.initialState),this.currentState=o;let i=await o.onEntered?.(this.context);if(i)return this.changeState(i,e);let n=await this.prepareMessageOptions(o),r=e.replied||e.deferred?await e.editReply({embeds:n.embeds,components:n.components,content:n.content}):await e.reply(n);return this.collector&&this.collector.stop(),this.collector=r.createMessageComponentCollector({time:o.timeout||12e4,filter:c(a=>a.user.id===e.user.id,"filter")}),this.collector.on("collect",this.handleCollect.bind(this)),!0}async handleCollect(t){let e=this.components.get(t.customId);if(!e)throw new p(`Couldn't find component with customId: ${t.customId}`);if(e.type===1||e.type===3){let i=typeof e.component=="function"?await e.component(this.context):e.component,n=typeof i.modal=="function"?await i.modal(t):i.modal;if(!n){await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let l=await this.getNewStateFromCallback(e)??this.currentState?.name;return l?void this.changeState(l,t):t.deleteReply()}let r=u.generate().toString();n.setCustomId(r),await t.showModal(n);let a=await t.awaitModalSubmit({time:3e5,filter:c(l=>l.user.id===t.user.id&&l.customId===r,"filter")}).catch(()=>null);if(!a)return;await a.deferUpdate(),this.collector?.stop(),this.context.interaction=a;let s=await this.getNewStateFromCallback(e,a)??this.currentState?.name;return s?void this.changeState(s,t):t.deleteReply()}await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let o=await this.getNewStateFromCallback(e)??this.currentState?.name;if(!o)return t.deleteReply();this.changeState(o,t)}async prepareMessageOptions(t){let e=typeof t.message=="object"?t.message:await t.message(this.context);return{components:await this.formatComponents(e),fetchReply:!0,flags:e.ephemeral?M.Ephemeral:0,content:typeof e.content=="function"?await e.content?.(this.context):e.content,embeds:Array.isArray(e.embeds)?e.embeds:await e.embeds(this.context)}}async getNewStateFromCallback(t,e){if(typeof t.callback=="string")return t.callback;if(t.type===1||t.type===3){if(!e)throw new p("No modal submit interaction found when required.");return t.callback(this.context,e)}return t.callback(this.context)}async formatComponents(t){let e=[],o=Array.isArray(t.components)?t.components:await t.components(this.context);this.components.clear();for(let i of o){let n=new x;for(let r of i){let a=typeof r.component=="function"?await r.component(this.context):r.component,s=u.generate().toString();"button"in a?(a.button.setCustomId(s),this.components.set(s,r),n.addComponents(a.button)):(a.setCustomId(s),this.components.set(s,r),n.addComponents(a))}e.push(n)}return e}};export{d as Prompt};
@@ -1,67 +0,0 @@
1
- import { RepliableInteraction, EmbedBuilder, ButtonInteraction, ButtonBuilder, StringSelectMenuInteraction, StringSelectMenuBuilder, ModalSubmitInteraction, ModalBuilder } from 'discord.js';
2
- import { MaybePromise } from './util.types.mjs';
3
-
4
- /** An enum containing the prompt component types that are supported */
5
- declare enum PromptComponentType {
6
- Button = 0,
7
- ModalButton = 1,
8
- SelectMenu = 2,
9
- ModalSelectMenu = 3
10
- }
11
- type PromptContext<Context extends object, T = RepliableInteraction> = Context & {
12
- interaction?: T;
13
- previousStates: string[];
14
- goBack: () => string;
15
- genericError?: string;
16
- };
17
- interface ModalComponentReturnType<T, Interaction> {
18
- button: T;
19
- modal: ModalBuilder | ((interaction: Interaction) => MaybePromise<ModalBuilder | null>);
20
- }
21
- interface PromptStateComponentBase<Context extends object, Interaction = RepliableInteraction> {
22
- callback: string | ((ctx: PromptContext<Context, Interaction>) => MaybePromise<string | undefined>);
23
- }
24
- interface PromptStateComponentBaseWithInteraction<Context extends object, Interaction = RepliableInteraction, AlternateInteraction = Interaction> {
25
- callback: string | ((ctx: PromptContext<Context, Interaction>, interaction: AlternateInteraction) => MaybePromise<string | undefined>);
26
- }
27
- /** A prompt state that uses a button component */
28
- interface PromptStateButtonComponent<T extends object> extends PromptStateComponentBase<T, ButtonInteraction> {
29
- type: PromptComponentType.Button;
30
- component: ButtonBuilder | ((ctx: PromptContext<T>) => MaybePromise<ButtonBuilder>);
31
- }
32
- /** A prompt state that contains a select menu */
33
- interface PromptStateSelectMenuComponent<T extends object> extends PromptStateComponentBase<T, StringSelectMenuInteraction> {
34
- type: PromptComponentType.SelectMenu;
35
- component: StringSelectMenuBuilder | ((ctx: PromptContext<T>) => MaybePromise<StringSelectMenuBuilder>);
36
- }
37
- /** A prompt state that uses a modal that is opened by a button */
38
- interface PromptStateModalButtonComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, ButtonInteraction, ModalSubmitInteraction> {
39
- type: PromptComponentType.ModalButton;
40
- component: ModalComponentReturnType<ButtonBuilder, ButtonInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<ButtonBuilder, ButtonInteraction>>);
41
- }
42
- /** A prompt state that is a modal opened by a select menu component */
43
- interface PromptStateModalSelectMenuComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, StringSelectMenuInteraction, ModalSubmitInteraction | null> {
44
- type: PromptComponentType.ModalSelectMenu;
45
- component: ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction>>);
46
- }
47
- /** A type representing a prompt state component, used when defining the components added for a prompt state */
48
- type PromptStateComponent<T extends object> = PromptStateButtonComponent<T> | PromptStateSelectMenuComponent<T> | PromptStateModalSelectMenuComponent<T> | PromptStateModalButtonComponent<T>;
49
- /** A prompt state message callback */
50
- interface PromptStateMessageCallback<T extends object> {
51
- ephemeral: boolean;
52
- content?: string | ((ctx: PromptContext<T>) => MaybePromise<string | undefined>);
53
- embeds: EmbedBuilder[] | ((ctx: PromptContext<T>) => MaybePromise<EmbedBuilder[]>);
54
- components: PromptStateComponent<T>[][] | ((ctx: PromptContext<T>) => MaybePromise<PromptStateComponent<T>[][]>);
55
- }
56
- /**
57
- * A type representing a prompt state that is possible
58
- * @param T The type of the prompt context
59
- */
60
- interface PromptState<T extends object> {
61
- name: string;
62
- timeout?: number;
63
- onEntered?: (ctx: PromptContext<T>) => MaybePromise<string | undefined>;
64
- message: PromptStateMessageCallback<T> | ((ctx: PromptContext<T>) => MaybePromise<PromptStateMessageCallback<T>>);
65
- }
66
-
67
- export { PromptComponentType, type PromptContext, type PromptState, type PromptStateButtonComponent, type PromptStateComponent, type PromptStateComponentBase, type PromptStateComponentBaseWithInteraction, type PromptStateMessageCallback, type PromptStateModalButtonComponent, type PromptStateModalSelectMenuComponent, type PromptStateSelectMenuComponent };
@@ -1,67 +0,0 @@
1
- import { RepliableInteraction, EmbedBuilder, ButtonInteraction, ButtonBuilder, StringSelectMenuInteraction, StringSelectMenuBuilder, ModalSubmitInteraction, ModalBuilder } from 'discord.js';
2
- import { MaybePromise } from './util.types.js';
3
-
4
- /** An enum containing the prompt component types that are supported */
5
- declare enum PromptComponentType {
6
- Button = 0,
7
- ModalButton = 1,
8
- SelectMenu = 2,
9
- ModalSelectMenu = 3
10
- }
11
- type PromptContext<Context extends object, T = RepliableInteraction> = Context & {
12
- interaction?: T;
13
- previousStates: string[];
14
- goBack: () => string;
15
- genericError?: string;
16
- };
17
- interface ModalComponentReturnType<T, Interaction> {
18
- button: T;
19
- modal: ModalBuilder | ((interaction: Interaction) => MaybePromise<ModalBuilder | null>);
20
- }
21
- interface PromptStateComponentBase<Context extends object, Interaction = RepliableInteraction> {
22
- callback: string | ((ctx: PromptContext<Context, Interaction>) => MaybePromise<string | undefined>);
23
- }
24
- interface PromptStateComponentBaseWithInteraction<Context extends object, Interaction = RepliableInteraction, AlternateInteraction = Interaction> {
25
- callback: string | ((ctx: PromptContext<Context, Interaction>, interaction: AlternateInteraction) => MaybePromise<string | undefined>);
26
- }
27
- /** A prompt state that uses a button component */
28
- interface PromptStateButtonComponent<T extends object> extends PromptStateComponentBase<T, ButtonInteraction> {
29
- type: PromptComponentType.Button;
30
- component: ButtonBuilder | ((ctx: PromptContext<T>) => MaybePromise<ButtonBuilder>);
31
- }
32
- /** A prompt state that contains a select menu */
33
- interface PromptStateSelectMenuComponent<T extends object> extends PromptStateComponentBase<T, StringSelectMenuInteraction> {
34
- type: PromptComponentType.SelectMenu;
35
- component: StringSelectMenuBuilder | ((ctx: PromptContext<T>) => MaybePromise<StringSelectMenuBuilder>);
36
- }
37
- /** A prompt state that uses a modal that is opened by a button */
38
- interface PromptStateModalButtonComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, ButtonInteraction, ModalSubmitInteraction> {
39
- type: PromptComponentType.ModalButton;
40
- component: ModalComponentReturnType<ButtonBuilder, ButtonInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<ButtonBuilder, ButtonInteraction>>);
41
- }
42
- /** A prompt state that is a modal opened by a select menu component */
43
- interface PromptStateModalSelectMenuComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, StringSelectMenuInteraction, ModalSubmitInteraction | null> {
44
- type: PromptComponentType.ModalSelectMenu;
45
- component: ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction>>);
46
- }
47
- /** A type representing a prompt state component, used when defining the components added for a prompt state */
48
- type PromptStateComponent<T extends object> = PromptStateButtonComponent<T> | PromptStateSelectMenuComponent<T> | PromptStateModalSelectMenuComponent<T> | PromptStateModalButtonComponent<T>;
49
- /** A prompt state message callback */
50
- interface PromptStateMessageCallback<T extends object> {
51
- ephemeral: boolean;
52
- content?: string | ((ctx: PromptContext<T>) => MaybePromise<string | undefined>);
53
- embeds: EmbedBuilder[] | ((ctx: PromptContext<T>) => MaybePromise<EmbedBuilder[]>);
54
- components: PromptStateComponent<T>[][] | ((ctx: PromptContext<T>) => MaybePromise<PromptStateComponent<T>[][]>);
55
- }
56
- /**
57
- * A type representing a prompt state that is possible
58
- * @param T The type of the prompt context
59
- */
60
- interface PromptState<T extends object> {
61
- name: string;
62
- timeout?: number;
63
- onEntered?: (ctx: PromptContext<T>) => MaybePromise<string | undefined>;
64
- message: PromptStateMessageCallback<T> | ((ctx: PromptContext<T>) => MaybePromise<PromptStateMessageCallback<T>>);
65
- }
66
-
67
- export { PromptComponentType, type PromptContext, type PromptState, type PromptStateButtonComponent, type PromptStateComponent, type PromptStateComponentBase, type PromptStateComponentBaseWithInteraction, type PromptStateMessageCallback, type PromptStateModalButtonComponent, type PromptStateModalSelectMenuComponent, type PromptStateSelectMenuComponent };
@@ -1 +0,0 @@
1
- "use strict";var r=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var p=(n,e)=>{for(var o in e)r(n,o,{get:e[o],enumerable:!0})},u=(n,e,o,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of m(e))!l.call(n,t)&&t!==o&&r(n,t,{get:()=>e[t],enumerable:!(a=c(e,t))||a.enumerable});return n};var d=n=>u(r({},"__esModule",{value:!0}),n);var s={};p(s,{PromptComponentType:()=>i});module.exports=d(s);var i=(t=>(t[t.Button=0]="Button",t[t.ModalButton=1]="ModalButton",t[t.SelectMenu=2]="SelectMenu",t[t.ModalSelectMenu=3]="ModalSelectMenu",t))(i||{});0&&(module.exports={PromptComponentType});
@@ -1 +0,0 @@
1
- var e=(t=>(t[t.Button=0]="Button",t[t.ModalButton=1]="ModalButton",t[t.SelectMenu=2]="SelectMenu",t[t.ModalSelectMenu=3]="ModalSelectMenu",t))(e||{});export{e as PromptComponentType};
@@ -1,3 +0,0 @@
1
- type MaybePromise<T> = T | Promise<T>;
2
-
3
- export type { MaybePromise };
@@ -1,3 +0,0 @@
1
- type MaybePromise<T> = T | Promise<T>;
2
-
3
- export type { MaybePromise };
@@ -1 +0,0 @@
1
- "use strict";var i=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var t=Object.prototype.hasOwnProperty;var y=(o,e,m,T)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of s(e))!t.call(o,r)&&r!==m&&i(o,r,{get:()=>e[r],enumerable:!(T=p(e,r))||T.enumerable});return o};var P=o=>y(i({},"__esModule",{value:!0}),o);var a={};module.exports=P(a);
File without changes
@@ -1,8 +0,0 @@
1
- declare class PromptError extends Error {
2
- constructor(message: string);
3
- }
4
- declare class UserError extends Error {
5
- constructor(message: string);
6
- }
7
-
8
- export { PromptError, UserError };
@@ -1,8 +0,0 @@
1
- declare class PromptError extends Error {
2
- constructor(message: string);
3
- }
4
- declare class UserError extends Error {
5
- constructor(message: string);
6
- }
7
-
8
- export { PromptError, UserError };
@@ -1 +0,0 @@
1
- "use strict";var e=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var a=(t,r)=>e(t,"name",{value:r,configurable:!0});var d=(t,r)=>{for(var c in r)e(t,c,{get:r[c],enumerable:!0})},g=(t,r,c,u)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of i(r))!x.call(t,s)&&s!==c&&e(t,s,{get:()=>r[s],enumerable:!(u=p(r,s))||u.enumerable});return t};var h=t=>g(e({},"__esModule",{value:!0}),t);var k={};d(k,{PromptError:()=>o,UserError:()=>n});module.exports=h(k);var o=class t extends Error{static{a(this,"PromptError")}constructor(r){super(r),Error.captureStackTrace?.(this,t)}},n=class t extends Error{static{a(this,"UserError")}constructor(r){super(r),Error.captureStackTrace?.(this,t)}};0&&(module.exports={PromptError,UserError});
@@ -1 +0,0 @@
1
- var a=Object.defineProperty;var s=(r,t)=>a(r,"name",{value:t,configurable:!0});var c=class r extends Error{static{s(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,r)}},e=class r extends Error{static{s(this,"UserError")}constructor(t){super(t),Error.captureStackTrace?.(this,r)}};export{c as PromptError,e as UserError};
@@ -1,46 +0,0 @@
1
- import { RepliableInteraction } from 'discord.js';
2
- import { PromptState } from './prompt.types.mjs';
3
-
4
- declare class PromptError extends Error {
5
- constructor(message: string);
6
- }
7
- /**
8
- * A prompt that allows you to create a sequence of states that a user can go through
9
- * with discord.js buttons and select menus.
10
- *
11
- * @example
12
- * ```ts
13
- * const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
14
- * await prompt.start(interaction);
15
- * ```
16
- */
17
- declare class Prompt<T extends object> {
18
- initialState: string;
19
- /** The current context of this prompt */
20
- private context;
21
- /** The current state */
22
- private currentState?;
23
- /** All components for this prompt */
24
- private components;
25
- /** The interaction collector */
26
- private collector?;
27
- /** The states represented in a map */
28
- states: Map<string, PromptState<T>>;
29
- /** Creates the prompt with a given set of states */
30
- constructor(defaults: T, initialState: string, states: PromptState<T>[]);
31
- /**
32
- * Starts the prompt with a given interaction
33
- * @param interaction The interaction starting off the sequence
34
- * @returns {boolean} If true, the prompt started successfully
35
- */
36
- start(interaction: RepliableInteraction): Promise<boolean>;
37
- private changeState;
38
- private handleCollect;
39
- private prepareMessageOptions;
40
- private getNewStateFromCallback;
41
- private registerComponents;
42
- private unwrap;
43
- private replyOrEdit;
44
- }
45
-
46
- export { Prompt, PromptError };
@@ -1,46 +0,0 @@
1
- import { RepliableInteraction } from 'discord.js';
2
- import { PromptState } from './prompt.types.js';
3
-
4
- declare class PromptError extends Error {
5
- constructor(message: string);
6
- }
7
- /**
8
- * A prompt that allows you to create a sequence of states that a user can go through
9
- * with discord.js buttons and select menus.
10
- *
11
- * @example
12
- * ```ts
13
- * const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
14
- * await prompt.start(interaction);
15
- * ```
16
- */
17
- declare class Prompt<T extends object> {
18
- initialState: string;
19
- /** The current context of this prompt */
20
- private context;
21
- /** The current state */
22
- private currentState?;
23
- /** All components for this prompt */
24
- private components;
25
- /** The interaction collector */
26
- private collector?;
27
- /** The states represented in a map */
28
- states: Map<string, PromptState<T>>;
29
- /** Creates the prompt with a given set of states */
30
- constructor(defaults: T, initialState: string, states: PromptState<T>[]);
31
- /**
32
- * Starts the prompt with a given interaction
33
- * @param interaction The interaction starting off the sequence
34
- * @returns {boolean} If true, the prompt started successfully
35
- */
36
- start(interaction: RepliableInteraction): Promise<boolean>;
37
- private changeState;
38
- private handleCollect;
39
- private prepareMessageOptions;
40
- private getNewStateFromCallback;
41
- private registerComponents;
42
- private unwrap;
43
- private replyOrEdit;
44
- }
45
-
46
- export { Prompt, PromptError };