Skip to content

Commit

Permalink
fix: add cooldowns to delete & edit message commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Feb 22, 2024
1 parent afd9ecd commit 8476388
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/commands/context-menu/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default class Blacklist extends BaseCommand {
successEmbed.setDescription(
t(
{ phrase: 'blacklist.user.success', locale: interaction.user.locale },
{ username: user?.username ?? 'Unknown User', emoji: emojis.tick },
{ user: user?.username ?? 'Unknown User', emoji: emojis.tick },
),
);
await blacklistManager.addUserBlacklist(
Expand Down
10 changes: 7 additions & 3 deletions src/commands/context-menu/deleteMsg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
ApplicationCommandType,
CacheType,
ContextMenuCommandInteraction,
MessageContextMenuCommandInteraction,
RESTPostAPIApplicationCommandsJSONBody,
} from 'discord.js';
import BaseCommand from '../BaseCommand.js';
Expand All @@ -17,7 +16,12 @@ export default class DeleteMessage extends BaseCommand {
dm_permission: false,
};

async execute(interaction: ContextMenuCommandInteraction<CacheType>) {
readonly cooldown = 10_000;

async execute(interaction: MessageContextMenuCommandInteraction) {
const isOnCooldown = await this.handleCooldown(interaction);
if (isOnCooldown) return;

await interaction.deferReply({ ephemeral: true });

const messageInDb = await db?.broadcastedMessages.findFirst({
Expand Down
8 changes: 6 additions & 2 deletions src/commands/context-menu/editMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import db from '../../utils/Db.js';
import BaseCommand from '../BaseCommand.js';
import { HubSettingsBitField } from '../../utils/BitFields.js';
import { checkIfStaff, hasVoted, replaceLinks } from '../../utils/Utils.js';
import { checkIfStaff, replaceLinks, userVotedToday } from '../../utils/Utils.js';
import { censor } from '../../utils/Profanity.js';
import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { CustomID } from '../../utils/CustomID.js';
Expand All @@ -27,11 +27,15 @@ export default class EditMessage extends BaseCommand {
name: 'Edit Message',
dm_permission: false,
};
readonly cooldown = 10_000;

async execute(interaction: MessageContextMenuCommandInteraction) {
const isOnCooldown = await this.handleCooldown(interaction);
if (isOnCooldown) return;

const target = interaction.targetMessage;

if (!checkIfStaff(interaction.user.id) && !(await hasVoted(interaction.user.id))) {
if (!checkIfStaff(interaction.user.id) && !(await userVotedToday(interaction.user.id))) {
await interaction.reply({
content: t(
{ phrase: 'errors.mustVote', locale: interaction.user.locale },
Expand Down
9 changes: 9 additions & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export const hasVoted = async (userId: Snowflake): Promise<boolean> => {
return !!res.voted;
};

export const userVotedToday = async (userId: Snowflake): Promise<boolean> => {
const res = await db.userData.findFirst({
where: { userId },
});

if (res?.lastVoted && res.lastVoted > Date.now() - 60 * 60 * 24 * 1000) return true;
return false;
};

export const yesOrNoEmoji = (option: unknown, yesEmoji: string, noEmoji: string) => {
return option ? yesEmoji : noEmoji;
};
Expand Down

0 comments on commit 8476388

Please sign in to comment.