@moody-djs/prompts 1.0.0 → 1.0.1
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/Prompt.d.mts +42 -0
- package/dist/Prompt.d.ts +42 -0
- package/dist/Prompt.js +1 -0
- package/dist/Prompt.mjs +1 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -112
- package/dist/index.mjs +1 -0
- package/dist/types/prompt.types.d.mts +67 -0
- package/dist/types/prompt.types.d.ts +67 -0
- package/dist/types/prompt.types.js +1 -0
- package/dist/types/prompt.types.mjs +1 -0
- package/dist/types/util.types.d.mts +3 -0
- package/dist/types/util.types.d.ts +3 -0
- package/dist/types/util.types.js +1 -0
- package/dist/types/util.types.mjs +0 -0
- package/dist/util/errors.d.mts +8 -0
- package/dist/util/errors.d.ts +8 -0
- package/dist/util/errors.js +1 -0
- package/dist/util/errors.mjs +1 -0
- package/dist/v2/Prompt.d.mts +46 -0
- package/dist/v2/Prompt.d.ts +46 -0
- package/dist/v2/Prompt.js +1 -0
- package/dist/v2/Prompt.mjs +1 -0
- package/dist/v2/prompt.types.d.mts +53 -0
- package/dist/v2/prompt.types.d.ts +53 -0
- package/dist/v2/prompt.types.js +1 -0
- package/dist/v2/prompt.types.mjs +1 -0
- package/package.json +41 -28
|
@@ -0,0 +1,42 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
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};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Prompt } from './Prompt.mjs';
|
|
2
|
+
export { PromptComponentType, PromptContext, PromptState, PromptStateButtonComponent, PromptStateComponent, PromptStateComponentBase, PromptStateComponentBaseWithInteraction, PromptStateMessageCallback, PromptStateModalButtonComponent, PromptStateModalSelectMenuComponent, PromptStateSelectMenuComponent } from './types/prompt.types.mjs';
|
|
3
|
+
export { PromptError, UserError } from './util/errors.mjs';
|
|
4
|
+
import 'discord.js';
|
|
5
|
+
import './types/util.types.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,112 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
declare enum PromptComponentType {
|
|
7
|
-
Button = 0,
|
|
8
|
-
ModalButton = 1,
|
|
9
|
-
SelectMenu = 2,
|
|
10
|
-
ModalSelectMenu = 3
|
|
11
|
-
}
|
|
12
|
-
type PromptContext<Context extends object, T = RepliableInteraction> = Context & {
|
|
13
|
-
interaction?: T;
|
|
14
|
-
previousStates: string[];
|
|
15
|
-
goBack: () => string;
|
|
16
|
-
genericError?: string;
|
|
17
|
-
};
|
|
18
|
-
interface ModalComponentReturnType<T, Interaction> {
|
|
19
|
-
button: T;
|
|
20
|
-
modal: ModalBuilder | ((interaction: Interaction) => MaybePromise<ModalBuilder | null>);
|
|
21
|
-
}
|
|
22
|
-
interface PromptStateComponentBase<Context extends object, Interaction = RepliableInteraction> {
|
|
23
|
-
callback: string | ((ctx: PromptContext<Context, Interaction>) => MaybePromise<string | undefined>);
|
|
24
|
-
}
|
|
25
|
-
interface PromptStateComponentBaseWithInteraction<Context extends object, Interaction = RepliableInteraction, AlternateInteraction = Interaction> {
|
|
26
|
-
callback: string | ((ctx: PromptContext<Context, Interaction>, interaction: AlternateInteraction) => MaybePromise<string | undefined>);
|
|
27
|
-
}
|
|
28
|
-
/** A prompt state that uses a button component */
|
|
29
|
-
interface PromptStateButtonComponent<T extends object> extends PromptStateComponentBase<T, ButtonInteraction> {
|
|
30
|
-
type: PromptComponentType.Button;
|
|
31
|
-
component: ButtonBuilder | ((ctx: PromptContext<T>) => MaybePromise<ButtonBuilder>);
|
|
32
|
-
}
|
|
33
|
-
/** A prompt state that contains a select menu */
|
|
34
|
-
interface PromptStateSelectMenuComponent<T extends object> extends PromptStateComponentBase<T, StringSelectMenuInteraction> {
|
|
35
|
-
type: PromptComponentType.SelectMenu;
|
|
36
|
-
component: StringSelectMenuBuilder | ((ctx: PromptContext<T>) => MaybePromise<StringSelectMenuBuilder>);
|
|
37
|
-
}
|
|
38
|
-
/** A prompt state that uses a modal that is opened by a button */
|
|
39
|
-
interface PromptStateModalButtonComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, ButtonInteraction, ModalSubmitInteraction> {
|
|
40
|
-
type: PromptComponentType.ModalButton;
|
|
41
|
-
component: ModalComponentReturnType<ButtonBuilder, ButtonInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<ButtonBuilder, ButtonInteraction>>);
|
|
42
|
-
}
|
|
43
|
-
/** A prompt state that is a modal opened by a select menu component */
|
|
44
|
-
interface PromptStateModalSelectMenuComponent<T extends object> extends PromptStateComponentBaseWithInteraction<T, StringSelectMenuInteraction, ModalSubmitInteraction | null> {
|
|
45
|
-
type: PromptComponentType.ModalSelectMenu;
|
|
46
|
-
component: ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction> | ((ctx: PromptContext<T>) => MaybePromise<ModalComponentReturnType<StringSelectMenuBuilder, StringSelectMenuInteraction>>);
|
|
47
|
-
}
|
|
48
|
-
/** A type representing a prompt state component, used when defining the components added for a prompt state */
|
|
49
|
-
type PromptStateComponent<T extends object> = PromptStateButtonComponent<T> | PromptStateSelectMenuComponent<T> | PromptStateModalSelectMenuComponent<T> | PromptStateModalButtonComponent<T>;
|
|
50
|
-
/** A prompt state message callback */
|
|
51
|
-
interface PromptStateMessageCallback<T extends object> {
|
|
52
|
-
ephemeral: boolean;
|
|
53
|
-
content?: string | ((ctx: PromptContext<T>) => MaybePromise<string | undefined>);
|
|
54
|
-
embeds: EmbedBuilder[] | ((ctx: PromptContext<T>) => MaybePromise<EmbedBuilder[]>);
|
|
55
|
-
components: PromptStateComponent<T>[][] | ((ctx: PromptContext<T>) => MaybePromise<PromptStateComponent<T>[][]>);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* A type representing a prompt state that is possible
|
|
59
|
-
* @param T The type of the prompt context
|
|
60
|
-
*/
|
|
61
|
-
interface PromptState<T extends object> {
|
|
62
|
-
name: string;
|
|
63
|
-
timeout?: number;
|
|
64
|
-
onEntered?: (ctx: PromptContext<T>) => MaybePromise<string | undefined>;
|
|
65
|
-
message: PromptStateMessageCallback<T> | ((ctx: PromptContext<T>) => MaybePromise<PromptStateMessageCallback<T>>);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* A prompt that allows you to create a sequence of states that a user can go through
|
|
70
|
-
* with discord.js buttons and select menus.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const prompt = new Prompt<ContextType>(defaults, initialState.name, [...]);
|
|
75
|
-
* await prompt.start(interaction);
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
declare class Prompt<T extends object> {
|
|
79
|
-
initialState: string;
|
|
80
|
-
/** The current context of this prompt */
|
|
81
|
-
private context;
|
|
82
|
-
/** The current state */
|
|
83
|
-
private currentState?;
|
|
84
|
-
/** All components for this prompt */
|
|
85
|
-
private components;
|
|
86
|
-
/** The interaction collector */
|
|
87
|
-
private collector?;
|
|
88
|
-
/** The states represented in a map */
|
|
89
|
-
states: Map<string, PromptState<T>>;
|
|
90
|
-
/** Creates the prompt with a given set of states */
|
|
91
|
-
constructor(defaults: T, initialState: string, states: PromptState<T>[]);
|
|
92
|
-
/**
|
|
93
|
-
* Starts the prompt with a given interaction
|
|
94
|
-
* @param interaction The interaction starting off the sequence
|
|
95
|
-
* @returns {boolean} If true, the prompt started successfully
|
|
96
|
-
*/
|
|
97
|
-
start(interaction: RepliableInteraction): Promise<boolean>;
|
|
98
|
-
private changeState;
|
|
99
|
-
private handleCollect;
|
|
100
|
-
private prepareMessageOptions;
|
|
101
|
-
private getNewStateFromCallback;
|
|
102
|
-
private formatComponents;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
declare class PromptError extends Error {
|
|
106
|
-
constructor(message: string);
|
|
107
|
-
}
|
|
108
|
-
declare class UserError extends Error {
|
|
109
|
-
constructor(message: string);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export { Prompt, PromptComponentType, type PromptContext, PromptError, type PromptState, type PromptStateButtonComponent, type PromptStateComponent, type PromptStateComponentBase, type PromptStateComponentBaseWithInteraction, type PromptStateMessageCallback, type PromptStateModalButtonComponent, type PromptStateModalSelectMenuComponent, type PromptStateSelectMenuComponent, UserError };
|
|
1
|
+
export { Prompt } from './Prompt.js';
|
|
2
|
+
export { PromptComponentType, PromptContext, PromptState, PromptStateButtonComponent, PromptStateComponent, PromptStateComponentBase, PromptStateComponentBaseWithInteraction, PromptStateMessageCallback, PromptStateModalButtonComponent, PromptStateModalSelectMenuComponent, PromptStateSelectMenuComponent } from './types/prompt.types.js';
|
|
3
|
+
export { PromptError, UserError } from './util/errors.js';
|
|
4
|
+
import 'discord.js';
|
|
5
|
+
import './types/util.types.js';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var g=Object.defineProperty;var s=(p,t)=>g(p,"name",{value:t,configurable:!0});import{ActionRowBuilder as M,Collection as b,MessageFlags as h,SnowflakeUtil as S}from"discord.js";var u=(o=>(o[o.Button=0]="Button",o[o.ModalButton=1]="ModalButton",o[o.SelectMenu=2]="SelectMenu",o[o.ModalSelectMenu=3]="ModalSelectMenu",o))(u||{});var l=class p extends Error{static{s(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,p)}},d=class p extends Error{static{s(this,"UserError")}constructor(t){super(t),Error.captureStackTrace?.(this,p)}};var x=class{constructor(t,e,r){this.initialState=e;let o=[],n=s(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:o,goBack:n},this.states=new Map(r.map(a=>[a.name,a]))}static{s(this,"Prompt")}context;currentState;components=new b;collector;states;async start(t){return this.context.interaction=t,this.changeState(this.initialState,t)}async changeState(t,e){let r=this.states.get(t);if(!r)return Promise.reject(new l(`State ${t} not found.`));this.context.previousStates.push(this.currentState?.name??this.initialState),this.currentState=r;let o=await r.onEntered?.(this.context);if(o)return this.changeState(o,e);let n=await this.prepareMessageOptions(r),a=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=a.createMessageComponentCollector({time:r.timeout||12e4,filter:s(i=>i.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 l(`Couldn't find component with customId: ${t.customId}`);if(e.type===1||e.type===3){let o=typeof e.component=="function"?await e.component(this.context):e.component,n=typeof o.modal=="function"?await o.modal(t):o.modal;if(!n){await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let m=await this.getNewStateFromCallback(e)??this.currentState?.name;return m?void this.changeState(m,t):t.deleteReply()}let a=S.generate().toString();n.setCustomId(a),await t.showModal(n);let i=await t.awaitModalSubmit({time:3e5,filter:s(m=>m.user.id===t.user.id&&m.customId===a,"filter")}).catch(()=>null);if(!i)return;await i.deferUpdate(),this.collector?.stop(),this.context.interaction=i;let c=await this.getNewStateFromCallback(e,i)??this.currentState?.name;return c?void this.changeState(c,t):t.deleteReply()}await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let r=await this.getNewStateFromCallback(e)??this.currentState?.name;if(!r)return t.deleteReply();this.changeState(r,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?h.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 l("No modal submit interaction found when required.");return t.callback(this.context,e)}return t.callback(this.context)}async formatComponents(t){let e=[],r=Array.isArray(t.components)?t.components:await t.components(this.context);this.components.clear();for(let o of r){let n=new M;for(let a of o){let i=typeof a.component=="function"?await a.component(this.context):a.component,c=S.generate().toString();"button"in i?(i.button.setCustomId(c),this.components.set(c,a),n.addComponents(i.button)):(i.setCustomId(c),this.components.set(c,a),n.addComponents(i))}e.push(n)}return e}};export{x as Prompt,u as PromptComponentType,l as PromptError,d as UserError};
|
|
@@ -0,0 +1,67 @@
|
|
|
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 };
|
|
@@ -0,0 +1,67 @@
|
|
|
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 };
|
|
@@ -0,0 +1 @@
|
|
|
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});
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
|
@@ -0,0 +1 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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});
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
|
@@ -0,0 +1,46 @@
|
|
|
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 };
|
|
@@ -0,0 +1,46 @@
|
|
|
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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var s=(a,t)=>d(a,"name",{value:t,configurable:!0});var y=(a,t)=>{for(var e in t)d(a,e,{get:t[e],enumerable:!0})},M=(a,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of g(t))!I.call(a,n)&&n!==e&&d(a,n,{get:()=>t[n],enumerable:!(o=h(t,n))||o.enumerable});return a};var x=a=>M(d({},"__esModule",{value:!0}),a);var T={};y(T,{Prompt:()=>P,PromptError:()=>c});module.exports=x(T);var S=require("util"),r=require("discord.js");var b=require("discord.js");var m=class extends b.ButtonBuilder{static{s(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}},C=class extends b.StringSelectMenuBuilder{static{s(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 c=class a extends Error{static{s(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,a)}},P=class{constructor(t,e,o){this.initialState=e;let n=[],l=s(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:n,goBack:l},this.states=new Map(o.map(i=>[i.name,i]))}static{s(this,"Prompt")}context;currentState;components=new r.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 c(`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 l=await this.prepareMessageOptions(o),i=await this.replyOrEdit(e,l);return this.collector&&this.collector.stop(),this.collector=i.createMessageComponentCollector({time:o.timeout||12e4,filter:s(u=>u.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 c(`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 p=await this.getNewStateFromCallback(e)??this.currentState?.name;return p?void this.changeState(p,t):t.deleteReply()}let l=r.SnowflakeUtil.generate().toString();n.setCustomId(l),await t.showModal(n);let i=await t.awaitModalSubmit({time:3e5,filter:s(p=>p.user.id===t.user.id&&p.customId===l,"filter")}).catch(()=>null);if(!i)return;await i.deferUpdate(),this.collector?.stop(),this.context.interaction=i;let u=await this.getNewStateFromCallback(e,i)??this.currentState?.name;return u?void this.changeState(u,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((0,S.inspect)(o,{depth:null})),{components:o,fetchReply:!0,flags:(e.ephemeral?r.MessageFlags.Ephemeral:0)|r.MessageFlags.IsComponentsV2}}async getNewStateFromCallback(t,e){if(t.callback){if(typeof t.callback=="string")return t.callback;if(t.modal){if(!e)throw new c("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 m){let o=r.SnowflakeUtil.generate().toString();e.setCustomId(o),this.components.set(o,{...e.toJSON(),custom_id:o,callback:e.getCallback(),modal:e.getModal()})}else if(e instanceof C){let o=r.SnowflakeUtil.generate().toString();e.setCustomId(o),this.components.set(o,{...e.toJSON(),custom_id:o,callback:e.getCallback(),modal:e.getModal()})}else if(e instanceof r.SectionBuilder&&e.accessory instanceof m){let o=r.SnowflakeUtil.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)}};0&&(module.exports={Prompt,PromptError});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var P=Object.defineProperty;var r=(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{r(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{r(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{r(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,i)}},b=class{constructor(t,e,o){this.initialState=e;let n=[],s=r(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:n,goBack:s},this.states=new Map(o.map(a=>[a.name,a]))}static{r(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),a=await this.replyOrEdit(e,s);return this.collector&&this.collector.stop(),this.collector=a.createMessageComponentCollector({time:o.timeout||12e4,filter:r(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 a=await t.awaitModalSubmit({time:3e5,filter:r(l=>l.user.id===t.user.id&&l.customId===s,"filter")}).catch(()=>null);if(!a)return;await a.deferUpdate(),this.collector?.stop(),this.context.interaction=a;let m=await this.getNewStateFromCallback(e,a)??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,p as PromptError};
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
export { type APIPromptButtonComponent, type APIPromptSelectMenuComponent, type APIPromptTopLevelComponent, PromptButtonBuilder, type PromptContext, PromptSelectMenuBuilder, type PromptState, type PromptStateMessageCallback, type RegisterComponentsOptions };
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
export { type APIPromptButtonComponent, type APIPromptSelectMenuComponent, type APIPromptTopLevelComponent, PromptButtonBuilder, type PromptContext, PromptSelectMenuBuilder, type PromptState, type PromptStateMessageCallback, type RegisterComponentsOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var l=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var u=Object.prototype.hasOwnProperty;var r=(e,t)=>l(e,"name",{value:t,configurable:!0});var b=(e,t)=>{for(var n in t)l(e,n,{get:t[n],enumerable:!0})},d=(e,t,n,p)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of s(t))!u.call(e,o)&&o!==n&&l(e,o,{get:()=>t[o],enumerable:!(p=m(t,o))||p.enumerable});return e};var C=e=>d(l({},"__esModule",{value:!0}),e);var P={};b(P,{PromptButtonBuilder:()=>i,PromptSelectMenuBuilder:()=>c});module.exports=C(P);var a=require("discord.js");var i=class extends a.ButtonBuilder{static{r(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}},c=class extends a.StringSelectMenuBuilder{static{r(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}};0&&(module.exports={PromptButtonBuilder,PromptSelectMenuBuilder});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});import{ButtonBuilder as r,StringSelectMenuBuilder as i}from"discord.js";var n=class extends r{static{o(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}},l=class extends i{static{o(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}};export{n as PromptButtonBuilder,l as PromptSelectMenuBuilder};
|
package/package.json
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@moody-djs/prompts",
|
|
3
|
+
"description": "A simple library for creating state based prompts in discord.js",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "tsc --noEmit --skipLibCheck",
|
|
8
|
+
"format": "biome format src --write",
|
|
9
|
+
"lint": "biome lint src --write",
|
|
10
|
+
"biome": "biome check src --write",
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"bump": "cliff-jumper",
|
|
13
|
+
"check-update": "cliff-jumper --dry-run",
|
|
14
|
+
"publish": "npm publish --access public"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*",
|
|
18
|
+
"package.json"
|
|
19
|
+
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"require": "./dist/index.js",
|
|
24
|
+
"import": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./v2": {
|
|
27
|
+
"types": "./dist/v2/index.d.ts",
|
|
28
|
+
"require": "./dist/v2/index.js",
|
|
29
|
+
"import": "./dist/v2/index.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"discord.js": "latest"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"prompts",
|
|
37
|
+
"discord.js"
|
|
38
|
+
],
|
|
39
|
+
"author": "ahmood",
|
|
40
|
+
"license": "ISC"
|
|
41
|
+
}
|