add temporary ratelimit
This commit is contained in:
parent
cfa3f14f36
commit
efc138e117
3 changed files with 24 additions and 4 deletions
|
@ -4,6 +4,6 @@ A Minecraft chatbot for 3b3t NoHax.
|
|||
## Future Ideas
|
||||
- Reputation system
|
||||
- Message of the day which is sent to every player upon joining
|
||||
- Respond to whispers with whispers to prevent chat clutter
|
||||
- Respond to whispers with whispers to prevent chat clutter. Be careful with discord's in-game chat's /msg command
|
||||
- Ratelimit to prevent spam
|
||||
- `Error: read ECONNRESET` crashes the bot
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
export default class Commands {
|
||||
|
||||
#client
|
||||
#chat
|
||||
#prefix
|
||||
#commands
|
||||
#ratelimit
|
||||
#clearInterval
|
||||
|
||||
constructor(chat, prefix) {
|
||||
constructor(client, chat, prefix) {
|
||||
this.#client = client
|
||||
this.#chat = chat
|
||||
this.#prefix = prefix
|
||||
this.#commands = []
|
||||
this.#ratelimit = new Map()
|
||||
|
||||
this.#clearInterval = setInterval(() => this.#ratelimit.clear(), 5 * 60 * 1_000)
|
||||
this.#client.addListener("end", () => clearInterval(this.#clearInterval))
|
||||
|
||||
this.#chat.addListener("chat:chat", this.#onChat.bind(this))
|
||||
}
|
||||
|
@ -24,12 +32,24 @@ export default class Commands {
|
|||
const args = message
|
||||
.substring(this.#prefix.length)
|
||||
.split(" ")
|
||||
|
||||
|
||||
// Just to be safe
|
||||
if (args.length < 1) {
|
||||
return
|
||||
}
|
||||
|
||||
let ratelimit = this.#ratelimit.get(username)
|
||||
if (ratelimit === undefined) {
|
||||
ratelimit = 0
|
||||
}
|
||||
|
||||
ratelimit++
|
||||
this.#ratelimit.set(username, ratelimit)
|
||||
|
||||
if (ratelimit > 10) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const command of this.#commands) {
|
||||
if (command.name !== args[0]) {
|
||||
continue
|
||||
|
|
|
@ -95,7 +95,7 @@ class Bot {
|
|||
this.#client.addListener("connect", this.#onClientConnect.bind(this))
|
||||
this.#client.addListener("end", this.#onClientEnd.bind(this))
|
||||
|
||||
this.#commands = new Commands(this.#chat, this.#prefix)
|
||||
this.#commands = new Commands(this.#client, this.#chat, this.#prefix)
|
||||
this.#commands.register(new BestPingCommand(this.#chat, this.#players))
|
||||
this.#commands.register(new HelpCommand(this.#chat, this.#commands))
|
||||
this.#commands.register(new InfoCommand(this.#chat))
|
||||
|
|
Loading…
Add table
Reference in a new issue