From a509270989a12327c069d7124c2d09004db4f780 Mon Sep 17 00:00:00 2001 From: diamante0018 Date: Wed, 30 Apr 2025 09:26:39 +0200 Subject: [PATCH] chore: lighten the load for messages --- bot/commands.py | 60 ++++++++++++++++++++++++++- bot/config.py | 4 -- bot/events_handlers/message_events.py | 35 +--------------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/bot/commands.py b/bot/commands.py index a7dc40a..49bdb03 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,15 +1,33 @@ from typing import Literal +import re import discord from discord import app_commands -from database import add_pattern, add_user_to_blacklist +from database import add_pattern, add_user_to_blacklist, is_user_blacklisted -from bot.config import update_patterns +from bot.config import update_patterns, message_patterns from bot.utils import compile_stats, fetch_game_stats, perform_search GUILD_ID = 1110531063161299074 +BOT_LOG = 1112049391482703873 +GENERAL_CHANNEL = 1110531063744303138 +ALLOWED_CHANNELS = [ + GENERAL_CHANNEL, + 1119371841711112314, # vip-channel + 1112048063448617142, # off-topic + 1112016681880014928, # mw2 sp + 1145459504436220014, # iw5 support + 1145469136919613551, # s1 general + 1145459788151537804, # s1 support + 1145469106133401682, # iw6 general + 1145458770122649691, # iw6 support + 1180796251529293844, # bo3 general + 1180796301953212537, # bo3 support + BOT_LOG, +] + async def setup(bot): async def on_tree_error( @@ -92,6 +110,44 @@ async def setup(bot): await interaction.response.send_message(stats_message, ephemeral=True) + @bot.tree.command( + name="meme", + description="Check if the input matches any predefined memess.", + guild=discord.Object(id=GUILD_ID), + ) + async def meme(interaction: discord.Interaction, input: str): + """ + Slash command to check if the input matches any predefined patterns. + """ + # Check if the command is executed in an allowed channel + if interaction.channel_id not in ALLOWED_CHANNELS: + await interaction.response.send_message( + "This command cannot be used in this channel.", ephemeral=True + ) + return + + # Check if the user is blacklisted + if is_user_blacklisted(interaction.user.id): + await interaction.response.send_message( + "You are blacklisted from using this command.", ephemeral=True + ) + return + + # Check if any of the patterns match the input + for pattern in message_patterns: + if re.search(pattern["regex"], input, re.IGNORECASE): + response = pattern["response"] + reply_message = await interaction.response.send_message( + response, ephemeral=False + ) + # Add a reaction to the reply message (if the user decides to delete it) + await reply_message.add_reaction("\U0000274C") + break + else: + await interaction.response.send_message( + "No matching patterns found.", ephemeral=True + ) + await bot.tree.sync(guild=discord.Object(id=GUILD_ID)) # Force sync print("Commands extension loaded!") diff --git a/bot/config.py b/bot/config.py index 7e7d332..1895e91 100644 --- a/bot/config.py +++ b/bot/config.py @@ -2,10 +2,6 @@ import json import os from database import get_patterns -PATTERNS_FILE = "patterns.json" - -BOT_DATA_DIR = os.getenv("BOT_DATA_DIR", "/bot-data") - message_patterns = get_patterns() diff --git a/bot/events_handlers/message_events.py b/bot/events_handlers/message_events.py index d84955c..a3caad9 100644 --- a/bot/events_handlers/message_events.py +++ b/bot/events_handlers/message_events.py @@ -1,12 +1,10 @@ from datetime import timedelta -import re import discord from bot.utils import timeout_member, aware_utcnow -from bot.config import message_patterns from bot.ai.handle_request import forward_to_google_api -from database import add_user_to_role, is_user_blacklisted +from database import add_user_to_role BOT_LOG = 1112049391482703873 GENERAL_CHANNEL = 1110531063744303138 @@ -20,20 +18,6 @@ HATE_ME_USER_ID = 748201351665680438 HATE_ME_URL = "https://cdn.discordapp.com/attachments/1160511084143312959/1361051561400205524/download_1.png" hate_me_last_response_time = None -ALLOWED_CHANNELS = [ - GENERAL_CHANNEL, - 1112048063448617142, # off-topic - 1112016681880014928, # mw2 sp - 1145459504436220014, # iw5 support - 1145469136919613551, # s1 general - 1145459788151537804, # s1 support - 1145469106133401682, # iw6 general - 1145458770122649691, # iw6 support - 1180796251529293844, # bo3 general - 1180796301953212537, # bo3 support - BOT_LOG, -] - SPAM_ROLE_ID = 1350511935677927514 STAFF_ROLE_ID = 1112016152873414707 GROK_ROLE_ID = 1362837967919386916 @@ -422,20 +406,3 @@ async def handle_message(message, bot): ): await message.reply(FAILED_EMBED_MESSAGE) return - - # Check if the message is in an allowed channel - 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: - if re.search(pattern["regex"], message.content, re.IGNORECASE): - response = pattern["response"] - reply_message = await message.reply(response, mention_author=True) - # Add a reaction to the reply message (if the user decides to delete it) - await reply_message.add_reaction("\U0000274C") - break