From 57c3d603a9e212b1998ca1cdcb082329dc59149f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 16 Dec 2024 19:17:23 +0000 Subject: [PATCH 1/2] Add check for cooldown --- src/commands/effects.ts | 2 +- src/constants/EffectDetails.ts | 6 ++++-- src/helpers/EffectHelper.ts | 9 ++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/commands/effects.ts b/src/commands/effects.ts index db19b5e..c7059e8 100644 --- a/src/commands/effects.ts +++ b/src/commands/effects.ts @@ -93,6 +93,6 @@ export default class Effects extends Command { return; } - await interaction.reply("Unable to use effect! Please make sure you have it in your inventory"); + await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); } } diff --git a/src/constants/EffectDetails.ts b/src/constants/EffectDetails.ts index c59d33d..4b84dad 100644 --- a/src/constants/EffectDetails.ts +++ b/src/constants/EffectDetails.ts @@ -3,15 +3,17 @@ class EffectDetail { public readonly friendlyName: string; public readonly duration: number; public readonly cost: number; + public readonly cooldown: number; - constructor(id: string, friendlyName: string, duration: number, cost: number) { + constructor(id: string, friendlyName: string, duration: number, cost: number, cooldown: number) { this.id = id; this.friendlyName = friendlyName; this.duration = duration; this.cost = cost; + this.cooldown = cooldown; } }; export const EffectDetails = new Map([ - [ "unclaimed", new EffectDetail("unclaimed", "Unclaimed Chance Up", 24 * 60 * 60 * 1000, 100) ], + [ "unclaimed", new EffectDetail("unclaimed", "Unclaimed Chance Up", 10 * 60 * 1000, 100, 3 * 60 * 60 * 1000) ], ]); diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts index d0d29a0..3aaca15 100644 --- a/src/helpers/EffectHelper.ts +++ b/src/helpers/EffectHelper.ts @@ -1,6 +1,7 @@ import {ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder} from "discord.js"; import UserEffect from "../database/entities/app/UserEffect"; import EmbedColours from "../constants/EmbedColours"; +import {EffectDetails} from "../constants/EffectDetails"; export default class EffectHelper { public static async AddEffectToUserInventory(userId: string, name: string, quantity: number = 1) { @@ -23,7 +24,13 @@ export default class EffectHelper { return false; } - if (effect.WhenExpires && now < effect.WhenExpires) { + const effectDetail = EffectDetails.get(effect.Id); + + if (!effectDetail) { + return false; + } + + if (effect.WhenExpires && now < new Date(effect.WhenExpires.getMilliseconds() + effectDetail.cooldown)) { return false; } From d874cb7a12b16e2ad693f097f2b9bacab5c2eafd Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 16 Dec 2024 19:32:28 +0000 Subject: [PATCH 2/2] WIP: Start creating confirmation embed --- src/buttonEvents/Effects.ts | 55 +++++++++++++++++++++++++++++++++- src/commands/effects.ts | 44 +++++++++++++-------------- src/helpers/EffectHelper.ts | 20 ++++++++++--- src/helpers/TimeLengthInput.ts | 3 ++ 4 files changed, 94 insertions(+), 28 deletions(-) diff --git a/src/buttonEvents/Effects.ts b/src/buttonEvents/Effects.ts index 0810c94..8cefbf7 100644 --- a/src/buttonEvents/Effects.ts +++ b/src/buttonEvents/Effects.ts @@ -1,6 +1,7 @@ -import {ButtonInteraction} from "discord.js"; +import {ButtonInteraction,EmbedBuilder} from "discord.js"; import {ButtonEvent} from "../type/buttonEvent"; import EffectHelper from "../helpers/EffectHelper"; +import { EffectDetails } from "../constants/EffectDetails"; export default class Effects extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -10,6 +11,9 @@ export default class Effects extends ButtonEvent { case "list": await this.List(interaction); break; + case "use": + await this.Use(interaction); + break; } } @@ -30,4 +34,53 @@ export default class Effects extends ButtonEvent { components: [ result.row ], }); } + + private async Use(interaction: ButtonInteraction) { + const subaction = interaction.customId.split(" ")[2]; + + switch (subaction) { + case "confirm": + await this.UseConfirm(interaction); + break; + } + } + + private async UseConfirm(interaction: ButtonInteraction) { + const id = interaction.customId.split(" ")[3]; + + const effectDetail = EffectDetails.get(id); + + if (!effectDetail) { + await interaction.reply("Unable to find effect!"); + return; + } + + const now = new Date(); + const whenExpires = new Date(now.getMilliseconds() + effectDetail.duration); + + const result = await EffectHelper.UseEffect(interaction.user.id, id, whenExpires); + + if (result) { + const embed = new EmbedBuilder() + .setTitle("Effect Used") + .setDescription("You now have an active effect!") + .addFields([ + { + name: "Effect", + value: effectDetail.friendlyName, + inline: true, + }, + { + name: "Expires", + value: ``, + inline: true, + }, + ]); + + await interaction.update({ embeds: [ embed ] }); + return; + } + + await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); + } } diff --git a/src/commands/effects.ts b/src/commands/effects.ts index c7059e8..3fe30ef 100644 --- a/src/commands/effects.ts +++ b/src/commands/effects.ts @@ -2,6 +2,7 @@ import {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"; export default class Effects extends Command { constructor() { @@ -67,32 +68,29 @@ export default class Effects extends Command { return; } - const now = new Date(); - const whenExpires = new Date(now.getMilliseconds() + effectDetail.duration); + const canUseEffect = await EffectHelper.CanUseEffect(interaction.user.id, id) - const result = await EffectHelper.UseEffect(interaction.user.id, id, whenExpires); - - if (result) { - const embed = new EmbedBuilder() - .setTitle("Effect Used") - .setDescription("You now have an active effect!") - .addFields([ - { - name: "Effect", - value: effectDetail.friendlyName, - inline: true, - }, - { - name: "Expires", - value: ``, - inline: true, - }, - ]); - - await interaction.reply({ embeds: [ embed ] }); + if (!canUseEffect) { + await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); return; } - await interaction.reply("Unable to use effect! Please make sure you have it in your inventory and is not on cooldown"); + const embed = new EmbedBuilder() + .setTitle("Effect Confirmation") + .setDescription("Would you like to use this effect?") + .addFields([ + { + name: "Effect", + value: effectDetail.friendlyName, + inline: true, + }, + { + name: "Length", + value: "", + inline: true, + }, + ]); + + await interaction.reply({ embeds: [ embed ] }); } } diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts index 3aaca15..26aab8c 100644 --- a/src/helpers/EffectHelper.ts +++ b/src/helpers/EffectHelper.ts @@ -17,6 +17,22 @@ export default class EffectHelper { } public static async UseEffect(userId: string, name: string, whenExpires: Date): Promise { + const canUseEffect = await this.CanUseEffect(userId, name); + + if (!canUseEffect) return false; + + const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + + if (!effect) return false; + + effect.UseEffect(whenExpires); + + await effect.Save(UserEffect, effect); + + return true; + } + + public static async CanUseEffect(userId: string, name: string): Promise { const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); const now = new Date(); @@ -34,10 +50,6 @@ export default class EffectHelper { return false; } - effect.UseEffect(whenExpires); - - await effect.Save(UserEffect, effect); - return true; } diff --git a/src/helpers/TimeLengthInput.ts b/src/helpers/TimeLengthInput.ts index d1d8734..8fa232d 100644 --- a/src/helpers/TimeLengthInput.ts +++ b/src/helpers/TimeLengthInput.ts @@ -118,4 +118,7 @@ export default class TimeLengthInput { return desNumber; } + + public static ConvertFromMilliseconds(ms: number): TimeLengthInput { + } } \ No newline at end of file