config bans, cascade deletion, add albemic, add enable_stan and get_quotes

This commit is contained in:
Rykov7
2023-04-06 01:11:01 +03:00
parent bcce3aa629
commit 99148b5bb6
5 changed files with 45 additions and 16 deletions
+10 -13
View File
@@ -7,18 +7,16 @@ from . import rules
from .helpers import represent_as_get, detect_args, update_stats
from .filters import *
from .config import *
from .database import session
from .models import Chat
""" [ ANTISPAM ] """
@bot.edited_message_handler(func=in_spam_list)
@bot.message_handler(func=in_spam_list)
@bot.edited_message_handler(func=in_spam_list, chat_types=["supergroup", "group"])
@bot.message_handler(func=in_spam_list, chat_types=["supergroup", "group"])
def moderate_messages(message: types.Message):
"""Ban user and delete their message."""
logging.info(
f"[BAN] {message.from_user.id} {message.from_user.username} - {message.text}"
f"[BAN] [{message.from_user.id}] {message.from_user.username}: {message.text}"
)
bot.delete_message(message.chat.id, message.id)
bot.ban_chat_member(message.chat.id, message.from_user.id)
@@ -26,7 +24,7 @@ def moderate_messages(message: types.Message):
s["Banned"] += 1
@bot.message_handler(func=in_caption_spam_list, content_types=["video"])
@bot.message_handler(func=in_caption_spam_list, content_types=["video"], chat_types=["supergroup", "group"])
def catch_videos(message: types.Message):
"""Catch offensive videos"""
logging.info(
@@ -38,14 +36,13 @@ def catch_videos(message: types.Message):
s["Banned"] += 1
@bot.edited_message_handler(func=in_delete_list)
@bot.message_handler(func=in_delete_list)
@bot.edited_message_handler(func=in_delete_list, chat_types=["supergroup", "group"])
@bot.message_handler(func=in_delete_list, chat_types=["supergroup", "group"])
def delete_message(message: types.Message):
if session.query(Chat.antispam).filter_by(chat_id=message.chat.id).first()[0]:
"""Delete unwanted message."""
bot.delete_message(message.chat.id, message.id)
with shelve.open(f"{DATA}{message.chat.id}") as s:
s["Deleted"] += 1
"""Delete unwanted message."""
bot.delete_message(message.chat.id, message.id)
with shelve.open(f"{DATA}{message.chat.id}") as s:
s["Deleted"] += 1
""" [ COMMANDS ] """
+28
View File
@@ -2,6 +2,8 @@
import logging
from datetime import datetime as dt
from ..models import Chat
from ..database import session
from ..config import bot, ADMIN_ID, types
from .. import reminder
from .. import report
@@ -66,3 +68,29 @@ def send_stats(message: types.Message):
logging.warning("reset_stats")
report.reset_report_stats(message.chat.id)
bot.send_message(message.chat.id, report.reset_report_stats(message.chat.id))
@bot.message_handler(commands=["enable_stan"], chat_types=["supergroup", "group"])
def enable_stan(message: types.Message):
"""Add group to database."""
logging.info(
f"[{message.chat.title}] [{message.from_user.id}] {message.from_user.username}: {message.text}"
)
if not session.query(Chat.id).filter(Chat.id == message.chat.id).first():
logging.info('[TRUE] chat.id == message.chat.id')
session.add(Chat(chat_id=message.chat.id, title=message.chat.title, antispam=1, report=0, reminder=1))
session.commit()
bot.send_message(message.chat.id, f"""Группа "{message.chat.title}"
Добавлена в БД
Текущие настройки:
Антиспам: {session.query(Chat.antispam).filter(Chat.chat_id == message.chat.id).first()[0]}
Ежедневные отчёты: {session.query(Chat.report).filter(Chat.chat_id == message.chat.id).first()[0]}
Праздники: {session.query(Chat.reminder).filter(Chat.chat_id == message.chat.id).first()[0]}""")
@bot.message_handler(commands=["get_quotes"], chat_types=["supergroup", "group"])
def get_quotes(message: types.Message):
logging.info('[TRUE] getting quotes...')
quotes = session.query(Chat.quotes).filter(Chat.id == message.chat.id).all()
bot.send_message(message.chat.id, f"{quotes}")
+6 -2
View File
@@ -1,9 +1,12 @@
from .database import session
from .models import Chat
from .config import *
def in_spam_list(message: types.Message) -> bool:
"""Check for mentioning unwanted persons in text."""
if message.from_user.id not in WHITEIDS:
antispam_is_enabled = session.query(Chat.antispam).filter_by(chat_id=message.chat.id).first()[0]
if message.from_user.id not in WHITEIDS and antispam_is_enabled:
for url in SPAM:
if url in message.text.casefold():
return True
@@ -25,7 +28,8 @@ def in_not_allowed(word_list, msg):
def in_delete_list(message: types.Message) -> bool:
"""Check for URLs in message and delete."""
if message.from_user.id not in WHITEIDS:
antispam_is_enabled = session.query(Chat.antispam).filter_by(chat_id=message.chat.id).first()[0]
if message.from_user.id not in WHITEIDS and antispam_is_enabled:
if URL_RX.search(message.text) and in_not_allowed(
ALLOWED_WORDS, message.text
):
+1 -1
View File
@@ -21,7 +21,7 @@ class Chat(Base):
title = Column(String)
# Settings
quotes = relationship('Quote', backref='chat')
quotes = relationship('Quote', backref='chat', cascade='all, delete')
antispam = Column(Boolean)
report = Column(Boolean)
reminder = Column(Boolean)