From e2233097965de410acde563444f8a0b5dd4acd97 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Thu, 1 Jun 2023 12:13:59 +0200 Subject: [PATCH] feat: :sparkles: ability to set prorata cooldowns This should solve issues where some commands wants prorata cooldowns, that expire on midnight and such Ability to set a cooldown using seconds, now you need to call a Date constructing function. So you always pass a date as cooldownTime --- .../credits/groups/bonus/subcommands/daily/index.ts | 7 +++---- .../credits/groups/bonus/subcommands/monthly/index.ts | 7 +++---- .../credits/groups/bonus/subcommands/weekly/index.ts | 7 +++---- src/commands/credits/subcommands/work/index.ts | 3 ++- src/commands/dns/subcommands/lookup/index.ts | 8 +++----- src/commands/fun/subcommands/meme/index.ts | 7 +++---- src/commands/quotes/subcommands/post/index.ts | 3 ++- src/commands/reputation/subcommands/repute/index.ts | 3 ++- src/events/messageCreate/components/earnCredits.ts | 8 +++++++- src/handlers/CooldownManager.ts | 3 +-- 10 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/commands/credits/groups/bonus/subcommands/daily/index.ts b/src/commands/credits/groups/bonus/subcommands/daily/index.ts index 4ba70e18..db474233 100644 --- a/src/commands/credits/groups/bonus/subcommands/daily/index.ts +++ b/src/commands/credits/groups/bonus/subcommands/daily/index.ts @@ -1,3 +1,4 @@ +import { addDays } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -60,12 +61,10 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { await sendResponse(interaction, { embeds: [embed] }); - const cooldownDuration = 24 * 60 * 60; // 24 hours in seconds - const cooldownName = await generateCooldownName(interaction); await cooldownManager.setCooldown( - cooldownName, + await generateCooldownName(interaction), guild, user, - cooldownDuration + addDays(new Date(), 1) ); }; diff --git a/src/commands/credits/groups/bonus/subcommands/monthly/index.ts b/src/commands/credits/groups/bonus/subcommands/monthly/index.ts index 51d58bfb..26768d73 100644 --- a/src/commands/credits/groups/bonus/subcommands/monthly/index.ts +++ b/src/commands/credits/groups/bonus/subcommands/monthly/index.ts @@ -1,3 +1,4 @@ +import { addMonths } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -66,12 +67,10 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { await sendResponse(interaction, { embeds: [embed] }); - const cooldownDuration = 4 * 7 * 24 * 60 * 60; // 1 month in seconds - const cooldownName = await generateCooldownName(interaction); await cooldownManager.setCooldown( - cooldownName, + await generateCooldownName(interaction), guild, user, - cooldownDuration + addMonths(new Date(), 1) ); }; diff --git a/src/commands/credits/groups/bonus/subcommands/weekly/index.ts b/src/commands/credits/groups/bonus/subcommands/weekly/index.ts index 7efeedb6..da7bd3e8 100644 --- a/src/commands/credits/groups/bonus/subcommands/weekly/index.ts +++ b/src/commands/credits/groups/bonus/subcommands/weekly/index.ts @@ -1,3 +1,4 @@ +import { addWeeks } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -62,12 +63,10 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { await sendResponse(interaction, { embeds: [embed] }); - const cooldownDuration = 7 * 24 * 60 * 60; // 1 week in seconds - const cooldownName = await generateCooldownName(interaction); await cooldownManager.setCooldown( - cooldownName, + await generateCooldownName(interaction), guild, user, - cooldownDuration + addWeeks(new Date(), 1) ); }; diff --git a/src/commands/credits/subcommands/work/index.ts b/src/commands/credits/subcommands/work/index.ts index 88e372af..487b6fa8 100644 --- a/src/commands/credits/subcommands/work/index.ts +++ b/src/commands/credits/subcommands/work/index.ts @@ -1,4 +1,5 @@ import Chance from "chance"; +import { addHours } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -132,6 +133,6 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { await generateCooldownName(interaction), guild, user, - 24 * 60 * 60 + addHours(new Date(), 1) ); }; diff --git a/src/commands/dns/subcommands/lookup/index.ts b/src/commands/dns/subcommands/lookup/index.ts index d6d930b8..db671979 100644 --- a/src/commands/dns/subcommands/lookup/index.ts +++ b/src/commands/dns/subcommands/lookup/index.ts @@ -1,4 +1,5 @@ import axios from "axios"; +import { addSeconds } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -70,14 +71,11 @@ export const execute = async ( ], }); - const cooldownName = await generateCooldownName(interaction); - const cooldownDuration = 5; - await cooldownManager.setCooldown( - cooldownName, + await generateCooldownName(interaction), guild || null, user, - cooldownDuration + addSeconds(new Date(), 5) ); } catch (error: unknown) { if ((error as NodeJS.ErrnoException).code === "ENOTFOUND") { diff --git a/src/commands/fun/subcommands/meme/index.ts b/src/commands/fun/subcommands/meme/index.ts index bd6d36f3..1478ef06 100644 --- a/src/commands/fun/subcommands/meme/index.ts +++ b/src/commands/fun/subcommands/meme/index.ts @@ -1,4 +1,5 @@ import axios from "axios"; +import { addSeconds } from "date-fns"; import { ActionRowBuilder, ButtonBuilder, @@ -39,8 +40,6 @@ export const execute = async ( await deferReply(interaction, false); const { channel, guild, user } = interaction; - const cooldownItem = await generateCooldownName(interaction); - const cooldownDuration = 15; // 10 seconds try { const content: MemeContent = await fetchRandomMeme(); @@ -65,10 +64,10 @@ export const execute = async ( } await cooldownManager.setCooldown( - cooldownItem, + await generateCooldownName(interaction), guild || null, user, - cooldownDuration + addSeconds(new Date(), 5) ); }; diff --git a/src/commands/quotes/subcommands/post/index.ts b/src/commands/quotes/subcommands/post/index.ts index f5ec8281..8ecb944a 100644 --- a/src/commands/quotes/subcommands/post/index.ts +++ b/src/commands/quotes/subcommands/post/index.ts @@ -1,3 +1,4 @@ +import { addMinutes } from "date-fns"; import { ChannelType, ChatInputCommandInteraction, @@ -99,6 +100,6 @@ export const execute = async ( await generateCooldownName(interaction), guild, user, - 5 * 60 + addMinutes(new Date(), 5) ); }; diff --git a/src/commands/reputation/subcommands/repute/index.ts b/src/commands/reputation/subcommands/repute/index.ts index 180c6c3a..3708b113 100644 --- a/src/commands/reputation/subcommands/repute/index.ts +++ b/src/commands/reputation/subcommands/repute/index.ts @@ -1,3 +1,4 @@ +import { addDays } from "date-fns"; import { ChatInputCommandInteraction, EmbedBuilder, @@ -82,6 +83,6 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { await generateCooldownName(interaction), guild, user, - 24 * 60 * 60 + addDays(new Date(), 1) ); }; diff --git a/src/events/messageCreate/components/earnCredits.ts b/src/events/messageCreate/components/earnCredits.ts index 35798229..d79647c5 100644 --- a/src/events/messageCreate/components/earnCredits.ts +++ b/src/events/messageCreate/components/earnCredits.ts @@ -1,3 +1,4 @@ +import { addSeconds } from "date-fns"; import { Channel, ChannelType, Guild, Message, User } from "discord.js"; import CooldownManager from "../../../handlers/CooldownManager"; import CreditsManager from "../../../handlers/CreditsManager"; @@ -93,5 +94,10 @@ async function isUserOnCooldown(guild: Guild, author: User): Promise { } async function setCooldown(guild: Guild, user: User) { - await cooldownManager.setCooldown(cooldownName, guild, user, 5); + await cooldownManager.setCooldown( + cooldownName, + guild, + user, + addSeconds(new Date(), 5) + ); } diff --git a/src/handlers/CooldownManager.ts b/src/handlers/CooldownManager.ts index 2a9a2d88..0f713573 100644 --- a/src/handlers/CooldownManager.ts +++ b/src/handlers/CooldownManager.ts @@ -8,9 +8,8 @@ class CooldownManager { cooldownItem: string, guild: Guild | null, user: User | null, - cooldownSeconds: number + expiresAt: Date ): Promise { - const expiresAt = new Date(Date.now() + cooldownSeconds * 1000); const data = { cooldownItem, expiresAt,