From 2dc9604dc9d1d01f19c74d4c2f2f66f07c307139 Mon Sep 17 00:00:00 2001 From: diamante0018 Date: Fri, 28 Mar 2025 14:00:14 +0100 Subject: [PATCH] feat: black list for memes --- bot/commands.py | 17 ++++++++++++++- bot/events_handlers/message_events.py | 6 +++++- database/__init__.py | 31 +++++++++++++++++++++++++-- database/schema.sql | 6 ++++++ 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/bot/commands.py b/bot/commands.py index 02764a1..a7dc40a 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -3,7 +3,7 @@ from typing import Literal import discord from discord import app_commands -from database import add_pattern +from database import add_pattern, add_user_to_blacklist from bot.config import update_patterns from bot.utils import compile_stats, fetch_game_stats, perform_search @@ -44,6 +44,21 @@ async def setup(bot): f"Pattern added!\n**Regex:** `{regex}`\n**Response:** `{response}`" ) + @bot.tree.command( + name="add_to_blacklist", + description="Add a user to the blacklist.", + guild=discord.Object(id=GUILD_ID), + ) + @app_commands.checks.has_permissions(administrator=True) + async def add_to_blacklist_cmd( + interaction: discord.Interaction, user: discord.User, reason: str + ): + """Slash command to add a user to the blacklist.""" + add_user_to_blacklist(user.id, reason) + await interaction.response.send_message( + f"User **{user.name}** has been added to the blacklist.\n**Reason:** `{reason}`" + ) + @bot.tree.command( name="search", description="Search for servers by hostname or IP.", diff --git a/bot/events_handlers/message_events.py b/bot/events_handlers/message_events.py index 4f9854d..888dc18 100644 --- a/bot/events_handlers/message_events.py +++ b/bot/events_handlers/message_events.py @@ -5,7 +5,7 @@ import discord from bot.utils import timeout_member, aware_utcnow from bot.config import message_patterns -from database import add_user_to_role +from database import add_user_to_role, is_user_blacklisted BOT_LOG = 1112049391482703873 @@ -15,6 +15,7 @@ crazy_last_response_time = None ALLOWED_CHANNELS = [ 1110531063744303138, # GENERAL_CHANNEL + 1112048063448617142, # off-topic 1145458108190163014, # mw2 general 1145456435518525611, # mw2 mp 1112016681880014928, # mw2 sp @@ -327,6 +328,9 @@ async def handle_message(message, bot): if message.channel.id not in ALLOWED_CHANNELS: return + if is_user_blacklisted(message.author.id): + return + # Check if any of the patterns match the message # print('Checking for patterns...') for pattern in message_patterns: diff --git a/database/__init__.py b/database/__init__.py index 43733d0..64fd64e 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -87,12 +87,39 @@ def add_user_to_role(user_id: int, role_id: int, user_name: str): conn.close() -def user_has_role(user_id: int): +def user_has_role(user_id: int) -> bool: """Checks if a user is in the database.""" conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() - cursor.execute("SELECT * FROM user_roles WHERE user_id = ?", (user_id,)) + cursor.execute("SELECT 1 FROM user_roles WHERE user_id = ?", (user_id,)) + result = cursor.fetchone() + + conn.close() + + return result is not None + + +def add_user_to_blacklist(user_id: int, reason: str): + """Adds a user to the blacklist.""" + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + cursor.execute( + "INSERT OR IGNORE INTO black_list (user_id, date_assigned, reason) VALUES (?, ?, ?)", + (user_id, aware_utcnow().isoformat(), reason), + ) + + conn.commit() + conn.close() + + +def is_user_blacklisted(user_id: int) -> bool: + """Checks if a user is on the blacklist.""" + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + cursor.execute("SELECT 1 FROM black_list WHERE user_id = ?", (user_id,)) result = cursor.fetchone() conn.close() diff --git a/database/schema.sql b/database/schema.sql index de609f8..f548607 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -10,3 +10,9 @@ CREATE TABLE IF NOT EXISTS user_roles ( date_assigned TEXT, user_name TEXT ); + +CREATE TABLE IF NOT EXISTS black_list ( + user_id INTEGER PRIMARY KEY, + date_assigned TEXT, + reason TEXT +);