Added a cooldown with a default of 5 minutes. This can be adjusted to meet desired timing.

This commit is contained in:
Nix Krystik
2026-05-16 17:40:26 +08:00
parent e9c45b1907
commit 7dcc71d240
+80 -18
View File
@@ -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<string, number>();
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) {
handler: async function (
client: Client,
interaction: MessageContextMenuCommandInteraction
) {
try {
const reportsChannel = await client.channels.fetch("1504936076081369150") as GuildTextBasedChannel;
const userId = interaction.user.id;
const now = Date.now();
reportsChannel.send({
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 <t:${expiresAt}:R>.`
}],
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]
});
}
}
}
}
};