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