Implement banning unwanted users

People might use this bot on very gore/spam this repeatedly, we can block off them

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-04-09 23:15:02 +05:30
parent d1bbc59e27
commit 9c41348338
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -1,4 +1,6 @@
import telebot, std/[asyncdispatch, logging, options, strutils, random, with, os], norm/[model, sqlite] import telebot
import std/[asyncdispatch, logging, options, strutils, random, with, os]
import norm/[model, sqlite]
type type
CensoredData* = ref object of Model CensoredData* = ref object of Model
@ -7,12 +9,21 @@ type
fileid*: string fileid*: string
caption*: string caption*: string
type
BannedUsers* = ref object of Model
userid*: string
func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""): func NewCensoredData*(ftype = ""; fhash = ""; fileid = ""; caption = ""):
CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption) CensoredData = CensoredData(ftype: ftype, fhash: fhash, fileid: fileid, caption: caption)
func NewBannedUsers*(userid = ""):
BannedUsers = BannedUsers(userid: userid)
let dbConn* = sqlite.open("censordata.db", "", "", "") let dbConn* = sqlite.open("censordata.db", "", "", "")
dbConn.createTables(NewCensoredData()) dbConn.createTables(NewCensoredData())
dbConn.createTables(NewBannedUsers())
var L = newConsoleLogger(fmtStr="$levelname, [$time] ") var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
addHandler(L) addHandler(L)
@ -28,62 +39,70 @@ const API_KEY = getEnv("TELEGRAM_TOKEN")
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} = proc updateHandler(b: Telebot, u: Update): Future[bool] {.async, gcsafe.} =
let response = u.message.get let response = u.message.get
if response.text.isSome: if not dbConn.exists(BannedUsers, "userid = ?", response.chat.id):
let message = response.text.get if response.text.isSome:
if message == "/start": let message = response.text.get
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") if message == "/start":
elif message.contains("/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")
let deeplink = message.split(" ") elif message.contains("/ban"):
var TempData = @[NewCensoredData()] let user = message.split(" ")
dbConn.select(TempData, "fhash = ?", deeplink[1]) echo "BANNING"
for i in TempData: var BannedUser = NewBannedUsers(user[1])
if i.ftype == "photo": with dbConn:
discard await b.sendPhoto(response.chat.id, i.fileid, i.caption) insert BannedUser
elif i.ftype == "document": discard await b.sendMessage(response.chat.id, "Banned!")
discard await b.sendDocument(response.chat.id, i.fileid, i.caption) elif message.contains("/start"):
elif i.ftype == "video": let deeplink = message.split(" ")
discard await b.sendVideo(response.chat.id, i.fileid, caption=i.caption) var TempData = @[NewCensoredData()]
elif i.ftype == "videonote": dbConn.select(TempData, "fhash = ?", deeplink[1])
discard await b.sendVideoNote(response.chat.id, i.fileid) for i in TempData:
elif i.ftype == "animation": if i.ftype == "photo":
discard await b.sendAnimation(response.chat.id, i.fileid, caption=i.caption) discard await b.sendPhoto(response.chat.id, i.fileid, i.caption)
elif i.ftype == "sticker": elif i.ftype == "document":
discard await b.sendSticker(response.chat.id, i.fileid) discard await b.sendDocument(response.chat.id, i.fileid, i.caption)
else: elif i.ftype == "video":
var fileid = "" discard await b.sendVideo(response.chat.id, i.fileid, caption=i.caption)
var ftype = "" elif i.ftype == "videonote":
var fcaption = "" discard await b.sendVideoNote(response.chat.id, i.fileid)
if response.caption.isSome: elif i.ftype == "animation":
fcaption = response.caption.get discard await b.sendAnimation(response.chat.id, i.fileid, caption=i.caption)
if response.document.isSome: elif i.ftype == "sticker":
fileid = response.document.get.fileId discard await b.sendSticker(response.chat.id, i.fileid)
ftype = "document" else:
elif response.video.isSome: var fileid = ""
fileid = response.video.get.fileId var ftype = ""
ftype = "video" var fcaption = ""
elif response.videoNote.isSome: if response.caption.isSome:
fileid = response.videoNote.get.fileId fcaption = response.caption.get
ftype = "videonote" if response.document.isSome:
elif response.animation.isSome: fileid = response.document.get.fileId
fileid = response.animation.get.fileId ftype = "document"
ftype = "animation" elif response.video.isSome:
elif response.photo.isSome: fileid = response.video.get.fileId
fileid = response.photo.get[0].fileId ftype = "video"
ftype = "photo" elif response.videoNote.isSome:
elif response.sticker.isSome: fileid = response.videoNote.get.fileId
fileid = response.sticker.get.fileId ftype = "videonote"
ftype = "sticker" elif response.animation.isSome:
elif response.mediaGroupId.isSome: fileid = response.animation.get.fileId
fileid = response.mediaGroupId.get ftype = "animation"
elif response.photo.isSome:
fileid = response.photo.get[0].fileId
ftype = "photo"
elif response.sticker.isSome:
fileid = response.sticker.get.fileId
ftype = "sticker"
elif response.mediaGroupId.isSome:
fileid = response.mediaGroupId.get
let filehash = generate_hash() let filehash = generate_hash()
var CensoredRow = NewCensoredData(ftype, filehash, fileid, fcaption) var CensoredRow = NewCensoredData(ftype, filehash, fileid, fcaption)
with dbConn: with dbConn:
insert CensoredRow insert CensoredRow
discard await b.sendMessage(response.chat.id, "Censored --> " & "tg://resolve?domain=botbotbotnotabot&start=" & filehash) discard await b.sendMessage(response.chat.id, "Censored --> " & "tg://resolve?domain=botbotbotnotabot&start=" & filehash)
let bot = newTeleBot(API_KEY) let bot = newTeleBot(API_KEY)
bot.onUpdate(updateHandler) bot.onUpdate(updateHandler)