mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
feat: promisified single interaction collection (#5770)
This commit is contained in:
@@ -49,6 +49,8 @@ const Messages = {
|
|||||||
BUTTON_URL: 'MessageButton url must be a string',
|
BUTTON_URL: 'MessageButton url must be a string',
|
||||||
BUTTON_CUSTOM_ID: 'MessageButton customID must be a string',
|
BUTTON_CUSTOM_ID: 'MessageButton customID must be a string',
|
||||||
|
|
||||||
|
INTERACTION_COLLECTOR_TIMEOUT: 'Collector timed out without receiving any interactions',
|
||||||
|
|
||||||
FILE_NOT_FOUND: file => `File could not be found: ${file}`,
|
FILE_NOT_FOUND: file => `File could not be found: ${file}`,
|
||||||
|
|
||||||
USER_NO_DMCHANNEL: 'No DM Channel exists!',
|
USER_NO_DMCHANNEL: 'No DM Channel exists!',
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const MessageCollector = require('../MessageCollector');
|
|||||||
const APIMessage = require('../APIMessage');
|
const APIMessage = require('../APIMessage');
|
||||||
const SnowflakeUtil = require('../../util/SnowflakeUtil');
|
const SnowflakeUtil = require('../../util/SnowflakeUtil');
|
||||||
const Collection = require('../../util/Collection');
|
const Collection = require('../../util/Collection');
|
||||||
const { RangeError, TypeError } = require('../../errors');
|
const { RangeError, TypeError, Error } = require('../../errors');
|
||||||
const MessageComponentInteractionCollector = require('../MessageComponentInteractionCollector');
|
const MessageComponentInteractionCollector = require('../MessageComponentInteractionCollector');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -335,24 +335,25 @@ class TextBasedChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Similar to createMessageComponentInteractionCollector but in promise form.
|
* Collects a single component interaction that passes the filter.
|
||||||
* Resolves with a collection of interactions that pass the specified filter.
|
* The Promise will reject if the time expires.
|
||||||
* @param {CollectorFilter} filter The filter function to use
|
* @param {CollectorFilter} filter The filter function to use
|
||||||
* @param {AwaitMessageComponentInteractionsOptions} [options={}] Optional options to pass to the internal collector
|
* @param {number} [time] Time to wait for an interaction before rejecting
|
||||||
* @returns {Promise<Collection<string, MessageComponentInteraction>>}
|
* @returns {Promise<MessageComponentInteraction>}
|
||||||
* @example
|
* @example
|
||||||
* // Create a button interaction collector
|
* // Collect a button interaction
|
||||||
* const filter = (interaction) => interaction.customID === 'button' && interaction.user.id === 'someID';
|
* const filter = (interaction) => interaction.customID === 'button' && interaction.user.id === 'someID';
|
||||||
* channel.awaitMessageComponentInteractions(filter, { time: 15000 })
|
* channel.awaitMessageComponentInteraction(filter, 15000)
|
||||||
* .then(collected => console.log(`Collected ${collected.size} interactions`))
|
* .then(interaction => console.log(`${interaction.customID} was clicked!`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
awaitMessageComponentInteractions(filter, options = {}) {
|
awaitMessageComponentInteraction(filter, time) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const collector = this.createMessageComponentInteractionCollector(filter, options);
|
const collector = this.createMessageComponentInteractionCollector(filter, { max: 1, time });
|
||||||
collector.once('end', (interactions, reason) => {
|
collector.once('end', interactions => {
|
||||||
if (options.errors && options.errors.includes(reason)) reject(interactions);
|
const interaction = interactions.first();
|
||||||
else resolve(interactions);
|
if (!interaction) reject(new Error('INTERACTION_COLLECTOR_TIMEOUT'));
|
||||||
|
else resolve(interaction);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
18
typings/index.d.ts
vendored
18
typings/index.d.ts
vendored
@@ -1231,10 +1231,10 @@ declare module 'discord.js' {
|
|||||||
public webhookID: Snowflake | null;
|
public webhookID: Snowflake | null;
|
||||||
public flags: Readonly<MessageFlags>;
|
public flags: Readonly<MessageFlags>;
|
||||||
public reference: MessageReference | null;
|
public reference: MessageReference | null;
|
||||||
public awaitMessageComponentInteractions(
|
public awaitMessageComponentInteraction(
|
||||||
filter: CollectorFilter<[MessageComponentInteraction]>,
|
filter: CollectorFilter<[MessageComponentInteraction]>,
|
||||||
options?: AwaitMessageComponentInteractionsOptions,
|
time?: number,
|
||||||
): Promise<Collection<Snowflake, MessageComponentInteraction>>;
|
): Promise<MessageComponentInteraction>;
|
||||||
public awaitReactions(
|
public awaitReactions(
|
||||||
filter: CollectorFilter<[MessageReaction, User]>,
|
filter: CollectorFilter<[MessageReaction, User]>,
|
||||||
options?: AwaitReactionsOptions,
|
options?: AwaitReactionsOptions,
|
||||||
@@ -1245,7 +1245,7 @@ declare module 'discord.js' {
|
|||||||
): ReactionCollector;
|
): ReactionCollector;
|
||||||
public createMessageComponentInteractionCollector(
|
public createMessageComponentInteractionCollector(
|
||||||
filter: CollectorFilter<[MessageComponentInteraction]>,
|
filter: CollectorFilter<[MessageComponentInteraction]>,
|
||||||
options?: AwaitMessageComponentInteractionsOptions,
|
options?: MessageComponentInteractionCollectorOptions,
|
||||||
): MessageComponentInteractionCollector;
|
): MessageComponentInteractionCollector;
|
||||||
public delete(): Promise<Message>;
|
public delete(): Promise<Message>;
|
||||||
public edit(
|
public edit(
|
||||||
@@ -2503,10 +2503,10 @@ declare module 'discord.js' {
|
|||||||
readonly lastPinAt: Date | null;
|
readonly lastPinAt: Date | null;
|
||||||
typing: boolean;
|
typing: boolean;
|
||||||
typingCount: number;
|
typingCount: number;
|
||||||
awaitMessageComponentInteractions(
|
awaitMessageComponentInteraction(
|
||||||
filter: CollectorFilter<[MessageComponentInteraction]>,
|
filter: CollectorFilter<[MessageComponentInteraction]>,
|
||||||
options?: AwaitMessageComponentInteractionsOptions,
|
time?: number,
|
||||||
): Promise<Collection<Snowflake, MessageComponentInteraction>>;
|
): Promise<MessageComponentInteraction>;
|
||||||
awaitMessages(
|
awaitMessages(
|
||||||
filter: CollectorFilter<[Message]>,
|
filter: CollectorFilter<[Message]>,
|
||||||
options?: AwaitMessagesOptions,
|
options?: AwaitMessagesOptions,
|
||||||
@@ -2729,10 +2729,6 @@ declare module 'discord.js' {
|
|||||||
new?: any;
|
new?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AwaitMessageComponentInteractionsOptions extends MessageComponentInteractionCollectorOptions {
|
|
||||||
errors?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AwaitMessagesOptions extends MessageCollectorOptions {
|
interface AwaitMessagesOptions extends MessageCollectorOptions {
|
||||||
errors?: string[];
|
errors?: string[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user