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]
# Logging Level
var L = newConsoleLogger(levelThreshold=lvlError, fmtStr="$levelname, [$time] ")
var L = newConsoleLogger(levelThreshold = lvlError,
fmtStr = "$levelname, [$time] ")
addHandler(L)
# 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
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)
# Create tables if they dont exist
@ -72,9 +74,11 @@ proc ManageRateLimit(): void=
withDb(connPool):
db.insert(BannedUser)
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()]
db.select(TempData, "bantype = ? and ? - bantime >= 1800", "auto", epochTime())
db.select(TempData, "bantype = ? and ? - bantime >= 1800", "auto",
epochTime())
for i in TempData:
var e = i
db.delete(e)
@ -99,7 +103,7 @@ proc generate_hash(): string=
return result
# 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
ManageRateLimit()
withDb(connPool):
@ -125,26 +129,33 @@ proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
var inputmedia = newSeq[InputMediaPhoto]()
for i in TempData:
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:
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid))
discard await b.sendMediaGroup($c.message.chat.id, media=inputmedia)
inputmedia.insert(InputMediaPhoto(kind: i.ftype,
media: i.fileid))
discard await b.sendMediaGroup($c.message.chat.id,
media = inputmedia)
else:
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":
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":
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":
discard await b.sendVideoNote(c.message.chat.id, TempData[0].fileid)
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":
discard await b.sendSticker(c.message.chat.id, TempData[0].fileid)
# 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()
withDb(connPool):
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))
# 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:
let user = c.params
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!")
# 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:
let user = c.params
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!")
# 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 ftype = ""
var res: InlineQueryResultArticle
@ -221,7 +232,7 @@ proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} =
discard await b.answerInlineQuery(u.id, results)
# 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:
let response = u.message
# 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():
let filehash = generate_hash()
GroupMedia[parseInt(response.mediaGroupId)] = filehash
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool):
db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash)
var replybutton = InlineKeyboardButton(text: "Share",
switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id,
"*NSFW " &
@ -283,16 +296,19 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
parseMode = "Markdown")
else:
let filehash = GroupMedia[parseInt(response.mediaGroupId)]
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool):
db.insert(CensoredRow)
else:
let filehash = generate_hash()
var CensoredRow = NewCensoredData(ftype, filehash, fileid, epochTime(), fcaption)
var CensoredRow = NewCensoredData(ftype, filehash, fileid,
epochTime(), fcaption)
withDb(connPool):
db.insert(CensoredRow)
var replybutton = InlineKeyboardButton(text: "Share", switchInlineQuery: filehash)
var replybutton = InlineKeyboardButton(text: "Share",
switchInlineQuery: filehash)
let replymark = newInlineKeyboardMarkup(@[replybutton])
discard await b.sendMessage(response.chat.id,
"*NSFW " &
@ -326,6 +342,7 @@ when isMainModule:
bot.onCommand("source", sourceHandler)
bot.onInlineQuery(inlineHandler)
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:
bot.poll(timeout = 300)