Skip to content

Commit 95985e6

Browse files
committed
Update auto-reply rate limiting
1 parent 841697c commit 95985e6

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

src/modules/tccpp/components/auto-reply.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
import * as Discord from "discord.js";
22

3-
import { strict as assert } from "assert";
4-
53
import { M } from "../../../utils/debugging-and-logging.js";
6-
import { DAY, HOUR } from "../../../common.js";
4+
import { HOUR } from "../../../common.js";
75
import { BotComponent } from "../../../bot-component.js";
8-
import { SelfClearingSet } from "../../../utils/containers.js";
96

107
const LLM_REGEX = /\b(?<!!)llms?\b/gi;
118
const MICROSLOP_REGEX = /\b(?<!!)microsoft?\b/gi;
129

1310
const LLM_AUTOREPLY_ENABLED = false;
1411
const MICROSLOP_AUTOREPLY_ENABLED = true;
12+
const RATELIMIT_DURATION = 4 * HOUR;
1513

1614
export default class Autoreply extends BotComponent {
1715
static override get is_freestanding() {
1816
return true;
1917
}
2018

21-
// set of channel ids
22-
ratelimit = new SelfClearingSet<string>(DAY, HOUR);
19+
last_reply_time = 0;
20+
21+
is_ratelimited() {
22+
return Date.now() - this.last_reply_time < RATELIMIT_DURATION;
23+
}
2324

2425
override async on_message_create(message: Discord.Message) {
2526
if (message.guildId !== this.wheatley.guild.id || message.author.bot) {
2627
return;
2728
}
2829
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
29-
if (LLM_AUTOREPLY_ENABLED && LLM_REGEX.test(message.content) && !this.ratelimit.has(message.channelId)) {
30-
this.ratelimit.insert(message.channelId);
30+
if (LLM_AUTOREPLY_ENABLED && LLM_REGEX.test(message.content) && !this.is_ratelimited()) {
31+
this.last_reply_time = Date.now();
3132
M.log("firing llm auto-reply");
3233
await message.reply("Did you mean: [***LLVM***](https://llvm.org/)");
3334
}
3435
if (
3536
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
3637
MICROSLOP_AUTOREPLY_ENABLED &&
3738
MICROSLOP_REGEX.test(message.content) &&
38-
!this.ratelimit.has(message.channelId)
39+
!this.is_ratelimited()
3940
) {
40-
this.ratelimit.insert(message.channelId);
41+
this.last_reply_time = Date.now();
4142
M.log("firing microslop auto-reply");
4243
await message.reply("Did you mean: [***Microslop***](https://microslop.com/)");
4344
}

0 commit comments

Comments
 (0)