3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-18 23:09:59 +00:00
zeppelin/backend/src/utils/waitForInteraction.ts

53 lines
1.8 KiB
TypeScript
Raw Normal View History

import { MessageActionRow, MessageButton, MessageComponentInteraction, MessageOptions, TextChannel } from "discord.js";
import { noop } from "knub/dist/utils";
import moment from "moment";
export async function waitForButtonConfirm(
channel: TextChannel,
toPost: MessageOptions,
options?: WaitForOptions,
): Promise<boolean> {
return new Promise(async resolve => {
const idMod = `${channel.guild.id}-${moment.utc().valueOf()}`;
const row = new MessageActionRow().addComponents([
new MessageButton()
.setStyle("SUCCESS")
.setLabel(options?.confirmText || "Confirm")
2021-07-04 23:14:12 +02:00
.setCustomId(`confirmButton:${idMod}`),
new MessageButton()
.setStyle("DANGER")
.setLabel(options?.cancelText || "Cancel")
2021-07-04 23:14:12 +02:00
.setCustomId(`cancelButton:${idMod}`),
]);
2021-06-30 04:56:56 +02:00
const message = await channel.send({ ...toPost, components: [row] });
const filter = (iac: MessageComponentInteraction) => iac.message.id === message.id;
2021-07-04 17:41:44 +02:00
const collector = message.createMessageComponentCollector({ filter, time: 10000 });
collector.on("collect", (interaction: MessageComponentInteraction) => {
if (options?.restrictToId && options.restrictToId !== interaction.user.id) {
2021-06-30 04:56:56 +02:00
interaction.reply({ content: `You are not permitted to use these buttons.`, ephemeral: true });
} else {
2021-07-04 23:14:12 +02:00
if (interaction.customId === `confirmButton:${idMod}`) {
message.delete();
resolve(true);
2021-07-04 23:14:12 +02:00
} else if (interaction.customId === `cancelButton:${idMod}`) {
message.delete();
resolve(false);
}
}
});
collector.on("end", () => {
if (!message.deleted) message.delete().catch(noop);
resolve(false);
});
});
}
export interface WaitForOptions {
restrictToId?: string;
confirmText?: string;
cancelText?: string;
}