From b37c0873935b228f8635f014ce3e4ed23107ba08 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 21 Dec 2024 15:46:18 +0000 Subject: [PATCH 1/2] Add confirmation button event --- src/commands/effects.ts | 22 ++++++++++++++++++---- src/helpers/TimeLengthInput.ts | 12 ++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/commands/effects.ts b/src/commands/effects.ts index 3fe30ef..13e7eeb 100644 --- a/src/commands/effects.ts +++ b/src/commands/effects.ts @@ -1,8 +1,9 @@ -import {CommandInteraction, EmbedBuilder, SlashCommandBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, SlashCommandBuilder} from "discord.js"; import {Command} from "../type/command"; import EffectHelper from "../helpers/EffectHelper"; import {EffectDetails} from "../constants/EffectDetails"; import UserEffect from "../database/entities/app/UserEffect"; +import TimeLengthInput from "../helpers/TimeLengthInput"; export default class Effects extends Command { constructor() { @@ -68,13 +69,15 @@ export default class Effects extends Command { return; } - const canUseEffect = await EffectHelper.CanUseEffect(interaction.user.id, id) + const canUseEffect = await EffectHelper.CanUseEffect(interaction.user.id, id); if (!canUseEffect) { await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); return; } + const timeLengthInput = TimeLengthInput.ConvertFromMilliseconds(effectDetail.duration); + const embed = new EmbedBuilder() .setTitle("Effect Confirmation") .setDescription("Would you like to use this effect?") @@ -86,11 +89,22 @@ export default class Effects extends Command { }, { name: "Length", - value: "", + value: timeLengthInput.GetLengthShort(), inline: true, }, ]); - await interaction.reply({ embeds: [ embed ] }); + const row = new ActionRowBuilder() + .addComponents([ + new ButtonBuilder() + .setLabel("Confirm") + .setCustomId(`effects use confirm ${effectDetail.id}`) + .setStyle(ButtonStyle.Primary), + ]); + + await interaction.reply({ + embeds: [ embed ], + components: [ row ], + }); } } diff --git a/src/helpers/TimeLengthInput.ts b/src/helpers/TimeLengthInput.ts index 8fa232d..aef58ba 100644 --- a/src/helpers/TimeLengthInput.ts +++ b/src/helpers/TimeLengthInput.ts @@ -120,5 +120,17 @@ export default class TimeLengthInput { } public static ConvertFromMilliseconds(ms: number): TimeLengthInput { + const seconds = Math.floor(ms / 1000); + const minutes = Math.floor(seconds / 60); + const hours = Math.floor(minutes / 60); + const days = Math.floor(hours / 24); + + const remainingSeconds = seconds % 60; + const remainingMinutes = minutes % 60; + const remainingHours = hours % 24; + + const timeString = `${days}d ${remainingHours}h ${remainingMinutes}m ${remainingSeconds}s`; + + return new TimeLengthInput(timeString); } } \ No newline at end of file From dd1f25917062b527826c84793c53a160966530e6 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 21 Dec 2024 15:55:04 +0000 Subject: [PATCH 2/2] Add cancel button event --- src/buttonEvents/Effects.ts | 78 +++++++++++++++++++++++++++++++++++-- src/commands/effects.ts | 6 +++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/buttonEvents/Effects.ts b/src/buttonEvents/Effects.ts index 8cefbf7..2a318ec 100644 --- a/src/buttonEvents/Effects.ts +++ b/src/buttonEvents/Effects.ts @@ -1,7 +1,9 @@ -import {ButtonInteraction,EmbedBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonInteraction,ButtonStyle,Embed,EmbedBuilder} from "discord.js"; import {ButtonEvent} from "../type/buttonEvent"; import EffectHelper from "../helpers/EffectHelper"; import { EffectDetails } from "../constants/EffectDetails"; +import TimeLengthInput from "../helpers/TimeLengthInput"; +import EmbedColours from "../constants/EmbedColours"; export default class Effects extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -28,7 +30,7 @@ export default class Effects extends ButtonEvent { } const result = await EffectHelper.GenerateEffectEmbed(interaction.user.id, page); - + await interaction.update({ embeds: [ result.embed ], components: [ result.row ], @@ -42,6 +44,9 @@ export default class Effects extends ButtonEvent { case "confirm": await this.UseConfirm(interaction); break; + case "cancel": + await this.UseCancel(interaction); + break; } } @@ -64,6 +69,7 @@ export default class Effects extends ButtonEvent { const embed = new EmbedBuilder() .setTitle("Effect Used") .setDescription("You now have an active effect!") + .setColor(EmbedColours.Green) .addFields([ { name: "Effect", @@ -77,10 +83,76 @@ export default class Effects extends ButtonEvent { }, ]); - await interaction.update({ embeds: [ embed ] }); + const row = new ActionRowBuilder() + .addComponents([ + new ButtonBuilder() + .setLabel("Confirm") + .setCustomId(`effects use confirm ${effectDetail.id}`) + .setStyle(ButtonStyle.Primary) + .setDisabled(true), + new ButtonBuilder() + .setLabel("Cancel") + .setCustomId(`effects use cancel ${effectDetail.id}`) + .setStyle(ButtonStyle.Danger) + .setDisabled(true), + ]); + + await interaction.update({ + embeds: [ embed ], + components: [ row ], + }); return; } await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); } + + private async UseCancel(interaction: ButtonInteraction) { + const id = interaction.customId.split(" ")[3]; + + const effectDetail = EffectDetails.get(id); + + if (!effectDetail) { + await interaction.reply("Unable to find effect!"); + return; + } + + const timeLengthInput = TimeLengthInput.ConvertFromMilliseconds(effectDetail.duration); + + const embed = new EmbedBuilder() + .setTitle("Effect Use Cancelled") + .setDescription("The effect from your inventory has not been used") + .setColor(EmbedColours.Grey) + .addFields([ + { + name: "Effect", + value: effectDetail.friendlyName, + inline: true, + }, + { + name: "Expires", + value: timeLengthInput.GetLengthShort(), + inline: true, + }, + ]); + + const row = new ActionRowBuilder() + .addComponents([ + new ButtonBuilder() + .setLabel("Confirm") + .setCustomId(`effects use confirm ${effectDetail.id}`) + .setStyle(ButtonStyle.Primary) + .setDisabled(true), + new ButtonBuilder() + .setLabel("Cancel") + .setCustomId(`effects use cancel ${effectDetail.id}`) + .setStyle(ButtonStyle.Danger) + .setDisabled(true), + ]); + + await interaction.update({ + embeds: [ embed ], + components: [ row ], + }); + } } diff --git a/src/commands/effects.ts b/src/commands/effects.ts index 13e7eeb..55f0da9 100644 --- a/src/commands/effects.ts +++ b/src/commands/effects.ts @@ -4,6 +4,7 @@ import EffectHelper from "../helpers/EffectHelper"; import {EffectDetails} from "../constants/EffectDetails"; import UserEffect from "../database/entities/app/UserEffect"; import TimeLengthInput from "../helpers/TimeLengthInput"; +import EmbedColours from "../constants/EmbedColours"; export default class Effects extends Command { constructor() { @@ -81,6 +82,7 @@ export default class Effects extends Command { const embed = new EmbedBuilder() .setTitle("Effect Confirmation") .setDescription("Would you like to use this effect?") + .setColor(EmbedColours.Ok) .addFields([ { name: "Effect", @@ -100,6 +102,10 @@ export default class Effects extends Command { .setLabel("Confirm") .setCustomId(`effects use confirm ${effectDetail.id}`) .setStyle(ButtonStyle.Primary), + new ButtonBuilder() + .setLabel("Cancel") + .setCustomId(`effects use cancel ${effectDetail.id}`) + .setStyle(ButtonStyle.Danger), ]); await interaction.reply({