@moody-djs/prompts 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +112 -0
- package/dist/index.js +1 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { RepliableInteraction, EmbedBuilder, ButtonInteraction, ButtonBuilder, StringSelectMenuInteraction, StringSelectMenuBuilder, ModalSubmitInteraction, ModalBuilder } from 'discord.js';
|
|
2
|
+
|
|
3
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
+
|
|
5
|
+
/** An enum containing the prompt component types that are supported */
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var p=(r,t)=>d(r,"name",{value:t,configurable:!0});var C=(r,t)=>{for(var e in t)d(r,e,{get:t[e],enumerable:!0})},y=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of b(t))!h.call(r,o)&&o!==e&&d(r,o,{get:()=>t[o],enumerable:!(n=M(t,o))||n.enumerable});return r};var f=r=>y(d({},"__esModule",{value:!0}),r);var P={};C(P,{Prompt:()=>g,PromptComponentType:()=>S,PromptError:()=>m,UserError:()=>x});module.exports=f(P);var c=require("discord.js");var S=(o=>(o[o.Button=0]="Button",o[o.ModalButton=1]="ModalButton",o[o.SelectMenu=2]="SelectMenu",o[o.ModalSelectMenu=3]="ModalSelectMenu",o))(S||{});var m=class r extends Error{static{p(this,"PromptError")}constructor(t){super(t),Error.captureStackTrace?.(this,r)}},x=class r extends Error{static{p(this,"UserError")}constructor(t){super(t),Error.captureStackTrace?.(this,r)}};var g=class{constructor(t,e,n){this.initialState=e;let o=[],a=p(()=>this.context.previousStates.pop()??this.initialState,"goBack");this.context={...t,previousStates:o,goBack:a},this.states=new Map(n.map(i=>[i.name,i]))}static{p(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 n=this.states.get(t);if(!n)return Promise.reject(new m(`State ${t} not found.`));this.context.previousStates.push(this.currentState?.name??this.initialState),this.currentState=n;let o=await n.onEntered?.(this.context);if(o)return this.changeState(o,e);let a=await this.prepareMessageOptions(n),i=e.replied||e.deferred?await e.editReply({embeds:a.embeds,components:a.components,content:a.content}):await e.reply(a);return this.collector&&this.collector.stop(),this.collector=i.createMessageComponentCollector({time:n.timeout||12e4,filter:p(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 o=typeof e.component=="function"?await e.component(this.context):e.component,a=typeof o.modal=="function"?await o.modal(t):o.modal;if(!a){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 i=c.SnowflakeUtil.generate().toString();a.setCustomId(i),await t.showModal(a);let s=await t.awaitModalSubmit({time:3e5,filter:p(u=>u.user.id===t.user.id&&u.customId===i,"filter")}).catch(()=>null);if(!s)return;await s.deferUpdate(),this.collector?.stop(),this.context.interaction=s;let l=await this.getNewStateFromCallback(e,s)??this.currentState?.name;return l?void this.changeState(l,t):t.deleteReply()}await t.deferUpdate(),this.collector?.stop(),this.context.interaction=t;let n=await this.getNewStateFromCallback(e)??this.currentState?.name;if(!n)return t.deleteReply();this.changeState(n,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=[],n=Array.isArray(t.components)?t.components:await t.components(this.context);this.components.clear();for(let o of n){let a=new c.ActionRowBuilder;for(let i of o){let s=typeof i.component=="function"?await i.component(this.context):i.component,l=c.SnowflakeUtil.generate().toString();"button"in s?(s.button.setCustomId(l),this.components.set(l,i),a.addComponents(s.button)):(s.setCustomId(l),this.components.set(l,i),a.addComponents(s))}e.push(a)}return e}};0&&(module.exports={Prompt,PromptComponentType,PromptError,UserError});
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@moody-djs/prompts",
|
|
3
|
+
"description": "A simple library for creating state based prompts in discord.js",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist/**/*",
|
|
8
|
+
"package.json"
|
|
9
|
+
],
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"discord.js": "latest"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"prompts",
|
|
15
|
+
"discord.js"
|
|
16
|
+
],
|
|
17
|
+
"author": "ahmood",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "tsc --noEmit --skipLibCheck",
|
|
21
|
+
"format": "biome format src --write",
|
|
22
|
+
"lint": "biome lint src --write",
|
|
23
|
+
"biome": "biome check src --write",
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"bump": "cliff-jumper",
|
|
26
|
+
"check-update": "cliff-jumper --dry-run"
|
|
27
|
+
}
|
|
28
|
+
}
|