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