From 7dcc71d2400086dcf3f94f6b8a1874ff17a31404 Mon Sep 17 00:00:00 2001 From: Nix Krystik Date: Sat, 16 May 2026 17:40:26 +0800 Subject: [PATCH] Added a cooldown with a default of 5 minutes. This can be adjusted to meet desired timing. --- src/context-menus/report.ts | 102 +++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 20 deletions(-) diff --git a/src/context-menus/report.ts b/src/context-menus/report.ts index 7a364f2..1d0b4d4 100644 --- a/src/context-menus/report.ts +++ b/src/context-menus/report.ts @@ -1,45 +1,107 @@ -import { ApplicationCommandType, ApplicationIntegrationType, Client, ContextMenuCommandBuilder, GuildTextBasedChannel, InteractionContextType, MessageContextMenuCommandInteraction, MessageFlags } from "discord.js"; +import { + ApplicationCommandType, + ApplicationIntegrationType, + Client, + ContextMenuCommandBuilder, + GuildTextBasedChannel, + InteractionContextType, + MessageContextMenuCommandInteraction, + MessageFlags +} from "discord.js"; -// TODO: Later Nix's problem: Command Cooldown. +import { Database } from "../shared/Database"; + +const cooldowns = new Map(); +const COOLDOWN_TIME = 300000; // 5 minutes export default { name: 'Report Message', + data: new ContextMenuCommandBuilder() .setName('Report Message') .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) .setContexts(InteractionContextType.Guild) .setType(ApplicationCommandType.Message), - - handler: async function (client: Client, interaction: MessageContextMenuCommandInteraction) { - try { - const reportsChannel = await client.channels.fetch("1504936076081369150") as GuildTextBasedChannel; - reportsChannel.send({ + handler: async function ( + client: Client, + interaction: MessageContextMenuCommandInteraction + ) { + try { + const userId = interaction.user.id; + const now = Date.now(); + + const cooldownExpiration = cooldowns.get(userId); + if (cooldownExpiration && now < cooldownExpiration) { + const expiresAt = Math.floor(cooldownExpiration / 1000); + return interaction.reply({ + embeds: [{ + color: 0xffaa00, + description: `Slow down there, you can report another message .` + }], + flags: [MessageFlags.Ephemeral] + }); + } + + // Set cooldown + cooldowns.set(userId, now + COOLDOWN_TIME); + + // Optional cleanup + setTimeout(() => { + cooldowns.delete(userId); + }, COOLDOWN_TIME); + + const guildSettings = await Database.getGuildSettings(interaction.guildId!); + + const reportsChannel = + await client.channels.fetch( + guildSettings?.moderator_channel_id! + ) as GuildTextBasedChannel; + + await reportsChannel.send({ embeds: [{ title: 'New Message Report!', fields: [ - { name: 'Message', value: interaction.targetMessage.url, inline: false }, - { name: 'Author', value: interaction.targetMessage.author.toString(), inline: false }, - { name: 'Reporter', value: interaction.user.toString(), inline: false } + { + name: 'Message', + value: interaction.targetMessage.url, + inline: false + }, + { + name: 'Author', + value: interaction.targetMessage.author.toString(), + inline: false + }, + { + name: 'Reporter', + value: interaction.user.toString(), + inline: false + } ] }] }); - interaction.reply({ + await interaction.reply({ embeds: [{ color: 0x014995, - description: "Thanks for making a report! Moderators have been notified and will investigate when they can next!" + description: + "Thanks for making a report! I've notified the moderators who can take further action." }], - flags: [MessageFlags.Ephemeral] }); + } catch (error) { - interaction.reply({ - embeds: [{ - color: 0xff5555, - description: "Hmm, an error occured while processing your request. If you receive this error more than once, DM a moderator for assistance." - }] - }) + console.error(error); + if (!interaction.replied) { + await interaction.reply({ + embeds: [{ + color: 0xff5555, + description: + "Hmm, an error occurred while processing your request. If you receive this error more than once, DM a moderator for further assistance." + }], + flags: [MessageFlags.Ephemeral] + }); + } } } -} \ No newline at end of file +}; \ No newline at end of file