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
@ -72,9 +74,11 @@ 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)
@ -99,7 +103,7 @@ proc generate_hash(): string=
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):
@ -162,7 +173,7 @@ proc sourceHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
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):
@ -175,7 +186,7 @@ proc unbanHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
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")
@ -184,7 +195,7 @@ proc banHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
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
@ -221,7 +232,7 @@ proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} =
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,10 +276,12 @@ 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 " &
@ -283,16 +296,19 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
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 " &
@ -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)