New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sapphire/discord.js-utilities

Package Overview
Dependencies
Maintainers
3
Versions
1082
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sapphire/discord.js-utilities - npm Package Compare versions

Comparing version 2.0.1-pr-137.e999494f.0 to 2.0.1

903

dist/index.d.ts
export * from '@sapphire/discord-utilities';
export * from './lib/builders/MessageBuilder';
export * from './lib/MessagePrompter';
export * from './lib/PaginatedMessages';
export * from './lib/type-guards';
//# sourceMappingURL=index.d.ts.map
import { MessageOptions, MessageEmbed, MessageMentionOptions, APIMessageContentResolvable, MessageAdditions, EmojiIdentifierResolvable, Message, TextChannel, NewsChannel, DMChannel, User, CollectorFilter, GuildEmoji, ReactionEmoji, EmojiResolvable, ReactionCollector, APIMessage, MessageReaction, Collection, Snowflake, Channel, CategoryChannel, PartialGroupDMChannel, GuildChannel, StoreChannel, VoiceChannel } from 'discord.js';
import { Awaited, Ctor } from '@sapphire/utilities';
declare type MessageBuilderFileResolvable = NonNullable<MessageOptions['files']>[number];
declare type MessageBuilderCodeResolvable = NonNullable<MessageOptions['code']>;
declare type MessageBuilderSplitResolvable = NonNullable<MessageOptions['split']>;
declare type MessageBuilderResolvable = Omit<MessageOptions, 'embed' | 'disableMentions' | 'reply'> & {
embed?: MessageEmbed;
};
/**
* A message builder class, it implements the {@linkplain https://discord.js.org/#/docs/main/stable/typedef/MessageOptions MessageOptions}
* interface.
*/
declare class MessageBuilder implements MessageOptions {
/**
* Whether or not the message should be spoken aloud.
* @default false
*/
tts?: boolean;
/**
* The nonce for the message.
* @default ''
*/
nonce?: string;
/**
* The content for the message. If set to undefined and the builder is used to edit, the content will not be
* replaced.
*/
content?: string;
/**
* An embed for the message. If set to undefined and the builder is used to edit, the embed will not be replaced.
*/
embed?: MessageEmbed;
/**
* Which mentions should be parsed from the message content.
*/
allowedMentions?: MessageMentionOptions;
/**
* Files to send with the message. This should not be set when editing a message, as Discord does not support
* editing file attachments.
*/
files?: MessageBuilderFileResolvable[];
/**
* Language for optional codeblock formatting to apply.
*/
code?: MessageBuilderCodeResolvable;
/**
* Whether or not the message should be split into multiple messages if it exceeds the character limit. If an object
* is provided, these are the options for splitting the message.
*/
split?: MessageBuilderSplitResolvable;
constructor(options?: MessageBuilderResolvable);
/**
* Sets the value for the {@link MessageBuilder.tts} field.
* @param tts Whether or not the message should be spoken aloud.
*/
setTTS(tts?: boolean): this;
/**
* Sets the value for the {@link MessageBuilder.nonce} field.
* @param nonce The nonce for the message.
*/
setNonce(nonce?: string): this;
/**
* Sets the value for the {@link MessageBuilder.content} field.
* @param content The content for the message. If set to undefined and the builder is used to edit, the content will
* not be replaced.
*/
setContent(content?: string): this;
/**
* Sets the value for the {@link MessageBuilder.embed} field.
* @param embed An embed for the message. If set to undefined and the builder is used to edit, the embed will not be
* replaced.
*/
setEmbed(embed?: MessageEmbed): this;
/**
* Sets the value for the {@link MessageBuilder.allowedMentions} field.
* @param allowedMentions Which mentions should be parsed from the message content.
*/
setAllowedMentions(allowedMentions?: MessageMentionOptions): this;
/**
* Adds a new value for the {@link MessageBuilder.files} field array.
* @param file The file to add to the {@link MessageBuilder.files} field array.
*/
addFile(file: MessageBuilderFileResolvable): this;
/**
* Sets a single value for the {@link MessageBuilder.files} field array.
* @param file The file to send with the message. This should not be set when editing a message, as Discord does not
* support editing file attachments.
*/
setFile(file: MessageBuilderFileResolvable): this;
/**
* Sets the value for the {@link MessageBuilder.files} field.
* @param files The files to send with the message. This should not be set when editing a message, as Discord does
* not support editing file attachments.
*/
setFiles(files?: MessageBuilderFileResolvable[]): this;
/**
* Sets the value for the {@link MessageBuilder.code} field.
* @param code Language for optional codeblock formatting to apply.
*/
setCode(code?: MessageBuilderCodeResolvable): this;
/**
* Sets the value for the {@link MessageBuilder.split} field.
* @param split Whether or not the message should be split into multiple messages if it exceeds the character limit.
* If an object is provided, these are the options for splitting the message.
*/
setSplit(split?: MessageBuilderSplitResolvable): this;
/**
* The default values for all MessageBuilder instances.
*/
static defaults: MessageBuilderResolvable;
}
/**
* A type to extend multiple discord types and simplify usage in {@link MessagePrompter}
*/
declare type MessagePrompterMessage = APIMessageContentResolvable | (MessageOptions & {
split?: false;
}) | MessageAdditions;
declare const enum MessagePrompterStrategies {
Confirm = "confirm",
Number = "number",
Message = "message",
Reaction = "reaction"
}
interface IMessagePrompterStrategyOptions {
timeout?: number;
explicitReturn?: boolean;
}
interface IMessagePrompterConfirmStrategyOptions extends IMessagePrompterStrategyOptions {
confirmEmoji?: string | EmojiIdentifierResolvable;
cancelEmoji?: string | EmojiIdentifierResolvable;
}
interface IMessagePrompterNumberStrategyOptions extends IMessagePrompterStrategyOptions {
start?: number;
end?: number;
numberEmojis?: string[] | EmojiIdentifierResolvable[];
}
interface IMessagePrompterReactionStrategyOptions extends IMessagePrompterStrategyOptions {
reactions: string[] | EmojiIdentifierResolvable[];
}
declare abstract class MessagePrompterBaseStrategy {
/**
* The type of strategy that was used
*/
type: string;
/**
* The timeout that was used in the collector
*/
timeout: number;
/**
* Wether to return an explicit object with data, or the strategies' default
*/
explicitReturn: boolean;
/**
* The message that has been sent in {@link MessagePrompter.run}
*/
appliedMessage: Message | null;
/**
* The message that will be sent in {@link MessagePrompter.run}
*/
message: MessagePrompterMessage;
/**
* Constructor for the {@link MessagePrompterBaseStrategy} class
* @param messagePrompter The used instance of {@link MessagePrompter}
* @param options Overrideable options if needed.
*/
constructor(type: string, message: MessagePrompterMessage, options?: IMessagePrompterStrategyOptions);
abstract run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): Awaited<unknown>;
protected collectReactions(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter, reactions: string[] | EmojiIdentifierResolvable[]): Promise<IMessagePrompterExplicitReturnBase>;
/**
* Creates a filter for the collector to filter on
* @return The filter for awaitReactions function
*/
protected createReactionPromptFilter(reactions: string[] | EmojiIdentifierResolvable[], authorOrFilter: User | CollectorFilter): CollectorFilter;
/**
* The default strategy options
*/
static defaultStrategyOptions: Required<IMessagePrompterStrategyOptions>;
}
interface IMessagePrompterExplicitReturnBase {
emoji?: GuildEmoji | ReactionEmoji;
reaction?: string | EmojiIdentifierResolvable;
strategy: MessagePrompterBaseStrategy;
appliedMessage: Message;
message: MessagePrompterMessage;
}
interface IMessagePrompterExplicitConfirmReturn extends IMessagePrompterExplicitReturnBase {
confirmed: boolean;
}
interface IMessagePrompterExplicitNumberReturn extends IMessagePrompterExplicitReturnBase {
number: number;
}
interface IMessagePrompterExplicitMessageReturn extends IMessagePrompterExplicitReturnBase {
response?: Message;
}
declare class MessagePrompterConfirmStrategy extends MessagePrompterBaseStrategy implements IMessagePrompterConfirmStrategyOptions {
/**
* The confirm emoji used
*/
confirmEmoji: string | EmojiResolvable;
/**
* The cancel emoji used
*/
cancelEmoji: string | EmojiResolvable;
/**
* Constructor for the {@link MessagePrompterBaseStrategy} class
* @param message The message to be sent {@link MessagePrompter}
* @param options Overrideable options if needed.
*/
constructor(message: MessagePrompterMessage, options?: IMessagePrompterConfirmStrategyOptions);
/**
* This executes the {@link MessagePrompter} and sends the message if {@link IMessagePrompterOptions.type} equals confirm.
* The handler will wait for one (1) reaction.
* @param channel The channel to use.
* @param authorOrFilter An author object to validate or a {@linkplain https://discord.js.org/#/docs/main/stable/typedef/CollectorFilter CollectorFilter} predicate callback.
* @returns A promise that resolves to a boolean denoting the value of the input (`true` for yes, `false` for no).
*/
run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): Promise<IMessagePrompterExplicitConfirmReturn | boolean>;
/**
* The default confirm emoji used for {@link MessagePrompterConfirmStrategy}
*/
static confirmEmoji: string | EmojiResolvable;
/**
* The default cancel emoji used for {@link MessagePrompterConfirmStrategy}
*/
static cancelEmoji: string | EmojiResolvable;
}
declare class MessagePrompterMessageStrategy extends MessagePrompterBaseStrategy implements IMessagePrompterStrategyOptions {
/**
* Constructor for the {@link MessagePrompterBaseStrategy} class
* @param messagePrompter The used instance of {@link MessagePrompter}
* @param options Overrideable options if needed.
*/
constructor(message: MessagePrompterMessage, options: IMessagePrompterStrategyOptions);
/**
* This executes the {@link MessagePrompter} and sends the message if {@link IMessagePrompterOptions.type} equals message.
* The handler will wait for one (1) message.
* @param channel The channel to use.
* @param authorOrFilter An author object to validate or a {@linkplain https://discord.js.org/#/docs/main/stable/typedef/CollectorFilter CollectorFilter} predicate callback.
* @returns A promise that resolves to the message object received.
*/
run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): Promise<IMessagePrompterExplicitMessageReturn | Message>;
/**
* Creates a filter for the collector to filter on
* @return The filter for awaitMessages function
*/
private createMessagePromptFilter;
}
declare class MessagePrompterNumberStrategy extends MessagePrompterBaseStrategy implements IMessagePrompterNumberStrategyOptions {
/**
* The available number emojis
*/
numberEmojis: string[] | EmojiResolvable[];
/**
* The available number emojis
*/
start: number;
/**
* The available number emojis
*/
end: number;
/**
* Constructor for the {@link MessagePrompterBaseStrategy} class
* @param messagePrompter The used instance of {@link MessagePrompter}
* @param options Overrideable options if needed.
*/
constructor(message: MessagePrompterMessage, options: IMessagePrompterNumberStrategyOptions);
/**
* This executes the {@link MessagePrompter} and sends the message if {@link IMessagePrompterOptions.type} equals number.
* The handler will wait for one (1) reaction.
* @param channel The channel to use.
* @param authorOrFilter An author object to validate or a {@linkplain https://discord.js.org/#/docs/main/stable/typedef/CollectorFilter CollectorFilter} predicate callback.
* @returns A promise that resolves to the selected number within the range.
*/
run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): Promise<IMessagePrompterExplicitNumberReturn | number>;
/**
* The default available number emojis
*/
static numberEmojis: string[];
}
declare class MessagePrompterReactionStrategy extends MessagePrompterBaseStrategy implements MessagePrompterReactionStrategy {
/**
* The emojis used
*/
reactions: string[] | EmojiResolvable[];
/**
* Constructor for the {@link MessagePrompterReactionStrategy} class
* @param messagePrompter The used instance of {@link MessagePrompter}
* @param options Overrideable options if needed.
*/
constructor(message: MessagePrompterMessage, options: IMessagePrompterReactionStrategyOptions);
/**
* This executes the {@link MessagePrompterReactionStrategy} and sends the message.
* The handler will wait for one (1) reaction.
* @param channel The channel to use.
* @param authorOrFilter An author object to validate or a {@linkplain https://discord.js.org/#/docs/main/stable/typedef/CollectorFilter CollectorFilter} predicate callback.
* @returns A promise that resolves to the reaction object.
*/
run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): Promise<IMessagePrompterExplicitReturnBase | string | EmojiResolvable>;
}
interface StrategyReturns {
[MessagePrompterStrategies.Confirm]: IMessagePrompterExplicitConfirmReturn | boolean;
[MessagePrompterStrategies.Message]: IMessagePrompterExplicitMessageReturn | Message;
[MessagePrompterStrategies.Number]: IMessagePrompterExplicitNumberReturn | number;
[MessagePrompterStrategies.Reaction]: IMessagePrompterExplicitReturnBase | string | EmojiResolvable;
}
interface StrategyOptions {
[MessagePrompterStrategies.Confirm]: IMessagePrompterConfirmStrategyOptions;
[MessagePrompterStrategies.Message]: IMessagePrompterStrategyOptions;
[MessagePrompterStrategies.Number]: IMessagePrompterNumberStrategyOptions;
[MessagePrompterStrategies.Reaction]: IMessagePrompterReactionStrategyOptions;
}
/**
* This is a {@link MessagePrompter}, a utility that sends a message, prompting for user input. The prompt can resolve to any kind of input.
* There are several specifiable types to prompt for user input, and they are as follows:
* - Confirm
* This will send a simple Yes/No prompt, using reactions.
* - Number
* This will prompt for an integer. By default it will be a number between 0 and 10 (inclusive), however you can also specify your own custom range (inclusive).
* - Reactions
* This can be any kind of reaction emoji that Discord supports, and as many as you want. This type will return that reaction instead of a boolean.
* - Message
* This will prompt the user and require a response in the form of a message. This can be helpful if you require a user to upload an image for example, or give text input.
*
* You must either use this class directly or extend it.
*
* {@link MessagePrompter} uses reactions to prompt for a yes/no answer and returns it.
* You can modify the confirm and cancel reaction used for each message, or use the {@link MessagePrompter.defaultPrompts}.
* {@link MessagePrompter.defaultPrompts} is also static so you can modify these directly.
*
* @example
* ```typescript
* const handler = new MessagePrompter('Are you sure you want to continue?');
* const result = await handler.run(channel, author);
* ```
*
* @example
* ```typescript
* const handler = new MessagePrompter('Choose a number between 5 and 10?', 'number', {
* start: 5,
* end: 10
* });
* const result = await handler.run(channel, author);
* ```
*
* @example
* ```typescript
* const handler = new MessagePrompter('Are you happy or sad?', 'reaction', {
* reactions: ['🙂', '🙁']
* });
* const result = await handler.run(channel, author);
* ```
*
* @example
* ```typescript
* const handler = new MessagePrompter('Do you love me?', 'message');
* const result = await handler.run(channel, author);
* ```
*/
declare class MessagePrompter<S extends MessagePrompterStrategies = MessagePrompterStrategies.Confirm> {
/**
* The strategy used in {@link MessagePrompter.run}
*/
strategy: MessagePrompterBaseStrategy;
/**
* Constructor for the {@link MessagePrompter} class
* @param message The message to send.
* @param strategy The strategy name or Instance to use
* @param strategyOptions The options that are passed to the strategy
*/
constructor(message: MessagePrompterMessage | MessagePrompterBaseStrategy, strategy?: S, strategyOptions?: S extends keyof StrategyOptions ? StrategyOptions[S] : never);
/**
* This executes the {@link MessagePrompter} and sends the message.
* @param channel The channel to use.
* @param authorOrFilter An author object to validate or a {@linkplain https://discord.js.org/#/docs/main/stable/typedef/CollectorFilter CollectorFilter} predicate callback.
*/
run(channel: TextChannel | NewsChannel | DMChannel, authorOrFilter: User | CollectorFilter): S extends keyof StrategyReturns ? Promise<StrategyReturns[S]> : never;
/**
* The available strategies
*/
static strategies: Map<MessagePrompterStrategies, Ctor<ConstructorParameters<typeof MessagePrompterConfirmStrategy> | ConstructorParameters<typeof MessagePrompterNumberStrategy> | ConstructorParameters<typeof MessagePrompterReactionStrategy> | ConstructorParameters<typeof MessagePrompterMessageStrategy>, MessagePrompterConfirmStrategy | MessagePrompterNumberStrategy | MessagePrompterReactionStrategy | MessagePrompterMessageStrategy>>;
/**
* The default strategy to use
*/
static defaultStrategy: MessagePrompterStrategies;
}
/**
* This is a {@link PaginatedMessage}, a utility to paginate messages (usually embeds).
* You must either use this class directly or extend it.
*
* {@link PaginatedMessage} uses actions, these are essentially reaction emojis, when triggered run the said action.
* You can utilize your own actions, or you can use the {@link PaginatedMessage.defaultActions}.
* {@link PaginatedMessage.defaultActions} is also static so you can modify these directly.
*
* {@link PaginatedMessage} also uses pages, these are simply {@linkplain https://discord.js.org/#/docs/main/stable/class/APIMessage APIMessages}.
*
* @example
* ```typescript
* const handler = new PaginatedMessage();
* ```
*
* @example
* ```typescript
* // To utilize actions you can use the IPaginatedMessageAction by implementing it into a class.
* // PaginatedMessage requires you to have the class initialized using `new`.
*
* class ForwardAction implements IPaginatedMessageAction {
* public id = '▶️';
*
* public run({ handler }) {
* if (handler.index !== handler.pages.length - 1) ++handler.index;
* }
* }
*
* // You can also give the object directly.
*
* const StopAction: IPaginatedMessageAction = {
* id: '⏹️',
* disableResponseEdit: true,
* run: ({ response, collector }) => {
* await response.reactions.removeAll();
* collector!.stop();
* }
* }
* ```
*/
declare class PaginatedMessage {
/**
* The pages to be converted to {@link PaginatedMessage.messages}
*/
pages: MessagePage[];
/**
* The response message used to edit on page changes.
*/
response: Message | null;
/**
* The collector used for handling reactions.
*/
collector: ReactionCollector | null;
/**
* The pages which were converted from {@link PaginatedMessage.pages}
*/
messages: (APIMessage | null)[];
/**
* The actions which are to be used.
*/
actions: Map<string, IPaginatedMessageAction>;
/**
* The handler's current page/message index.
*/
index: number;
/**
* The amount of milliseconds to idle before the paginator is closed. Defaults to 20 minutes.
*/
idle: number;
/**
* The template for this {@link PaginatedMessage}.
* You can use templates to set defaults that will apply to each and every page in the {@link PaginatedMessage}
*/
template: MessageOptions;
/**
* Constructor for the {@link PaginatedMessage} class
* @param __namedParameters The {@link PaginatedMessageOptions} for this instance of the {@link PaginatedMessage} class
*/
constructor({ pages, actions, template }?: PaginatedMessageOptions);
setPromptMessage(message: string): this;
/**
* Sets the handler's current page/message index.
* @param index The number to set the index to.
*/
setIndex(index: number): this;
/**
* Sets the amount of time to idle before the paginator is closed.
* @param idle The number to set the idle to.
*/
setIdle(idle: number): this;
/**
* Clears all current actions and sets them. The order given is the order they will be used.
* @param actions The actions to set.
*/
setActions(actions: IPaginatedMessageAction[]): this;
/**
* Adds actions to the existing ones. The order given is the order they will be used.
* @param actions The actions to add.
*/
addActions(actions: IPaginatedMessageAction[]): this;
/**
* Adds an action to the existing ones. This will be added as the last action.
* @param action The action to add.
*/
addAction(action: IPaginatedMessageAction): this;
/**
* Checks whether or not the handler has a specific page.
* @param index The index to check.
*/
hasPage(index: number): boolean;
/**
* Clears all current pages and messages and sets them. The order given is the order they will be used.
* @param pages The pages to set.
*/
setPages(pages: MessagePage[]): this;
/**
* Adds a page to the existing ones. This will be added as the last page.
* @remark While you can use this method you should first check out
* {@link PaginatedMessage.addPageBuilder},
* {@link PaginatedMessage.addPageContent} and
* {@link PaginatedMessage.addPageEmbed} as
* these are easier functional methods of adding pages and will likely already suffice for your needs.
*
* @param page The page to add.
*/
addPage(page: MessagePage): this;
/**
* Adds a page to the existing ones using a {@link MessageBuilder}. This will be added as the last page.
* @param builder Either a callback whose first parameter is `new MessageBuilder()`, or an already constructed {@link MessageBuilder}
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
* const { MessageEmbed } = require('discord.js');
*
* const paginatedMessage = new PaginatedMessage()
* .addPageBuilder((builder) => {
* const embed = new MessageEmbed()
* .setColor('#FF0000')
* .setDescription('example description');
*
* return builder
* .setContent('example content')
* .setEmbed(embed);
* });
* ```
* @example
* ```typescript
* const { MessageEmbed } = require('discord.js');
* const { MessageBuilder, PaginatedMessage } = require('@sapphire/discord.js-utilities');
*
* const embed = new MessageEmbed()
* .setColor('#FF0000')
* .setDescription('example description');
*
* const builder = new MessageBuilder()
* .setContent('example content')
* .setEmbed(embed);
*
* const paginatedMessage = new PaginatedMessage()
* .addPageBuilder(builder);
* ```
*/
addPageBuilder(builder: MessageBuilder | ((builder: MessageBuilder) => MessageBuilder)): this;
/**
* Adds a page to the existing ones asynchronously using a {@link MessageBuilder}. This wil be added as the last page.
* @param builder Either a callback whose first parameter is `new MessageBuilder()`, or an already constructed {@link MessageBuilder}
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
* const { MessageEmbed } = require('discord.js');
*
* const paginatedMessage = new PaginatedMessage()
* .addAsyncPageBuilder(async (builder) => {
* const someRemoteData = await fetch('https://contoso.com/api/users');
*
* const embed = new MessageEmbed()
* .setColor('#FF0000')
* .setDescription(someRemoteData.data);
*
* return builder
* .setContent('example content')
* .setEmbed(embed);
* });
* ```
*/
addAsyncPageBuilder(builder: MessageBuilder | ((builder: MessageBuilder) => Promise<MessageBuilder>)): this;
/**
* Adds a page to the existing ones using simple message content. This will be added as the last page.
* @param content The content to set.
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
*
* const paginatedMessage = new PaginatedMessage()
* .addPageContent('example content');
* ```
*/
addPageContent(content: string): this;
/**
* Adds a page to the existing ones using a {@link MessageEmbed}. This wil be added as the last page.
* @param embed Either a callback whose first paramter is `new MessageEmbed()`, or an already constructed {@link MessageEmbed}
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
*
* const paginatedMessage = new PaginatedMessage()
* .addPageEmbed((embed) => {
* embed
* .setColor('#FF0000')
* .setDescription('example description');
*
* return embed;
* });
* ```
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
*
* const embed = new MessageEmbed()
* .setColor('#FF0000')
* .setDescription('example description');
*
* const paginatedMessage = new PaginatedMessage()
* .addPageEmbed(embed);
* ```
*/
addPageEmbed(embed: MessageEmbed | ((embed: MessageEmbed) => MessageEmbed)): this;
/**
* Adds a page to the existing ones asynchronously using a {@link MessageEmbed}. This wil be added as the last page.
* @param embed Either a callback whose first paramter is `new MessageEmbed()`, or an already constructed {@link MessageEmbed}
* @example
* ```typescript
* const { PaginatedMessage } = require('@sapphire/discord.js-utilities');
*
* const paginatedMessage = new PaginatedMessage()
* .addAsyncPageEmbed(async (embed) => {
* const someRemoteData = await fetch('https://contoso.com/api/users');
*
* embed
* .setColor('#FF0000')
* .setDescription(someRemoteData.data);
*
* return embed;
* });
* ```
*/
addAsyncPageEmbed(embed: MessageEmbed | ((builder: MessageEmbed) => Promise<MessageEmbed>)): this;
/**
* Add pages to the existing ones. The order given is the order they will be used.
* @param pages The pages to add.
*/
addPages(pages: MessagePage[]): this;
/**
* Executes the {@link PaginatedMessage} and sends the pages corresponding with {@link PaginatedMessage.index}.
* The handler will start collecting reactions and running actions once all actions have been reacted to the message.
* @param message The message that triggered this {@link PaginatedMessage}.
* Generally this will be the command message, but it can also be another message from your bot, i.e. to indicate a loading state.
* @param target The user who will be able to interact with the reactions of this {@link PaginatedMessage}. Defaults to `message.author`.
*/
run(message: Message, target?: User): Promise<this>;
/**
* Executed whenever {@link PaginatedMessage.run} is called.
*/
resolvePagesOnRun(channel: Message['channel']): Promise<void>;
/**
* Executed whenever an action is triggered and resolved.
* @param index The index to resolve.
*/
resolvePage(channel: Message['channel'], index: number): Promise<APIMessage>;
/**
* Clones the current handler into a new instance.
*/
clone(): PaginatedMessage;
/**
* Sets up the message.
* @param channel The channel the handler is running at.
* @param author The author the handler is for.
*/
protected setUpMessage(channel: Message['channel'], author: User): Promise<void>;
/**
* Sets up the message's reactions and the collector.
* @param channel The channel the handler is running at.
* @param author The author the handler is for.
*/
protected setUpReactions(channel: Message['channel'], author: User): Promise<void>;
/**
* Handles the load of a page.
* @param page The page to be loaded.
* @param channel The channel the paginated message runs at.
* @param index The index of the current page.
*/
protected handlePageLoad(page: MessagePage, channel: Message['channel'], index: number): Promise<APIMessage>;
/**
* Handles the `collect` event from the collector.
* @param author The the handler is for.
* @param channel The channel the handler is running at.
* @param reaction The reaction that was received.
* @param user The user that reacted to the message.
*/
protected handleCollect(author: User, channel: Message['channel'], reaction: MessageReaction, user: User): Promise<void>;
/**
* Handles the `end` event from the collector.
* @param reason The reason for which the collector was ended.
*/
protected handleEnd(_: Collection<Snowflake, MessageReaction>, reason: string): Promise<void>;
protected applyFooter(message: APIMessage, index: number): APIMessage;
private applyTemplate;
private applyTemplateEmbed;
private mergeEmbeds;
private mergeArrays;
/**
* The default actions of this handler.
*/
static defaultActions: IPaginatedMessageAction[];
/**
* The reasons sent by {@linkplain https://discord.js.org/#/docs/main/stable/class/ReactionCollector?scrollTo=e-end ReactionCollector#end}
* event when the message (or its owner) has been deleted.
*/
static deletionStopReasons: string[];
/**
* Custom prompt message when a user wants to jump to a certain page number.
* @default "What page would you like to jump to?"
*/
static promptMessage: string;
/**
* The messages that are currently being handled by a {@link PaginatedMessage}
* The key is the ID of the message that triggered this {@link PaginatedMessage}
*
* This is to ensure that only 1 {@link PaginatedMessage} can run on a specified message at once.
* This is important when having an editable commands solution.
*/
static readonly messages: Map<string, PaginatedMessage>;
/**
* The current {@link ReactionCollector} handlers that are active.
* The key is the ID of of the author who sent the message that triggered this {@link PaginatedMessage}
*
* This is to ensure that any given author can only trigger 1 {@link PaginatedMessage}.
* This is important for performance reasons, and users should not have more than 1 {@link PaginatedMessage} open at once.
*/
static readonly handlers: Map<string, PaginatedMessage>;
private static resolveTemplate;
}
interface PaginatedMessage {
constructor: typeof PaginatedMessage;
}
/**
* To utilize actions you can use the {@link IPaginatedMessageAction} by implementing it into a class.
* @example
* ```typescript
* class ForwardAction implements IPaginatedMessageAction {
* public id = '▶️';
*
* public run({ handler }) {
* if (handler.index !== handler.pages.length - 1) ++handler.index;
* }
* }
*
* // You can also give the object directly.
*
* const StopAction: IPaginatedMessageAction {
* id: '⏹️',
* disableResponseEdit: true,
* run: ({ response, collector }) => {
* await response.reactions.removeAll();
* collector!.stop();
* }
* }
* ```
*/
interface IPaginatedMessageAction {
id: string;
run(context: PaginatedMessageActionContext): Awaited<unknown>;
}
/**
* The context to be used in {@link IPaginatedMessageAction}.
*/
interface PaginatedMessageActionContext {
handler: PaginatedMessage;
author: User;
channel: Message['channel'];
response: Message;
collector: ReactionCollector;
}
interface PaginatedMessageOptions {
/**
* The pages to display in this {@link PaginatedMessage}
*/
pages?: MessagePage[];
/**
* Custom actions to provide when sending the paginated message
*/
actions?: IPaginatedMessageAction[];
/**
* The {@link MessageEmbed} or {@link MessageOptions} options to apply to the entire {@link PaginatedMessage}
*/
template?: MessageEmbed | MessageOptions;
}
/**
* The pages that are used for {@link PaginatedMessage.pages}
*
* Pages can be either an {@linkplain https://discord.js.org/#/docs/main/stable/class/APIMessage APIMessage} directly,
* or an awaited function which returns an {@linkplain https://discord.js.org/#/docs/main/stable/class/APIMessage APIMessage}.
*
* Furthermore, {@linkplain https://discord.js.org/#/docs/main/stable/typedef/MessageOptions MessageOptions} can be used to
* construct the pages without state, this library also provides {@link MessageBuilder}, which can be used as a chainable
* alternative to raw objects, similar to how {@linkplain https://discord.js.org/#/docs/main/stable/class/MessageEmbed MessageEmbed}
* works.
*
* @example
* ```typescript
* // Direct usage as a MessageBuilder
* new MessageBuilder().setContent('Test content!');
* ```
*
* @example
* ```typescript
* // An awaited function. This function also passes index, pages, and handler.
* (index, pages) =>
* new MessageBuilder().setEmbed(
* new MessageEmbed().setFooter(`Page ${index + 1} / ${pages.length}`)
* );
* ```
*
* @example
* ```typescript
* // Direct usage as an APIMessage
* new APIMessage(message.channel, {
* content: 'Test content!',
* });
* ```
*
* @example
* ```typescript
* // An awaited function. This function also passes index, pages, and handler.
* (index, pages) =>
* new APIMessage(message.channel, {
* embed: new MessageEmbed().setFooter(`Page ${index + 1} / ${pages.length}`)
* });
* ```
*/
declare type MessagePage = ((index: number, pages: MessagePage[], handler: PaginatedMessage) => Awaited<APIMessage | MessageOptions>) | APIMessage | MessageOptions;
/**
* This is a LazyPaginatedMessage. Instead of resolving all pages that are functions on {@link PaginatedMessage.run} will resolve when requested.
*/
declare class LazyPaginatedMessage extends PaginatedMessage {
/**
* Only resolves the page corresponding with the handler's current index.
*/
resolvePagesOnRun(channel: Message['channel']): Promise<void>;
/**
* Resolves the page corresponding with the given index. This also resolves the index's before and after the given index.
* @param index The index to resolve. Defaults to handler's current index.
*/
resolvePage(channel: Message['channel'], index: number): Promise<APIMessage>;
addPageBuilder(builder: MessageBuilder | ((builder: MessageBuilder) => MessageBuilder)): this;
addPageContent(content: string): this;
addPageEmbed(cb: MessageEmbed | ((builder: MessageEmbed) => MessageEmbed)): this;
}
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/CategoryChannel CategoryChannel}
* @param channel The channel to check
*/
declare function isCategoryChannel(channel: Channel): channel is CategoryChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/DMChannel DMChannel}
* @param channel The channel to check
*/
declare function isDMChannel(channel: Channel): channel is DMChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/PartialGroupDMChannel PartialGroupDMChannel}
* @param channel The channel to check
*/
declare function isGroupChannel(channel: Channel): channel is PartialGroupDMChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/GuildChannel GuildChannel}
* @param channel The channel to check
*/
declare function isGuildBasedChannel(channel: Channel): channel is GuildChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/NewsChannel NewsChannel}
* @param channel The channel to check
*/
declare function isNewsChannel(channel: Channel): channel is NewsChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/StoreChannel StoreChannel}
* @param channel The channel to check
*/
declare function isStoreChannel(channel: Channel): channel is StoreChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/TextChannel TextChannel}
* @param channel The channel to check
*/
declare function isTextChannel(channel: Channel): channel is TextChannel;
/**
* Checks whether a given channel is a {@linkplain https://discord.js.org/#/docs/main/stable/class/VoiceChannel VoiceChannel}
* @param channel The channel to check
*/
declare function isVoiceChannel(channel: Channel): channel is VoiceChannel;
export { IMessagePrompterConfirmStrategyOptions, IMessagePrompterExplicitConfirmReturn, IMessagePrompterExplicitMessageReturn, IMessagePrompterExplicitNumberReturn, IMessagePrompterExplicitReturnBase, IMessagePrompterNumberStrategyOptions, IMessagePrompterReactionStrategyOptions, IMessagePrompterStrategyOptions, IPaginatedMessageAction, LazyPaginatedMessage, MessageBuilder, MessageBuilderCodeResolvable, MessageBuilderFileResolvable, MessageBuilderResolvable, MessageBuilderSplitResolvable, MessagePage, MessagePrompter, MessagePrompterBaseStrategy, MessagePrompterConfirmStrategy, MessagePrompterMessage, MessagePrompterMessageStrategy, MessagePrompterNumberStrategy, MessagePrompterReactionStrategy, MessagePrompterStrategies, PaginatedMessage, PaginatedMessageActionContext, PaginatedMessageOptions, StrategyOptions, StrategyReturns, isCategoryChannel, isDMChannel, isGroupChannel, isGuildBasedChannel, isNewsChannel, isStoreChannel, isTextChannel, isVoiceChannel };

4

dist/index.js

@@ -1062,3 +1062,3 @@ 'use strict';

// Try to get the previous PaginatedMessage for this user
const paginatedMessage = PaginatedMessage.handlers.get(message.author.id);
const paginatedMessage = PaginatedMessage.handlers.get(target.id);
// If a PaginatedMessage was found then stop it

@@ -1068,3 +1068,3 @@ if (paginatedMessage)

// If the message was sent by a bot, then set the response as this one
if (target.bot)
if (message.author.bot)
this.response = message;

@@ -1071,0 +1071,0 @@ await this.resolvePagesOnRun(message.channel);

{
"name": "@sapphire/discord.js-utilities",
"version": "2.0.1-pr-137.e999494f.0",
"version": "2.0.1",
"description": "Discord.js specific utilities for your JavaScript/TypeScript bots",

@@ -20,3 +20,3 @@ "author": "@sapphire",

"start": "yarn build -w",
"prepublishOnly": "yarn build"
"prepublishOnly": "rollup-type-bundler -e @sapphire/discord-utilities @sapphire/time-utilities @sapphire/utilities discord.js"
},

@@ -58,3 +58,3 @@ "repository": {

},
"gitHead": "e999494f1d165f58a8366a9e344a14815a1f82a3"
"gitHead": "0a148e440d60640e8994cb42faeb439e1f2e1976"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc