PromptsHelper
This file defines the PromptsHelper
class, a provider for interactive prompts in Discord bots using the discord.js
library. It offers helper methods to present confirmation dialogs and choice menus to users via Discord interactions, handling user responses in a type-safe and user-friendly way.
Features
- Confirmation Prompts: Displays a yes/no confirmation dialog using Discord buttons and returns the user's choice.
- Choice Prompts: Presents a select menu with multiple options and returns the user's selected value.
- Customizable UI: Supports custom labels, descriptions, fields, colors, and timeouts for both confirmation and choice prompts.
- Integration with Discord Interactions: Works with
ChatInputCommandInteraction
and other allowed interaction types.
Usage
typescript
import PromptsHelper from './provider/prompts';
// In a Discord command handler:
const confirmed = await promptsHelper.confirm(interaction, 'Are you sure?', { description: 'This action is irreversible.' });
const choice = await promptsHelper.choice(interaction, 'Pick a color:', [
{ label: 'Red', value: 'red' },
{ label: 'Blue', value: 'blue' }
]);
API
Methods
async confirm(interaction: IAllowedInteractionType, question: string, options?: IPromptsConfirmOptions): Promise<boolean>
Displays a confirmation dialog with "Yes" and "No" buttons.
- interaction: The Discord interaction to reply to.
- question: The question to display in the embed.
- options: Optional settings (description, labels, fields, timeout, etc.).
- Returns:
true
if the user confirms,false
otherwise.
async choice(interaction: ChatInputCommandInteraction, question: string, choices: IPromptChoiceItem[], options?: IPromptsChoiceOptions): Promise<string | null>
Displays a select menu with the provided choices.
- interaction: The Discord interaction to reply to.
- question: The question to display in the embed.
- choices: Array of choice items (label/value pairs).
- options: Optional settings (description, placeholder, fields, timeout, etc.).
- Returns: The selected value, or
null
if no selection was made.
How It Works
- Embeds: Creates a Discord embed with the provided question and options.
- Components: Adds either a row of buttons (for confirmation) or a select menu (for choices).
- Interaction Handling: Replies or edits the interaction with the prompt, then waits for the user's component interaction.
- Response Filtering: Ensures only the user who initiated the interaction can respond.
- Timeouts: Waits for a response up to the specified timeout (default 60 seconds).
Notes
- Requires
discord.js
v14 or newer. - Designed to be used as a provider within the Sodacore dependency injection system.
- Handles both initial replies and edits to already replied interactions.
- Returns
null
fromchoice
if the user does not select an option or the interaction times out.