Skip to content

Commit

Permalink
feat: fully handled command and message ratelimiting, with premium
Browse files Browse the repository at this point in the history
  • Loading branch information
maamokun committed Dec 12, 2024
1 parent f3bc4f9 commit a0b2923
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/commands/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { CommandInteraction } from "discord.js";
export default {
name: "ping",
description: "Replies with Pong!",
cooldown: 0,
cooldown: 5,
isPremium: false,
botPermissions: [],
userPermissions: [],
Expand Down
9 changes: 9 additions & 0 deletions src/handlers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type CommandInteraction,
} from "discord.js";
import crypto from "node:crypto";
import { setCommandRatelimit, checkCommandRatelimit } from "./ratelimit.ts";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
Expand Down Expand Up @@ -48,13 +49,21 @@ export async function handleCommand(interaction: CommandInteraction) {
`../commands/${interaction.commandName}.ts`
);
const command = commandModule.default;
const limited = await checkCommandRatelimit("cmd", interaction, command.name);
if (limited) return interaction.reply({ content: "You are being ratelimited! Please wait a bit before using this command again.", ephemeral: true });
if (!command.slashCommand.enabled)
return interaction.reply("This command is not enabled!");
if (command.isPremium && !premium)
return interaction.reply(
"This command is only available for premium users!",
);
if (command.slashCommand.enabled) command.interactionRun(interaction);
if (premium) {
setCommandRatelimit("cmd", interaction, command.PremiumCooldown || command.cooldown, command.name);
}
if (!premium) {
setCommandRatelimit("cmd", interaction, command.cooldown, command.name);
}
} catch (error) {
const logId = await sendLog(error.message);
console.error(`Error while executing command: ${logId}`);
Expand Down
25 changes: 25 additions & 0 deletions src/handlers/ratelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const redis = createClient({
url: process.env.REDIS_URL,
});

await redis.connect();

export function setMessageRatelimit(type: string, message: Message) {
if (type == "msg") {
redis.set(
Expand All @@ -27,3 +29,26 @@ export async function checkMessageRatelimit(type: string, message: Message) {
}
}
}

export function setCommandRatelimit(type: string, interaction: CommandInteraction, time: number, name: string) {
if (type == "cmd") {
redis.set(
`command:${interaction.guildId}:${interaction.user.id}:${name}`,
`${interaction.guildId}`,
{ EX: time },
);
}
}

export async function checkCommandRatelimit(type: string, interaction: CommandInteraction, name: string) {
if (type == "cmd") {
const isLimited = await redis.get(
`command:${interaction.guildId}:${interaction.user.id}:${name}`,
);
if (isLimited == `${interaction.guildId}`) {
return true;
} else {
return false;
}
}
}

0 comments on commit a0b2923

Please sign in to comment.