Skip to content

Commit

Permalink
feat: ratelimiting with redis instead of db
Browse files Browse the repository at this point in the history
  • Loading branch information
maamokun committed Dec 12, 2024
1 parent 504fa22 commit f3bc4f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
7 changes: 3 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ model User {
}

model guildLvl {
id String @id
level Int
xp Int
cooldown DateTime @default(now())
id String @id
level Int
xp Int
}

model server {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
},
interactionRun: async (interaction: CommandInteraction) => {
const ping = Math.abs(Math.round(interaction.client.ws.ping));
await interaction.reply("Loading...");
await interaction.reply("<a:loading:1272805571585642506>");
const roundtrip = Math.abs(Date.now() - interaction.createdTimestamp);
interaction.editReply(
`API Latency: ${ping}ms\nRoundtrip: ${roundtrip}ms`,
Expand Down
10 changes: 4 additions & 6 deletions src/handlers/lvl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { PrismaClient } from "@prisma/client";
import { setMessageRatelimit, checkMessageRatelimit } from "./ratelimit.ts";
import type { Message } from "discord.js";
import { initGuild } from "./initGuild";

const prisma = new PrismaClient();

const cooldown = 5000;

function getLevelFromXP(xp: number): number {
let level = 1;
let xpRequired = 50; // XP required for level 2
Expand Down Expand Up @@ -56,10 +55,9 @@ export async function handleLevel(message: Message) {
const levelMessage = guildDB?.levelsMessage;

if (lvlDB) {
const limited = await checkMessageRatelimit("msg", message);
if (limited) return;
const newXP = lvlDB.xp + increment;
const currentCooldown = lvlDB.cooldown;
if (currentCooldown > new Date()) return;
const cooldownTime = new Date(Date.now() + cooldown);
const level = getLevelFromXP(newXP);
const lvlMessage = levelMessage
?.replace(/{user}/g, message.author.toString())
Expand All @@ -77,8 +75,8 @@ export async function handleLevel(message: Message) {
data: {
xp: newXP,
level: level,
cooldown: cooldownTime,
},
});
setMessageRatelimit("msg", message);
}
}
21 changes: 19 additions & 2 deletions src/handlers/ratelimit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { createClient } from "redis";
import { type CommandInteraction } from "discord.js";
import { type CommandInteraction, type Message } from "discord.js";

const redis = createClient({
url: process.env.REDIS_URL,
});

export default function setRatelimit(type: string, interaction: CommandInteraction) {
export function setMessageRatelimit(type: string, message: Message) {
if (type == "msg") {
redis.set(
`message:${message.guildId}:${message.author.id}`,
`${message.guildId}`,
{ EX: 5 },
);
}
}

export async function checkMessageRatelimit(type: string, message: Message) {
if (type == "msg") {
const isLimited = await redis.get(
`message:${message.guildId}:${message.author.id}`,
);
if (isLimited == `${message.guildId}`) {
return true;
} else {
return false;
}
}
}

0 comments on commit f3bc4f9

Please sign in to comment.