update command throttle

This commit is contained in:
1e99 2025-07-23 16:05:53 +02:00
parent 0b557c94d8
commit 963a947a83

View file

@ -4,18 +4,18 @@ export default class Commands {
#chat
#prefix
#commands
#ratelimit
#clearInterval
#throttle
#decrementInterval
constructor(client, chat, prefix) {
this.#client = client
this.#chat = chat
this.#prefix = prefix
this.#commands = []
this.#ratelimit = new Map()
this.#throttle = new Map()
this.#clearInterval = setInterval(() => this.#ratelimit.clear(), 5 * 60 * 1_000)
this.#client.addListener("end", () => clearInterval(this.#clearInterval))
this.#decrementInterval = setInterval(this.#decrementThrottle.bind(this), 2 * 60 * 1_000)
this.#client.addListener("end", () => clearInterval(this.#decrementInterval))
this.#chat.addListener("chat:chat", this.#onChat.bind(this))
}
@ -24,23 +24,29 @@ export default class Commands {
this.#commands.push(command)
}
#decrementThrottle() {
for (const [username, throttle] of this.#throttle) {
this.#throttle.set(username, Math.max(0, throttle - 1))
}
}
#onChat(username, message) {
if (!message.startsWith(this.#prefix)) {
return
}
let ratelimit = this.#ratelimit.get(username)
if (ratelimit === undefined) {
ratelimit = 0
let throttle = this.#throttle.get(username)
if (throttle === undefined) {
throttle = 0
}
ratelimit++
this.#ratelimit.set(username, ratelimit)
if (ratelimit > 5) {
throttle++
if (throttle > 4) {
return
}
this.#throttle.set(username, throttle)
const args = message
.substring(this.#prefix.length)
.split(" ")