diff --git a/src/nim_censor_bot.nim b/src/nim_censor_bot.nim index 4b40265..d1de045 100644 --- a/src/nim_censor_bot.nim +++ b/src/nim_censor_bot.nim @@ -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)