Pretty print everything

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2024-01-10 21:29:32 +05:30
parent 42a85588fe
commit dc3edf3105
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -19,7 +19,8 @@ import std/[asyncdispatch,
import norm/[model, sqlite, pool] import norm/[model, sqlite, pool]
# Logging Level # Logging Level
var L = newConsoleLogger(levelThreshold=lvlError, fmtStr="$levelname, [$time] ") var L = newConsoleLogger(levelThreshold = lvlError,
fmtStr = "$levelname, [$time] ")
addHandler(L) addHandler(L)
# Custom Types Defined to handle tables on norm # Custom Types Defined to handle tables on norm
@ -50,9 +51,10 @@ var connPool = newPool[DbConn](100)
# Functions to assist adding/deleting entries from tables # Functions to assist adding/deleting entries from tables
func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; time = 0.0; caption = ""): func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; time = 0.0; caption = ""):
CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, time: time, caption: caption) CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid,
time: time, 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)
# Create tables if they dont exist # Create tables if they dont exist
@ -60,9 +62,9 @@ withDb(connPool):
db.createTables(NewCensoredData()) db.createTables(NewCensoredData())
db.createTables(NewBannedUsers()) db.createTables(NewBannedUsers())
# Ratelimits a user if there is more than 30 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)
@ -72,34 +74,36 @@ proc ManageRateLimit(): void=
withDb(connPool): withDb(connPool):
db.insert(BannedUser) db.insert(BannedUser)
withDb(connPool): withDb(connPool):
if db.exists(BannedUsers, "bantype = ? and ? - bantime >= 1800", "auto", epochTime()): if db.exists(BannedUsers, "bantype = ? and ? - bantime >= 1800", "auto",
epochTime()):
var TempData = @[NewBannedUsers()] var TempData = @[NewBannedUsers()]
db.select(TempData, "bantype = ? and ? - bantime >= 1800", "auto", epochTime()) db.select(TempData, "bantype = ? and ? - bantime >= 1800", "auto",
epochTime())
for i in TempData: for i in TempData:
var e = i var e = i
db.delete(e) db.delete(e)
# Cleanup old data to save space # Cleanup old data to save space
# Deletes old data after 6 months of entry # Deletes old data after 6 months of entry
proc OldDataCleanup(): void= proc OldDataCleanup(): void =
withDb(connPool): withDb(connPool):
if db.exists(CensoredData, "? - time >= 15780000", epochTime()): if db.exists(CensoredData, "? - time >= 15780000", epochTime()):
var TempData = @[NewCensoredData()] var TempData = @[NewCensoredData()]
db.select(TempData, "? - time >= 15780000", epochTime()) db.select(TempData, "? - time >= 15780000", epochTime())
for i in TempData: for i in TempData:
var e = i var e = i
db.delete(e) db.delete(e)
# Generates a 6-character magic that will be used to identify the file # Generates a 6-character magic that will be used to identify the file
proc generate_hash(): string= proc generate_hash(): string =
result = newString(7) result = newString(7)
const charset = {'a' .. 'z', 'A' .. 'Z', '0' .. '9'} const charset = {'a' .. 'z', 'A' .. 'Z', '0' .. '9'}
for i in 0..6: for i in 0..6:
result[i] = sample(charset) result[i] = sample(charset)
return result return result
# Start command handler # Start command handler
proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} = proc startHandler(b: Telebot; c: Command): Future[bool] {.gcsafe, async.} =
let param = c.params let param = c.params
ManageRateLimit() ManageRateLimit()
withDb(connPool): withDb(connPool):
@ -125,26 +129,33 @@ proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
var inputmedia = newSeq[InputMediaPhoto]() var inputmedia = newSeq[InputMediaPhoto]()
for i in TempData: for i in TempData:
if i.caption != "": if i.caption != "":
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid, caption: i.caption)) inputmedia.insert(InputMediaPhoto(kind: i.ftype,
media: i.fileid, caption: i.caption))
else: else:
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid)) inputmedia.insert(InputMediaPhoto(kind: i.ftype,
discard await b.sendMediaGroup($c.message.chat.id, media=inputmedia) media: i.fileid))
discard await b.sendMediaGroup($c.message.chat.id,
media = inputmedia)
else: else:
if TempData[0].ftype == "photo": if TempData[0].ftype == "photo":
discard await b.sendPhoto(chatId=c.message.chat.id, photo=TempData[0].fileid, caption=TempData[0].caption) discard await b.sendPhoto(chatId = c.message.chat.id,
photo = TempData[0].fileid, caption = TempData[0].caption)
elif TempData[0].ftype == "document": elif TempData[0].ftype == "document":
discard await b.sendDocument(c.message.chat.id, TempData[0].fileid, caption=TempData[0].caption) discard await b.sendDocument(c.message.chat.id, TempData[
0].fileid, caption = TempData[0].caption)
elif TempData[0].ftype == "video": elif TempData[0].ftype == "video":
discard await b.sendVideo(c.message.chat.id, TempData[0].fileid, caption=TempData[0].caption) discard await b.sendVideo(c.message.chat.id, TempData[0].fileid,
caption = TempData[0].caption)
elif TempData[0].ftype == "videoNote": elif TempData[0].ftype == "videoNote":
discard await b.sendVideoNote(c.message.chat.id, TempData[0].fileid) discard await b.sendVideoNote(c.message.chat.id, TempData[0].fileid)
elif TempData[0].ftype == "animation": elif TempData[0].ftype == "animation":
discard await b.sendAnimation(c.message.chat.id, TempData[0].fileid, caption=TempData[0].caption) discard await b.sendAnimation(c.message.chat.id, TempData[
0].fileid, caption = TempData[0].caption)
elif TempData[0].ftype == "sticker": elif TempData[0].ftype == "sticker":
discard await b.sendSticker(c.message.chat.id, TempData[0].fileid) discard await b.sendSticker(c.message.chat.id, TempData[0].fileid)
# Give them source url # Give them source url
proc sourceHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} = proc sourceHandler(b: Telebot; c: Command): Future[bool] {.gcsafe, async.} =
ManageRateLimit() ManageRateLimit()
withDb(connPool): withDb(connPool):
if not db.exists(BannedUsers, "userid = ?", c.message.chat.id): if not db.exists(BannedUsers, "userid = ?", c.message.chat.id):
@ -158,70 +169,70 @@ proc sourceHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
"Hey, this bot is open-source and licensed under AGPL-v3! " & "Hey, this bot is open-source and licensed under AGPL-v3! " &
"\n\nYou are welcome to selfhost your own instance of this bot" & "\n\nYou are welcome to selfhost your own instance of this bot" &
"\n\nThe source code is [here](https://git.baalajimaestro.me/baalajimaestro/nim-censor-bot)", "\n\nThe source code is [here](https://git.baalajimaestro.me/baalajimaestro/nim-censor-bot)",
parseMode="Markdown", parseMode = "Markdown",
linkPreviewOptions = LinkPreviewOptions(isDisabled: true)) linkPreviewOptions = LinkPreviewOptions(isDisabled: true))
# UnBan Handler # UnBan Handler
proc unbanHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} = proc unbanHandler(b: Telebot; c: Command): Future[bool] {.gcsafe, async.} =
if $c.message.chat.id in AdminID: if $c.message.chat.id in AdminID:
let user = c.params let user = c.params
withDb(connPool): withDb(connPool):
if db.exists(BannedUsers, "userid = ?", int64(parseInt(user))): if db.exists(BannedUsers, "userid = ?", int64(parseInt(user))):
var TempData = @[NewBannedUsers()] var TempData = @[NewBannedUsers()]
db.select(TempData, "userid = ?", int64(parseInt(user))) db.select(TempData, "userid = ?", int64(parseInt(user)))
for i in TempData: for i in TempData:
var e = i var e = i
db.delete(e) db.delete(e)
discard await b.sendMessage(c.message.chat.id, "Unbanned!") discard await b.sendMessage(c.message.chat.id, "Unbanned!")
# ban Handler # ban Handler
proc banHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} = proc banHandler(b: Telebot; c: Command): Future[bool] {.gcsafe, async.} =
if $c.message.chat.id in AdminID: if $c.message.chat.id in AdminID:
let user = c.params let user = c.params
var BannedUser = NewBannedUsers(int64(parseInt(user)), epochTime(), "permanent") var BannedUser = NewBannedUsers(int64(parseInt(user)), epochTime(), "permanent")
withDb(connPool): withDb(connPool):
db.insert(BannedUser) db.insert(BannedUser)
discard await b.sendMessage(c.message.chat.id, "Banned!") discard await b.sendMessage(c.message.chat.id, "Banned!")
# Inline share handler # Inline share handler
proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} = proc inlineHandler(b: Telebot; u: InlineQuery): Future[bool]{.gcsafe, async.} =
var TempData = @[NewCensoredData()] var TempData = @[NewCensoredData()]
var ftype = "" var ftype = ""
var res: InlineQueryResultArticle var res: InlineQueryResultArticle
var results: seq[InlineQueryResultArticle] var results: seq[InlineQueryResultArticle]
res.kind = "article" res.kind = "article"
res.id = "1" res.id = "1"
if u.query != "": if u.query != "":
withDb(connPool): withDb(connPool):
if not db.exists(CensoredData, "fhash = ?",u.query): if not db.exists(CensoredData, "fhash = ?", u.query):
res.title = "Media Not Found" res.title = "Media Not Found"
res.inputMessageContent = InputTextMessageContent( res.inputMessageContent = InputTextMessageContent(
"Media does not exist on database, ask the sender to censor this again!") "Media does not exist on database, ask the sender to censor this again!")
else: else:
db.select(TempData, "fhash = ?", u.query) db.select(TempData, "fhash = ?", u.query)
if len(TempData) > 1: if len(TempData) > 1:
ftype = "Album" ftype = "Album"
elif len(TempData) == 1: elif len(TempData) == 1:
ftype = TempData[0].ftype ftype = TempData[0].ftype
res.title = "NSFW " & capitalizeAscii(ftype) res.title = "NSFW " & capitalizeAscii(ftype)
res.inputMessageContent = InputMessageContent(kind: TextMessage, res.inputMessageContent = InputMessageContent(kind: TextMessage,
messageText: "*NSFW " & messageText: "*NSFW " &
capitalizeAscii(ftype) & capitalizeAscii(ftype) &
"*\n\n[Tap to View](https://t.me/" & "*\n\n[Tap to View](https://t.me/" &
b.username & "?start=" & b.username & "?start=" &
u.query & u.query &
")", ")",
parseMode: "Markdown", parseMode: "Markdown",
linkPreviewOptions: LinkPreviewOptions(isDisabled: true)) linkPreviewOptions: LinkPreviewOptions(isDisabled: true))
else: else:
res.title = "Waiting for File Hash" res.title = "Waiting for File Hash"
res.inputMessageContent = InputTextMessageContent( res.inputMessageContent = InputTextMessageContent(
"Provide the filehash on inline query") "Provide the filehash on inline query")
results.add(res) results.add(res)
discard await b.answerInlineQuery(u.id, results) discard await b.answerInlineQuery(u.id, results)
# Main update handler # Main update handler
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} = proc updateHandler(b: Telebot; u: Update): Future[bool] {.async, gcsafe.} =
if not u.message.isNil: if not u.message.isNil:
let response = u.message let response = u.message
# Refresh rate-limits # Refresh rate-limits
@ -265,13 +276,15 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
if parseInt(response.mediaGroupId) notin GroupMedia.keys().toSeq(): if parseInt(response.mediaGroupId) notin GroupMedia.keys().toSeq():
let filehash = generate_hash() let filehash = generate_hash()
GroupMedia[parseInt(response.mediaGroupId)] = filehash GroupMedia[parseInt(response.mediaGroupId)] = filehash
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash) var replybutton = InlineKeyboardButton(text: "Share",
switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton]) let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id, discard await b.sendMessage(response.chat.id,
"*NSFW " & "*NSFW " &
"Album" & "Album" &
"*\n\n[Tap to View](https://t.me/" & "*\n\n[Tap to View](https://t.me/" &
b.username & b.username &
@ -280,22 +293,25 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
")", ")",
replyMarkup = replymark, replyMarkup = replymark,
linkPreviewOptions = LinkPreviewOptions(isDisabled: true), linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
parseMode="Markdown") parseMode = "Markdown")
else: else:
let filehash = GroupMedia[parseInt(response.mediaGroupId)] let filehash = GroupMedia[parseInt(response.mediaGroupId)]
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
else: else:
let filehash = generate_hash() let filehash = generate_hash()
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool): withDb(connPool):
db.insert(CensoredRow) db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash) var replybutton = InlineKeyboardButton(text: "Share",
switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton]) let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id, discard await b.sendMessage(response.chat.id,
"*NSFW " & "*NSFW " &
capitalizeAscii(ftype) & capitalizeAscii(ftype) &
"*\n\n[Tap to View](https://t.me/" & "*\n\n[Tap to View](https://t.me/" &
b.username & b.username &
@ -304,7 +320,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
")", ")",
replyMarkup = replymark, replyMarkup = replymark,
linkPreviewOptions = LinkPreviewOptions(isDisabled: true), linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
parseMode="Markdown") parseMode = "Markdown")
OldDataCleanup() OldDataCleanup()
when isMainModule: when isMainModule:
@ -314,8 +330,8 @@ when isMainModule:
echo "*********************" echo "*********************"
var commands = @[ var commands = @[
BotCommand(command: "start" , description: "Start the bot!"), BotCommand(command: "start", description: "Start the bot!"),
BotCommand(command: "source" , description: "Info about bot source") BotCommand(command: "source", description: "Info about bot source")
] ]
discard waitFor bot.setMyCommands(commands) discard waitFor bot.setMyCommands(commands)
@ -326,6 +342,7 @@ when isMainModule:
bot.onCommand("source", sourceHandler) bot.onCommand("source", sourceHandler)
bot.onInlineQuery(inlineHandler) bot.onInlineQuery(inlineHandler)
if getEnv("HOOK_DOMAIN") != "": if getEnv("HOOK_DOMAIN") != "":
bot.startWebhook(getEnv("HOOK_SECRET"), getEnv("HOOK_DOMAIN") & "/" & getEnv("HOOK_SECRET")) bot.startWebhook(getEnv("HOOK_SECRET"), getEnv("HOOK_DOMAIN") & "/" &
getEnv("HOOK_SECRET"))
else: else:
bot.poll(timeout=300) bot.poll(timeout = 300)