mirror of https://github.com/nocturn9x/BotBase.git
Added the DELETE_MESSAGES option to the antiflood module, minor fixes
This commit is contained in:
parent
9e6e158ac8
commit
66c56dda5a
|
@ -17,11 +17,10 @@ def create_database(path: str, query: str):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with database:
|
with database:
|
||||||
cursor = database.cursor()
|
database.executescript(query)
|
||||||
cursor.executescript(query)
|
database.close()
|
||||||
cursor.close()
|
|
||||||
except sqlite3.Error as query_error:
|
except sqlite3.Error as query_error:
|
||||||
logging.info(f"An error has occurred while executing query: {query_error}")
|
logging.info(f"An error has occurred while executing DB_CREATE: {query_error}")
|
||||||
|
|
||||||
|
|
||||||
def get_user(tg_id: int):
|
def get_user(tg_id: int):
|
||||||
|
@ -48,9 +47,8 @@ def update_name(tg_id: int, name: str):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with database:
|
with database:
|
||||||
cursor = database.cursor()
|
database.execute(DB_UPDATE_NAME, (name, tg_id))
|
||||||
query = cursor.execute(DB_UPDATE_NAME, (name, tg_id))
|
return True
|
||||||
return query.fetchone()
|
|
||||||
except sqlite3.Error as query_error:
|
except sqlite3.Error as query_error:
|
||||||
logging.error(f"An error has occurred while executing DB_UPDATE_NAME query: {query_error}")
|
logging.error(f"An error has occurred while executing DB_UPDATE_NAME query: {query_error}")
|
||||||
return query_error
|
return query_error
|
||||||
|
@ -80,9 +78,7 @@ def set_user(tg_id: int, uname: str):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with database:
|
with database:
|
||||||
cursor = database.cursor()
|
database.execute(DB_SET_USER, (None, tg_id, uname, time.strftime("%d/%m/%Y %T %p"), 0))
|
||||||
cursor.execute(DB_SET_USER, (None, tg_id, uname, time.strftime("%d/%m/%Y %T %p"), 0))
|
|
||||||
cursor.close()
|
|
||||||
return True
|
return True
|
||||||
except sqlite3.Error as query_error:
|
except sqlite3.Error as query_error:
|
||||||
logging.error(f"An error has occurred while executing DB_GET_USERS query: {query_error}")
|
logging.error(f"An error has occurred while executing DB_GET_USERS query: {query_error}")
|
||||||
|
@ -97,9 +93,7 @@ def ban_user(tg_id: int):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with database:
|
with database:
|
||||||
cursor = database.cursor()
|
database.execute(DB_BAN_USER, (tg_id, ))
|
||||||
cursor.execute(DB_BAN_USER, (tg_id, ))
|
|
||||||
cursor.close()
|
|
||||||
return True
|
return True
|
||||||
except sqlite3.Error as query_error:
|
except sqlite3.Error as query_error:
|
||||||
logging.error(f"An error has occurred while executing DB_BAN_USER query: {query_error}")
|
logging.error(f"An error has occurred while executing DB_BAN_USER query: {query_error}")
|
||||||
|
@ -114,9 +108,7 @@ def unban_user(tg_id: int):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with database:
|
with database:
|
||||||
cursor = database.cursor()
|
database.execute(DB_UNBAN_USER, (tg_id, ))
|
||||||
cursor.execute(DB_UNBAN_USER, (tg_id, ))
|
|
||||||
cursor.close()
|
|
||||||
return True
|
return True
|
||||||
except sqlite3.Error as query_error:
|
except sqlite3.Error as query_error:
|
||||||
logging.error(f"An error has occurred while executing DB_UNBAN_USER query: {query_error}")
|
logging.error(f"An error has occurred while executing DB_UNBAN_USER query: {query_error}")
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from pyrogram import Client, Filters, Message
|
from pyrogram import Client, Filters, Message, CallbackQueryHandler
|
||||||
from ..config import MAX_UPDATE_THRESHOLD, ANTIFLOOD_SENSIBILITY, BAN_TIME, ADMINS, BYPASS_FLOOD, FLOOD_NOTICE, \
|
from ..config import MAX_UPDATE_THRESHOLD, ANTIFLOOD_SENSIBILITY, BAN_TIME, ADMINS, BYPASS_FLOOD, FLOOD_NOTICE, \
|
||||||
COUNT_CALLBACKS_SEPARATELY, FLOOD_PERCENTAGE, CACHE, PRIVATE_ONLY
|
COUNT_CALLBACKS_SEPARATELY, FLOOD_PERCENTAGE, CACHE, PRIVATE_ONLY, DELETE_MESSAGES
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from ..methods.safe_send import send_message
|
from ..methods.safe_send import send_message
|
||||||
|
from ..methods.various import delete_messages
|
||||||
|
|
||||||
# Some variables for runtime configuration
|
# Some variables for runtime configuration
|
||||||
|
|
||||||
|
@ -22,49 +23,51 @@ def is_flood(updates: list):
|
||||||
genexpr = [i <= ANTIFLOOD_SENSIBILITY for i in
|
genexpr = [i <= ANTIFLOOD_SENSIBILITY for i in
|
||||||
((updates[i + 1] - timestamp) if i < (MAX_UPDATE_THRESHOLD - 1) else (timestamp - updates[i - 1]) for
|
((updates[i + 1] - timestamp) if i < (MAX_UPDATE_THRESHOLD - 1) else (timestamp - updates[i - 1]) for
|
||||||
i, timestamp in enumerate(updates))]
|
i, timestamp in enumerate(updates))]
|
||||||
limit = (len(genexpr) / 100) * FLOOD_PERCENTAGE
|
return genexpr.count(True) >= int((len(genexpr) / 100) * FLOOD_PERCENTAGE)
|
||||||
if genexpr.count(True) >= limit:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@Client.on_callback_query(~BYPASS_USERS, group=-1)
|
|
||||||
@Client.on_message(FILTER & ~BYPASS_USERS, group=-1)
|
@Client.on_message(FILTER & ~BYPASS_USERS, group=-1)
|
||||||
def anti_flood(client, update):
|
def anti_flood(client, update):
|
||||||
"""Anti flood module"""
|
"""Anti flood module"""
|
||||||
|
|
||||||
VAR = MESSAGES if isinstance(update, Message) else QUERIES
|
user_id = update.from_user.id
|
||||||
if isinstance(VAR[update.from_user.id], tuple):
|
if isinstance(update, Message):
|
||||||
chat, date = VAR[update.from_user.id]
|
VAR = MESSAGES
|
||||||
if time.time() - date >= BAN_TIME:
|
chat = update.chat.id
|
||||||
logging.warning(f"{update.from_user.id} has waited at least {BAN_TIME} seconds and can now text again")
|
date = update.date
|
||||||
BANNED_USERS.remove(update.from_user.id)
|
message_id = update.message_id
|
||||||
del VAR[update.from_user.id]
|
|
||||||
elif len(VAR[update.from_user.id]) >= MAX_UPDATE_THRESHOLD:
|
|
||||||
logging.info(f"MAX_MESS_THRESHOLD ({MAX_UPDATE_THRESHOLD}) Reached for {update.from_user.id}")
|
|
||||||
timestamps = VAR.pop(update.from_user.id)
|
|
||||||
if is_flood(timestamps):
|
|
||||||
logging.warning(f"Flood detected from {update.from_user.id} in chat {update.chat.id}")
|
|
||||||
if update.from_user.id in CACHE:
|
|
||||||
del CACHE[update.from_user.id]
|
|
||||||
BANNED_USERS.add(update.from_user.id)
|
|
||||||
if isinstance(update, Message):
|
|
||||||
chatid = update.chat.id
|
|
||||||
else:
|
|
||||||
chatid = update.from_user.id
|
|
||||||
VAR[update.from_user.id] = chatid, time.monotonic()
|
|
||||||
if FLOOD_NOTICE:
|
|
||||||
send_message(client, update.from_user.id, FLOOD_NOTICE)
|
|
||||||
else:
|
|
||||||
if update.from_user.id in VAR:
|
|
||||||
del VAR[update.from_user.id]
|
|
||||||
else:
|
else:
|
||||||
if isinstance(update, Message):
|
VAR = QUERIES
|
||||||
date = update.date
|
message_id = None
|
||||||
|
chat = user_id
|
||||||
|
if update.message:
|
||||||
|
date = update.message.date
|
||||||
else:
|
else:
|
||||||
if update.message:
|
date = time.time()
|
||||||
date = update.message.date
|
if isinstance(VAR[user_id], tuple):
|
||||||
else:
|
chat, date = VAR[user_id]
|
||||||
date = time.monotonic()
|
if time.time() - date >= BAN_TIME:
|
||||||
VAR[update.from_user.id].append(date)
|
logging.warning(f"{user_id} has waited at least {BAN_TIME} seconds and can now text again")
|
||||||
|
BANNED_USERS.remove(user_id)
|
||||||
|
del VAR[user_id]
|
||||||
|
elif len(VAR[user_id]) >= MAX_UPDATE_THRESHOLD:
|
||||||
|
VAR[user_id].append({chat: (date, message_id)})
|
||||||
|
logging.info(f"MAX_UPDATE_THRESHOLD ({MAX_UPDATE_THRESHOLD}) Reached for {user_id}")
|
||||||
|
user_data = VAR.pop(user_id)
|
||||||
|
timestamps = [list(*d.values())[0] for d in user_data]
|
||||||
|
updates = [list(*d.values())[1] for d in user_data]
|
||||||
|
if is_flood(timestamps):
|
||||||
|
logging.warning(f"Flood detected from {user_id} in chat {chat}")
|
||||||
|
if user_id in CACHE:
|
||||||
|
del CACHE[user_id]
|
||||||
|
BANNED_USERS.add(user_id)
|
||||||
|
VAR[user_id] = chat, time.time()
|
||||||
|
if FLOOD_NOTICE:
|
||||||
|
send_message(client, True, user_id, FLOOD_NOTICE)
|
||||||
|
if DELETE_MESSAGES and any(updates):
|
||||||
|
delete_messages(client, True, chat, filter(bool, updates))
|
||||||
|
else:
|
||||||
|
if user_id in VAR:
|
||||||
|
del VAR[user_id]
|
||||||
|
else:
|
||||||
|
VAR[user_id].append({chat: (date, message_id)})
|
||||||
|
|
|
@ -14,6 +14,8 @@ def check_user_banned(tg_id: int):
|
||||||
if isinstance(res, Exception):
|
if isinstance(res, Exception):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
|
if not res:
|
||||||
|
return False
|
||||||
if res[-1]:
|
if res[-1]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
@ -50,13 +52,13 @@ def start_handler(client, message):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@Client.on_callback_query(Filters.callback_data("info"))
|
@Client.on_callback_query(Filters.callback_data("info") & ~BANNED_USERS)
|
||||||
def bot_info(_, query):
|
def bot_info(_, query):
|
||||||
buttons = InlineKeyboardMarkup([[InlineKeyboardButton("🔙 Back", "back_start")]])
|
buttons = InlineKeyboardMarkup([[InlineKeyboardButton("🔙 Back", "back_start")]])
|
||||||
edit_message_text(query, True, CREDITS.format(), reply_markup=buttons)
|
edit_message_text(query, True, CREDITS.format(), reply_markup=buttons)
|
||||||
|
|
||||||
|
|
||||||
@Client.on_callback_query(Filters.callback_data("back_start"))
|
@Client.on_callback_query(Filters.callback_data("back_start") & ~BANNED_USERS)
|
||||||
def back_start(_, query):
|
def back_start(_, query):
|
||||||
if query.from_user.first_name:
|
if query.from_user.first_name:
|
||||||
name = query.from_user.first_name
|
name = query.from_user.first_name
|
||||||
|
@ -69,9 +71,7 @@ def back_start(_, query):
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
for chatid, message_ids in data:
|
for chatid, message_ids in data:
|
||||||
delete_messages(_, True, chatid, message_ids)
|
delete_messages(_, True, chatid, message_ids)
|
||||||
start_handler(_, query)
|
edit_message_text(query, True,
|
||||||
else:
|
GREET.format(mention=f"[{name}](tg://user?id={query.from_user.id})", id=query.from_user.id,
|
||||||
edit_message_text(query, True,
|
username=query.from_user.username),
|
||||||
GREET.format(mention=f"[{name}](tg://user?id={query.from_user.id})", id=query.from_user.id,
|
reply_markup=BUTTONS)
|
||||||
username=query.from_user.username),
|
|
||||||
reply_markup=BUTTONS)
|
|
||||||
|
|
4
bot.py
4
bot.py
|
@ -1,5 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
from pyrogram import Client
|
from pyrogram import Client, CallbackQueryHandler
|
||||||
from pyrogram.session import Session
|
from pyrogram.session import Session
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
|
@ -7,10 +7,12 @@ import importlib
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
MODULE_NAME = "BotBase" # Change this to match the FOLDER name that contains the config.py file
|
MODULE_NAME = "BotBase" # Change this to match the FOLDER name that contains the config.py file
|
||||||
conf = importlib.import_module(f"{MODULE_NAME}.config")
|
conf = importlib.import_module(f"{MODULE_NAME}.config")
|
||||||
|
antiflood = importlib.import_module(f"{MODULE_NAME}.modules.antiflood")
|
||||||
dbmodule = importlib.import_module(f"{MODULE_NAME}.database.query")
|
dbmodule = importlib.import_module(f"{MODULE_NAME}.database.query")
|
||||||
logging.basicConfig(format=conf.LOGGING_FORMAT, datefmt=conf.DATE_FORMAT, level=conf.LOGGING_LEVEL)
|
logging.basicConfig(format=conf.LOGGING_FORMAT, datefmt=conf.DATE_FORMAT, level=conf.LOGGING_LEVEL)
|
||||||
bot = Client(api_id=conf.API_ID, api_hash=conf.API_HASH, bot_token=conf.BOT_TOKEN, plugins=conf.PLUGINS_ROOT,
|
bot = Client(api_id=conf.API_ID, api_hash=conf.API_HASH, bot_token=conf.BOT_TOKEN, plugins=conf.PLUGINS_ROOT,
|
||||||
session_name=conf.SESSION_NAME, workers=conf.WORKERS_NUM)
|
session_name=conf.SESSION_NAME, workers=conf.WORKERS_NUM)
|
||||||
|
bot.add_handler(CallbackQueryHandler(antiflood.anti_flood, ~antiflood.BYPASS_USERS), group=-1)
|
||||||
Session.notice_displayed = True
|
Session.notice_displayed = True
|
||||||
try:
|
try:
|
||||||
logging.warning("Running create_database()")
|
logging.warning("Running create_database()")
|
||||||
|
|
Loading…
Reference in New Issue