Pretty print everything
Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
parent
42a85588fe
commit
dc3edf3105
1 changed files with 92 additions and 75 deletions
|
@ -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
|
||||
|
@ -62,7 +64,7 @@ withDb(connPool):
|
|||
|
||||
# Ratelimits a user if there is more than 30 timestamps within 60 secs.
|
||||
# Resets their ratelimit counter if 60s has passed since first timestamp
|
||||
proc ManageRateLimit(): void=
|
||||
proc ManageRateLimit(): void =
|
||||
for i in RateLimiter.keys().toSeq():
|
||||
if RateLimiter[i][^1] - RateLimiter[i][0] >= 60:
|
||||
RateLimiter.del(i)
|
||||
|
@ -72,26 +74,28 @@ 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)
|
||||
|
||||
# Cleanup old data to save space
|
||||
# Deletes old data after 6 months of entry
|
||||
proc OldDataCleanup(): void=
|
||||
proc OldDataCleanup(): void =
|
||||
withDb(connPool):
|
||||
if db.exists(CensoredData, "? - time >= 15780000", epochTime()):
|
||||
var TempData = @[NewCensoredData()]
|
||||
db.select(TempData, "? - time >= 15780000", epochTime())
|
||||
db.select(TempData, "? - time >= 15780000", epochTime())
|
||||
for i in TempData:
|
||||
var e = i
|
||||
db.delete(e)
|
||||
|
||||
# Generates a 6-character magic that will be used to identify the file
|
||||
proc generate_hash(): string=
|
||||
proc generate_hash(): string =
|
||||
result = newString(7)
|
||||
const charset = {'a' .. 'z', 'A' .. 'Z', '0' .. '9'}
|
||||
for i in 0..6:
|
||||
|
@ -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):
|
||||
|
@ -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! " &
|
||||
"\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)",
|
||||
parseMode="Markdown",
|
||||
parseMode = "Markdown",
|
||||
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):
|
||||
if db.exists(BannedUsers, "userid = ?", int64(parseInt(user))):
|
||||
var TempData = @[NewBannedUsers()]
|
||||
db.select(TempData, "userid = ?", int64(parseInt(user)))
|
||||
db.select(TempData, "userid = ?", int64(parseInt(user)))
|
||||
for i in TempData:
|
||||
var e = i
|
||||
db.delete(e)
|
||||
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")
|
||||
withDb(connPool):
|
||||
db.insert(BannedUser)
|
||||
db.insert(BannedUser)
|
||||
discard await b.sendMessage(c.message.chat.id, "Banned!")
|
||||
|
||||
# Inline share handler
|
||||
proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.gcsafe, async.} =
|
||||
var TempData = @[NewCensoredData()]
|
||||
var ftype = ""
|
||||
var res: InlineQueryResultArticle
|
||||
var results: seq[InlineQueryResultArticle]
|
||||
res.kind = "article"
|
||||
res.id = "1"
|
||||
if u.query != "":
|
||||
withDb(connPool):
|
||||
if not db.exists(CensoredData, "fhash = ?",u.query):
|
||||
res.title = "Media Not Found"
|
||||
res.inputMessageContent = InputTextMessageContent(
|
||||
"Media does not exist on database, ask the sender to censor this again!")
|
||||
else:
|
||||
db.select(TempData, "fhash = ?", u.query)
|
||||
if len(TempData) > 1:
|
||||
ftype = "Album"
|
||||
elif len(TempData) == 1:
|
||||
ftype = TempData[0].ftype
|
||||
res.title = "NSFW " & capitalizeAscii(ftype)
|
||||
res.inputMessageContent = InputMessageContent(kind: TextMessage,
|
||||
messageText: "*NSFW " &
|
||||
capitalizeAscii(ftype) &
|
||||
"*\n\n[Tap to View](https://t.me/" &
|
||||
b.username & "?start=" &
|
||||
u.query &
|
||||
")",
|
||||
parseMode: "Markdown",
|
||||
linkPreviewOptions: LinkPreviewOptions(isDisabled: true))
|
||||
else:
|
||||
res.title = "Waiting for File Hash"
|
||||
res.inputMessageContent = InputTextMessageContent(
|
||||
"Provide the filehash on inline query")
|
||||
results.add(res)
|
||||
discard await b.answerInlineQuery(u.id, results)
|
||||
proc inlineHandler(b: Telebot; u: InlineQuery): Future[bool]{.gcsafe, async.} =
|
||||
var TempData = @[NewCensoredData()]
|
||||
var ftype = ""
|
||||
var res: InlineQueryResultArticle
|
||||
var results: seq[InlineQueryResultArticle]
|
||||
res.kind = "article"
|
||||
res.id = "1"
|
||||
if u.query != "":
|
||||
withDb(connPool):
|
||||
if not db.exists(CensoredData, "fhash = ?", u.query):
|
||||
res.title = "Media Not Found"
|
||||
res.inputMessageContent = InputTextMessageContent(
|
||||
"Media does not exist on database, ask the sender to censor this again!")
|
||||
else:
|
||||
db.select(TempData, "fhash = ?", u.query)
|
||||
if len(TempData) > 1:
|
||||
ftype = "Album"
|
||||
elif len(TempData) == 1:
|
||||
ftype = TempData[0].ftype
|
||||
res.title = "NSFW " & capitalizeAscii(ftype)
|
||||
res.inputMessageContent = InputMessageContent(kind: TextMessage,
|
||||
messageText: "*NSFW " &
|
||||
capitalizeAscii(ftype) &
|
||||
"*\n\n[Tap to View](https://t.me/" &
|
||||
b.username & "?start=" &
|
||||
u.query &
|
||||
")",
|
||||
parseMode: "Markdown",
|
||||
linkPreviewOptions: LinkPreviewOptions(isDisabled: true))
|
||||
else:
|
||||
res.title = "Waiting for File Hash"
|
||||
res.inputMessageContent = InputTextMessageContent(
|
||||
"Provide the filehash on inline query")
|
||||
results.add(res)
|
||||
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 " &
|
||||
|
@ -280,19 +293,22 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
|||
")",
|
||||
replyMarkup = replymark,
|
||||
linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
|
||||
parseMode="Markdown")
|
||||
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)
|
||||
db.insert(CensoredRow)
|
||||
var replybutton = InlineKeyboardButton(text: "Share",
|
||||
switchInlineQuery: filehash)
|
||||
let replymark = newInlineKeyboardMarkup(@[replybutton])
|
||||
discard await b.sendMessage(response.chat.id,
|
||||
"*NSFW " &
|
||||
|
@ -304,7 +320,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
|||
")",
|
||||
replyMarkup = replymark,
|
||||
linkPreviewOptions = LinkPreviewOptions(isDisabled: true),
|
||||
parseMode="Markdown")
|
||||
parseMode = "Markdown")
|
||||
OldDataCleanup()
|
||||
|
||||
when isMainModule:
|
||||
|
@ -314,8 +330,8 @@ when isMainModule:
|
|||
echo "*********************"
|
||||
|
||||
var commands = @[
|
||||
BotCommand(command: "start" , description: "Start the bot!"),
|
||||
BotCommand(command: "source" , description: "Info about bot source")
|
||||
BotCommand(command: "start", description: "Start the bot!"),
|
||||
BotCommand(command: "source", description: "Info about bot source")
|
||||
]
|
||||
|
||||
discard waitFor bot.setMyCommands(commands)
|
||||
|
@ -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)
|
||||
bot.poll(timeout = 300)
|
||||
|
|
Loading…
Reference in a new issue