From 1c49e8b730eb3376d3bfd46da40c0af16ffc2efa Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Tue, 2 May 2023 20:41:59 +0200 Subject: [PATCH] Add simple limit enforcing For now it is 25 messages in the last 24 horus. --- src/index.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/index.ts b/src/index.ts index e8dbe0c..27c214d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,41 @@ discord.on("messageCreate", async message => { if (message.author.bot) return; if (!message.mentions.has(message.client.user)) return; + const userLimits = await database.limits.findUnique({ + where: { + user: BigInt(message.author.id) + } + }); + + let limit = 25; + + if (userLimits?.limit) { + limit = userLimits.limit; + } + + if (!userLimits?.vip) { + const usedLimit = await database.usage.count({ + select: { _all: true }, + where: { + user: BigInt(message.author.id), + timestamp: { + gte: new Date(message.createdTimestamp - 1000*60*60*24 /*24 hours */) + } + }, + }); + + if (usedLimit._all >= limit) { + message.react("🛑"); + message.author.dmChannel?.send({ + embeds: [{ + color: 0xff0000, + description: `You've used up your message limit for today, ${limit} requrests in last 24 hours` + }] + }); + return; + } + } + const messagesForChannel = channelsRunning.ensure(message.channelId, () => {return [] as DiscordApi.Message[];} ); const shouldStart = messagesForChannel.length == 0; messagesForChannel.push(message);