2023-04-01 12:58:17 +01:00
|
|
|
import {
|
|
|
|
ActionRowBuilder,
|
|
|
|
ButtonBuilder,
|
|
|
|
ButtonStyle,
|
|
|
|
GuildTextBasedChannel,
|
|
|
|
MessageActionRowComponentBuilder,
|
|
|
|
MessageComponentInteraction,
|
|
|
|
MessageCreateOptions,
|
|
|
|
} from "discord.js";
|
2021-06-02 23:41:05 +02:00
|
|
|
import moment from "moment";
|
2021-08-18 21:01:57 +03:00
|
|
|
import uuidv4 from "uuid/v4";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { noop } from "../utils";
|
2021-06-02 23:41:05 +02:00
|
|
|
|
|
|
|
export async function waitForButtonConfirm(
|
2023-04-01 12:58:17 +01:00
|
|
|
channel: GuildTextBasedChannel,
|
|
|
|
toPost: MessageCreateOptions,
|
2021-06-02 23:41:05 +02:00
|
|
|
options?: WaitForOptions,
|
|
|
|
): Promise<boolean> {
|
2021-09-11 19:06:51 +03:00
|
|
|
return new Promise(async (resolve) => {
|
2021-06-02 23:41:05 +02:00
|
|
|
const idMod = `${channel.guild.id}-${moment.utc().valueOf()}`;
|
2023-04-01 12:58:17 +01:00
|
|
|
const row = new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents([
|
|
|
|
new ButtonBuilder()
|
|
|
|
.setStyle(ButtonStyle.Success)
|
2021-06-02 23:41:05 +02:00
|
|
|
.setLabel(options?.confirmText || "Confirm")
|
2021-08-18 21:01:57 +03:00
|
|
|
.setCustomId(`confirmButton:${idMod}:${uuidv4()}`),
|
2021-06-02 23:41:05 +02:00
|
|
|
|
2023-04-01 12:58:17 +01:00
|
|
|
new ButtonBuilder()
|
|
|
|
.setStyle(ButtonStyle.Danger)
|
2021-06-02 23:41:05 +02:00
|
|
|
.setLabel(options?.cancelText || "Cancel")
|
2021-08-18 21:01:57 +03:00
|
|
|
.setCustomId(`cancelButton:${idMod}:${uuidv4()}`),
|
2021-06-02 23:41:05 +02:00
|
|
|
]);
|
2021-06-30 04:56:56 +02:00
|
|
|
const message = await channel.send({ ...toPost, components: [row] });
|
2021-06-02 23:41:05 +02:00
|
|
|
|
2021-07-29 17:24:53 +01:00
|
|
|
const collector = message.createMessageComponentCollector({ time: 10000 });
|
2021-06-02 23:41:05 +02:00
|
|
|
|
|
|
|
collector.on("collect", (interaction: MessageComponentInteraction) => {
|
|
|
|
if (options?.restrictToId && options.restrictToId !== interaction.user.id) {
|
2022-06-01 18:58:54 +03:00
|
|
|
interaction
|
|
|
|
.reply({ content: `You are not permitted to use these buttons.`, ephemeral: true })
|
2023-04-01 12:58:17 +01:00
|
|
|
// tslint:disable-next-line no-console
|
2022-06-01 18:58:54 +03:00
|
|
|
.catch((err) => console.trace(err.message));
|
2021-06-02 23:41:05 +02:00
|
|
|
} else {
|
2021-08-18 21:01:57 +03:00
|
|
|
if (interaction.customId.startsWith(`confirmButton:${idMod}:`)) {
|
2021-06-02 23:41:05 +02:00
|
|
|
message.delete();
|
|
|
|
resolve(true);
|
2021-08-18 21:01:57 +03:00
|
|
|
} else if (interaction.customId.startsWith(`cancelButton:${idMod}:`)) {
|
2021-06-02 23:41:05 +02:00
|
|
|
message.delete();
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
collector.on("end", () => {
|
2023-04-01 12:58:17 +01:00
|
|
|
if (message.deletable) message.delete().catch(noop);
|
2021-06-02 23:41:05 +02:00
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface WaitForOptions {
|
|
|
|
restrictToId?: string;
|
|
|
|
confirmText?: string;
|
|
|
|
cancelText?: string;
|
|
|
|
}
|