Add a handler to cleanup old data

Also realign functions

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-04-11 09:44:57 +05:30
parent a87f37f689
commit 2dea576b58
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -2,6 +2,9 @@ import telebot
import std/[asyncdispatch, logging, options, strutils, random, with, os, tables, times, sequtils] import std/[asyncdispatch, logging, options, strutils, random, with, os, tables, times, sequtils]
import norm/[model, sqlite] import norm/[model, sqlite]
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
addHandler(L)
type type
CensoredData* = ref object of Model CensoredData* = ref object of Model
ftype*: string ftype*: string
@ -22,13 +25,16 @@ var GroupMedia = initTable[int, string]()
let AdminID = getEnv("ADMIN_ID").split(",") let AdminID = getEnv("ADMIN_ID").split(",")
let dbConn* = getDb()
func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""): func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""):
CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption) CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption)
func NewBannedUsers*(userid = int64(0), bantime = 0.0, bantype = ""): func NewBannedUsers*(userid = int64(0), bantime = 0.0, bantype = ""):
BannedUsers = BannedUsers(userid: userid, bantime: bantime, bantype: bantype) BannedUsers = BannedUsers(userid: userid, bantime: bantime, bantype: bantype)
let dbConn* = getDb() dbConn.createTables(NewCensoredData())
dbConn.createTables(NewBannedUsers())
proc ManageRateLimit(): void= proc ManageRateLimit(): void=
for i in RateLimiter.keys().toSeq(): for i in RateLimiter.keys().toSeq():
@ -46,11 +52,13 @@ proc ManageRateLimit(): void=
var e = i var e = i
dbConn.delete(e) dbConn.delete(e)
dbConn.createTables(NewCensoredData()) proc OldDataCleanup(): void=
dbConn.createTables(NewBannedUsers()) if dbConn.exists(CensoredData, "? - time >= 15780000", epochTime()):
var TempData = @[NewCensoredData()]
var L = newConsoleLogger(fmtStr="$levelname, [$time] ") dbConn.select(TempData, "? - time >= 15780000", epochTime())
addHandler(L) for i in TempData:
var e = i
dbConn.delete(e)
proc generate_hash(): string= proc generate_hash(): string=
result = newString(7) result = newString(7)
@ -151,6 +159,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
with dbConn: with dbConn:
insert CensoredRow insert CensoredRow
discard await b.sendMessage(response.chat.id, "Censored --> " & "tg://resolve?domain=botbotbotnotabot&start=" & filehash) discard await b.sendMessage(response.chat.id, "Censored --> " & "tg://resolve?domain=botbotbotnotabot&start=" & filehash)
OldDataCleanup()
let bot = newTeleBot(getEnv("TELEGRAM_TOKEN")) let bot = newTeleBot(getEnv("TELEGRAM_TOKEN"))
bot.onUpdate(updateHandler) bot.onUpdate(updateHandler)