Decouple the commands into independent handlers
Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
parent
28ed91a0b6
commit
11cbdcaced
1 changed files with 58 additions and 44 deletions
|
@ -85,6 +85,55 @@ proc generate_hash(): string=
|
|||
result[i] = sample(charset)
|
||||
return result
|
||||
|
||||
# Start command handler
|
||||
proc startHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
|
||||
let param = c.params
|
||||
ManageRateLimit()
|
||||
if not dbConn.exists(BannedUsers, "userid = ?", c.message.chat.id):
|
||||
# Update rate-limit counter
|
||||
if RateLimiter.contains(c.message.chat.id):
|
||||
RateLimiter[c.message.chat.id].insert(epochTime())
|
||||
else:
|
||||
RateLimiter[c.message.chat.id] = @[epochTime()]
|
||||
if param == "":
|
||||
discard await b.sendMessage(c.message.chat.id, "Hey, To create a censored post, you can share any album, video, photo, gif, sticker, etc. The messages could then be forwarded to any chat for them to view")
|
||||
else:
|
||||
if not dbConn.exists(CensoredData, "fhash = ?", param):
|
||||
discard await b.sendMessage(c.message.chat.id, "Media does not exist on database, ask the sender to censor this again!")
|
||||
else:
|
||||
var TempData = @[NewCensoredData()]
|
||||
dbConn.select(TempData, "fhash = ?", param)
|
||||
if len(TempData) > 1:
|
||||
var inputmedia = newSeq[InputMediaPhoto]()
|
||||
for i in TempData:
|
||||
if i.caption != "":
|
||||
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid, caption: some(i.caption)))
|
||||
else:
|
||||
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(c.message.chat.id, TempData[0].fileid, TempData[0].caption)
|
||||
elif TempData[0].ftype == "document":
|
||||
discard await b.sendDocument(c.message.chat.id, TempData[0].fileid, TempData[0].caption)
|
||||
elif TempData[0].ftype == "video":
|
||||
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)
|
||||
elif TempData[0].ftype == "sticker":
|
||||
discard await b.sendSticker(c.message.chat.id, TempData[0].fileid)
|
||||
|
||||
# Ban Handler
|
||||
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")
|
||||
with dbConn:
|
||||
insert BannedUser
|
||||
discard await b.sendMessage(c.message.chat.id, "Banned!")
|
||||
|
||||
# Main update handler
|
||||
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
||||
let response = u.message.get
|
||||
|
@ -97,50 +146,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
|
|||
RateLimiter[response.chat.id].insert(epochTime())
|
||||
else:
|
||||
RateLimiter[response.chat.id] = @[epochTime()]
|
||||
if response.text.isSome:
|
||||
let message = response.text.get
|
||||
# Respond to /start without payload
|
||||
if message == "/start":
|
||||
discard await b.sendMessage(response.chat.id, "Hey, To create a censored post, you can share any album, video, photo, gif, sticker, etc. The messages could then be forwarded to any chat for them to view")
|
||||
# Perma-ban, available to only admins
|
||||
elif message.contains("/ban"):
|
||||
if $response.chat.id in AdminID:
|
||||
let user = message.split(" ")
|
||||
var BannedUser = NewBannedUsers(int64(parseInt(user[1])), epochTime(), "permanent")
|
||||
with dbConn:
|
||||
insert BannedUser
|
||||
discard await b.sendMessage(response.chat.id, "Banned!")
|
||||
# Start, but with payload, handles sending the censored media
|
||||
elif message.contains("/start"):
|
||||
let deeplink = message.split(" ")
|
||||
if not dbConn.exists(CensoredData, "fhash = ?", deeplink[1]):
|
||||
discard await b.sendMessage(response.chat.id, "Media does not exist on database, ask the sender to censor this again!")
|
||||
else:
|
||||
var TempData = @[NewCensoredData()]
|
||||
dbConn.select(TempData, "fhash = ?", deeplink[1])
|
||||
if len(TempData) > 1:
|
||||
var inputmedia = newSeq[InputMediaPhoto]()
|
||||
for i in TempData:
|
||||
if i.caption != "":
|
||||
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid, caption: some(i.caption)))
|
||||
else:
|
||||
inputmedia.insert(InputMediaPhoto(kind: i.ftype, media: i.fileid))
|
||||
discard await b.sendMediaGroup($response.chat.id, media=inputmedia)
|
||||
else:
|
||||
if TempData[0].ftype == "photo":
|
||||
discard await b.sendPhoto(response.chat.id, TempData[0].fileid, TempData[0].caption)
|
||||
elif TempData[0].ftype == "document":
|
||||
discard await b.sendDocument(response.chat.id, TempData[0].fileid, TempData[0].caption)
|
||||
elif TempData[0].ftype == "video":
|
||||
discard await b.sendVideo(response.chat.id, TempData[0].fileid, caption=TempData[0].caption)
|
||||
elif TempData[0].ftype == "videoNote":
|
||||
discard await b.sendVideoNote(response.chat.id, TempData[0].fileid)
|
||||
elif TempData[0].ftype == "animation":
|
||||
discard await b.sendAnimation(response.chat.id, TempData[0].fileid, caption=TempData[0].caption)
|
||||
elif TempData[0].ftype == "sticker":
|
||||
discard await b.sendSticker(response.chat.id, TempData[0].fileid)
|
||||
# Its not a text, its some media, lets censor it!
|
||||
else:
|
||||
if not response.text.isSome:
|
||||
var fileid = ""
|
||||
var ftype = ""
|
||||
var fcaption = ""
|
||||
|
@ -193,5 +199,13 @@ let bot = newTeleBot(getEnv("TELEGRAM_TOKEN"))
|
|||
echo "*********************"
|
||||
echo "CensorBot is running!"
|
||||
echo "*********************"
|
||||
|
||||
var commands = @[
|
||||
BotCommand(command: "start" , description: "Start the bot!")
|
||||
]
|
||||
|
||||
discard waitFor bot.setMyCommands(commands)
|
||||
bot.onUpdate(updateHandler)
|
||||
bot.onCommand("start", startHandler)
|
||||
bot.onCommand("ban", banHandler)
|
||||
bot.poll(timeout=300)
|
||||
|
|
Loading…
Add table
Reference in a new issue