From 48c59120391c2632a989ee1e564bf749a90b720a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A7=81Ash=C3=BB=EA=A7=82?= <30575805+Ashu11-A@users.noreply.github.com> Date: Thu, 13 Jun 2024 22:03:30 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95=20New=20custom=20interaction=20(Yo?= =?UTF-8?q?uSure)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/discord/base/CustomIntetaction.ts | 49 ++++++++++++++++++- .../src/discord/base/CustomIntetaction.ts | 49 ++++++++++++++++++- .../src/discord/base/CustomIntetaction.ts | 49 ++++++++++++++++++- 3 files changed, 144 insertions(+), 3 deletions(-) diff --git a/plugins/plugin_base/src/discord/base/CustomIntetaction.ts b/plugins/plugin_base/src/discord/base/CustomIntetaction.ts index 4fadb398..090bd759 100644 --- a/plugins/plugin_base/src/discord/base/CustomIntetaction.ts +++ b/plugins/plugin_base/src/discord/base/CustomIntetaction.ts @@ -1,5 +1,6 @@ +import { ActionDrawer } from "@/functions/actionDrawer.js"; import { packageData } from "@/index.js"; -import { ActionRowBuilder, ButtonBuilder as Button, ButtonStyle, ComponentEmojiResolvable, ModalBuilder as Modal, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, TextInputBuilder } from "discord.js"; +import { ActionRowBuilder, ButtonBuilder as Button, ButtonInteraction, ButtonStyle, CacheType, CommandInteraction, ComponentEmojiResolvable, ComponentType, EmbedBuilder, ModalBuilder as Modal, ModalSubmitInteraction, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, StringSelectMenuInteraction, TextInputBuilder } from "discord.js"; export interface BaseButtonComponentData { customId?: string @@ -67,4 +68,50 @@ export class StringSelectMenuBuilder extends StringSelect { if (minValues) this.setMinValues(minValues) if (placeholder) this.setPlaceholder(placeholder) } +} + +type Interaction = CommandInteraction | ModalSubmitInteraction | ButtonInteraction | StringSelectMenuInteraction +export class YouSure { + private readonly interaction: Interaction + private readonly title?: string + constructor ({ interaction, title }: { interaction: Interaction, title: string }) { + this.interaction = interaction + this.title = title + } + + async question () { + return await new Promise(async (resolve, reject) => { + const embed = new EmbedBuilder({ title: this.title ?? 'Tem certeza que deseja fazer isso?' }).setColor('Orange') + const buttons = ActionDrawer([ + new Button({ customId: 'confirm-button', label: 'Confirmar', style: ButtonStyle.Success }), + new Button({ customId: 'cancel-button', label: 'Cancelar', style: ButtonStyle.Danger }) + ]) + const message = this.interaction.deferred + ? await this.interaction.editReply({ embeds: [embed], components: buttons }) + : await this.interaction.reply({ embeds: [embed], components: buttons }) + + const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, maxUsers: 1 }) + collector.once('collect', async (subInteraction) => { + switch (subInteraction.customId) { + case 'confirm-button': { + resolve(true) + break + } + case 'cancel-button': { + if (subInteraction.isRepliable()) { + const embed = new EmbedBuilder({ + title: 'Ação cancelada!' + }).setColor('Red') + await subInteraction.update({ embeds: [embed], components: [] }).catch(async () => { + if (subInteraction.deferred) { await this.interaction.editReply({ embeds: [embed], components: [] }); return } + await this.interaction.reply({ ephemeral: true, embeds: [embed], components: [] }) + }) + setTimeout(async () => await subInteraction.deleteReply().catch(() => undefined), 5000) + } + reject(false) + } + } + }) + }) + } } \ No newline at end of file diff --git a/plugins/tickets/src/discord/base/CustomIntetaction.ts b/plugins/tickets/src/discord/base/CustomIntetaction.ts index 4fadb398..090bd759 100644 --- a/plugins/tickets/src/discord/base/CustomIntetaction.ts +++ b/plugins/tickets/src/discord/base/CustomIntetaction.ts @@ -1,5 +1,6 @@ +import { ActionDrawer } from "@/functions/actionDrawer.js"; import { packageData } from "@/index.js"; -import { ActionRowBuilder, ButtonBuilder as Button, ButtonStyle, ComponentEmojiResolvable, ModalBuilder as Modal, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, TextInputBuilder } from "discord.js"; +import { ActionRowBuilder, ButtonBuilder as Button, ButtonInteraction, ButtonStyle, CacheType, CommandInteraction, ComponentEmojiResolvable, ComponentType, EmbedBuilder, ModalBuilder as Modal, ModalSubmitInteraction, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, StringSelectMenuInteraction, TextInputBuilder } from "discord.js"; export interface BaseButtonComponentData { customId?: string @@ -67,4 +68,50 @@ export class StringSelectMenuBuilder extends StringSelect { if (minValues) this.setMinValues(minValues) if (placeholder) this.setPlaceholder(placeholder) } +} + +type Interaction = CommandInteraction | ModalSubmitInteraction | ButtonInteraction | StringSelectMenuInteraction +export class YouSure { + private readonly interaction: Interaction + private readonly title?: string + constructor ({ interaction, title }: { interaction: Interaction, title: string }) { + this.interaction = interaction + this.title = title + } + + async question () { + return await new Promise(async (resolve, reject) => { + const embed = new EmbedBuilder({ title: this.title ?? 'Tem certeza que deseja fazer isso?' }).setColor('Orange') + const buttons = ActionDrawer([ + new Button({ customId: 'confirm-button', label: 'Confirmar', style: ButtonStyle.Success }), + new Button({ customId: 'cancel-button', label: 'Cancelar', style: ButtonStyle.Danger }) + ]) + const message = this.interaction.deferred + ? await this.interaction.editReply({ embeds: [embed], components: buttons }) + : await this.interaction.reply({ embeds: [embed], components: buttons }) + + const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, maxUsers: 1 }) + collector.once('collect', async (subInteraction) => { + switch (subInteraction.customId) { + case 'confirm-button': { + resolve(true) + break + } + case 'cancel-button': { + if (subInteraction.isRepliable()) { + const embed = new EmbedBuilder({ + title: 'Ação cancelada!' + }).setColor('Red') + await subInteraction.update({ embeds: [embed], components: [] }).catch(async () => { + if (subInteraction.deferred) { await this.interaction.editReply({ embeds: [embed], components: [] }); return } + await this.interaction.reply({ ephemeral: true, embeds: [embed], components: [] }) + }) + setTimeout(async () => await subInteraction.deleteReply().catch(() => undefined), 5000) + } + reject(false) + } + } + }) + }) + } } \ No newline at end of file diff --git a/plugins/utils/src/discord/base/CustomIntetaction.ts b/plugins/utils/src/discord/base/CustomIntetaction.ts index 4fadb398..090bd759 100644 --- a/plugins/utils/src/discord/base/CustomIntetaction.ts +++ b/plugins/utils/src/discord/base/CustomIntetaction.ts @@ -1,5 +1,6 @@ +import { ActionDrawer } from "@/functions/actionDrawer.js"; import { packageData } from "@/index.js"; -import { ActionRowBuilder, ButtonBuilder as Button, ButtonStyle, ComponentEmojiResolvable, ModalBuilder as Modal, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, TextInputBuilder } from "discord.js"; +import { ActionRowBuilder, ButtonBuilder as Button, ButtonInteraction, ButtonStyle, CacheType, CommandInteraction, ComponentEmojiResolvable, ComponentType, EmbedBuilder, ModalBuilder as Modal, ModalSubmitInteraction, SelectMenuComponentOptionData, StringSelectMenuBuilder as StringSelect, StringSelectMenuInteraction, TextInputBuilder } from "discord.js"; export interface BaseButtonComponentData { customId?: string @@ -67,4 +68,50 @@ export class StringSelectMenuBuilder extends StringSelect { if (minValues) this.setMinValues(minValues) if (placeholder) this.setPlaceholder(placeholder) } +} + +type Interaction = CommandInteraction | ModalSubmitInteraction | ButtonInteraction | StringSelectMenuInteraction +export class YouSure { + private readonly interaction: Interaction + private readonly title?: string + constructor ({ interaction, title }: { interaction: Interaction, title: string }) { + this.interaction = interaction + this.title = title + } + + async question () { + return await new Promise(async (resolve, reject) => { + const embed = new EmbedBuilder({ title: this.title ?? 'Tem certeza que deseja fazer isso?' }).setColor('Orange') + const buttons = ActionDrawer([ + new Button({ customId: 'confirm-button', label: 'Confirmar', style: ButtonStyle.Success }), + new Button({ customId: 'cancel-button', label: 'Cancelar', style: ButtonStyle.Danger }) + ]) + const message = this.interaction.deferred + ? await this.interaction.editReply({ embeds: [embed], components: buttons }) + : await this.interaction.reply({ embeds: [embed], components: buttons }) + + const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, maxUsers: 1 }) + collector.once('collect', async (subInteraction) => { + switch (subInteraction.customId) { + case 'confirm-button': { + resolve(true) + break + } + case 'cancel-button': { + if (subInteraction.isRepliable()) { + const embed = new EmbedBuilder({ + title: 'Ação cancelada!' + }).setColor('Red') + await subInteraction.update({ embeds: [embed], components: [] }).catch(async () => { + if (subInteraction.deferred) { await this.interaction.editReply({ embeds: [embed], components: [] }); return } + await this.interaction.reply({ ephemeral: true, embeds: [embed], components: [] }) + }) + setTimeout(async () => await subInteraction.deleteReply().catch(() => undefined), 5000) + } + reject(false) + } + } + }) + }) + } } \ No newline at end of file