Allow admins to forego ratelimits

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-05-08 21:46:46 +05:30
parent 99868f5986
commit 78964cdd45
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -61,13 +61,13 @@ func NewBannedUsers*(userid = int64(0), bantime = 0.0, bantype = ""):
dbConn.createTables(NewCensoredData()) dbConn.createTables(NewCensoredData())
dbConn.createTables(NewBannedUsers()) dbConn.createTables(NewBannedUsers())
# Ratelimits a user if there is more than 20 timestamps within 60 secs. # Ratelimits a user if there is more than 30 timestamps within 60 secs.
# Resets their ratelimit counter if 60s has passed since first timestamp # Resets their ratelimit counter if 60s has passed since first timestamp
proc ManageRateLimit(): void= proc ManageRateLimit(): void=
for i in RateLimiter.keys().toSeq(): for i in RateLimiter.keys().toSeq():
if RateLimiter[i][^1] - RateLimiter[i][0] >= 60: if RateLimiter[i][^1] - RateLimiter[i][0] >= 60:
RateLimiter.del(i) RateLimiter.del(i)
elif len(RateLimiter[i]) >= 20: elif len(RateLimiter[i]) >= 30:
var BannedUser = NewBannedUsers(i, epochTime(), "auto") var BannedUser = NewBannedUsers(i, epochTime(), "auto")
RateLimiter.del(i) RateLimiter.del(i)
with dbConn: with dbConn:
@ -103,6 +103,7 @@ proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
ManageRateLimit() ManageRateLimit()
if not dbConn.exists(BannedUsers, "userid = ?", c.message.chat.id): if not dbConn.exists(BannedUsers, "userid = ?", c.message.chat.id):
# Update rate-limit counter # Update rate-limit counter
if not AdminID.contains($c.message.chat.id):
if RateLimiter.contains(c.message.chat.id): if RateLimiter.contains(c.message.chat.id):
RateLimiter[c.message.chat.id].insert(epochTime()) RateLimiter[c.message.chat.id].insert(epochTime())
else: else:
@ -145,6 +146,7 @@ proc sourceHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
ManageRateLimit() ManageRateLimit()
if not dbConn.exists(BannedUsers, "userid = ?", c.message.chat.id): if not dbConn.exists(BannedUsers, "userid = ?", c.message.chat.id):
# Update rate-limit counter # Update rate-limit counter
if not AdminID.contains($c.message.chat.id):
if RateLimiter.contains(c.message.chat.id): if RateLimiter.contains(c.message.chat.id):
RateLimiter[c.message.chat.id].insert(epochTime()) RateLimiter[c.message.chat.id].insert(epochTime())
else: else:
@ -216,6 +218,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
# Dont bother about rate-limited/banned users # Dont bother about rate-limited/banned users
if not dbConn.exists(BannedUsers, "userid = ?", response.chat.id): if not dbConn.exists(BannedUsers, "userid = ?", response.chat.id):
# Update rate-limit counter # Update rate-limit counter
if not AdminID.contains($response.chat.id):
if RateLimiter.contains(response.chat.id): if RateLimiter.contains(response.chat.id):
RateLimiter[response.chat.id].insert(epochTime()) RateLimiter[response.chat.id].insert(epochTime())
else: else: