|
1 | 1 | import * as Discord from "discord.js"; |
2 | 2 |
|
3 | | -import { strict as assert } from "assert"; |
4 | | - |
5 | 3 | import { M } from "../../../utils/debugging-and-logging.js"; |
6 | | -import { DAY, HOUR } from "../../../common.js"; |
| 4 | +import { HOUR } from "../../../common.js"; |
7 | 5 | import { BotComponent } from "../../../bot-component.js"; |
8 | | -import { SelfClearingSet } from "../../../utils/containers.js"; |
9 | 6 |
|
10 | 7 | const LLM_REGEX = /\b(?<!!)llms?\b/gi; |
11 | 8 | const MICROSLOP_REGEX = /\b(?<!!)microsoft?\b/gi; |
12 | 9 |
|
13 | 10 | const LLM_AUTOREPLY_ENABLED = false; |
14 | 11 | const MICROSLOP_AUTOREPLY_ENABLED = true; |
| 12 | +const RATELIMIT_DURATION = 4 * HOUR; |
15 | 13 |
|
16 | 14 | export default class Autoreply extends BotComponent { |
17 | 15 | static override get is_freestanding() { |
18 | 16 | return true; |
19 | 17 | } |
20 | 18 |
|
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 | + } |
23 | 24 |
|
24 | 25 | override async on_message_create(message: Discord.Message) { |
25 | 26 | if (message.guildId !== this.wheatley.guild.id || message.author.bot) { |
26 | 27 | return; |
27 | 28 | } |
28 | 29 | // 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(); |
31 | 32 | M.log("firing llm auto-reply"); |
32 | 33 | await message.reply("Did you mean: [***LLVM***](https://llvm.org/)"); |
33 | 34 | } |
34 | 35 | if ( |
35 | 36 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
36 | 37 | MICROSLOP_AUTOREPLY_ENABLED && |
37 | 38 | MICROSLOP_REGEX.test(message.content) && |
38 | | - !this.ratelimit.has(message.channelId) |
| 39 | + !this.is_ratelimited() |
39 | 40 | ) { |
40 | | - this.ratelimit.insert(message.channelId); |
| 41 | + this.last_reply_time = Date.now(); |
41 | 42 | M.log("firing microslop auto-reply"); |
42 | 43 | await message.reply("Did you mean: [***Microslop***](https://microslop.com/)"); |
43 | 44 | } |
|
0 commit comments