Add simple limit enforcing

For now it is 25 messages in the last 24 horus.
This commit is contained in:
Wroclaw 2023-05-02 20:41:59 +02:00
parent a66115c3b8
commit 1c49e8b730

View file

@ -30,6 +30,41 @@ discord.on("messageCreate", async message => {
if (message.author.bot) return; if (message.author.bot) return;
if (!message.mentions.has(message.client.user)) 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 messagesForChannel = channelsRunning.ensure(message.channelId, () => {return [] as DiscordApi.Message[];} );
const shouldStart = messagesForChannel.length == 0; const shouldStart = messagesForChannel.length == 0;
messagesForChannel.push(message); messagesForChannel.push(message);