diff --git a/modules/admin/blacklist.py b/modules/admin/blacklist.py new file mode 100644 index 0000000..cace09b --- /dev/null +++ b/modules/admin/blacklist.py @@ -0,0 +1,37 @@ +import discord +from discord.ext import commands + +from lib.const import CONST +from services.blacklist_service import BlacklistUserService +from ui.embeds import Builder + + +class Blacklist(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.command(name="blacklist") + @commands.is_owner() + async def blacklist_command( + self, + ctx: commands.Context[commands.Bot], + user: discord.User, + *, + reason: str | None = None, + ) -> None: + blacklist_service = BlacklistUserService(user.id) + blacklist_service.add_to_blacklist(reason) + + embed = Builder.create_embed( + theme="success", + user_name=ctx.author.name, + author_text=CONST.STRINGS["admin_blacklist_author"], + description=CONST.STRINGS["admin_blacklist_description"].format(user.name), + footer_text=CONST.STRINGS["admin_blacklist_footer"], + ) + + await ctx.send(embed=embed) + + +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(Blacklist(bot)) diff --git a/services/blacklist_service.py b/services/blacklist_service.py new file mode 100644 index 0000000..24220d2 --- /dev/null +++ b/services/blacklist_service.py @@ -0,0 +1,39 @@ +from db import database + + +class BlacklistUserService: + def __init__(self, user_id: int) -> None: + self.user_id: int = user_id + + def add_to_blacklist(self, reason: str | None = None) -> None: + """ + Adds a user to the blacklist with the given reason. + + Args: + reason (str): The reason for blacklisting the user. + """ + query: str = """ + INSERT INTO blacklist_user (user_id, reason) + VALUES (%s, %s) + ON DUPLICATE KEY UPDATE reason = VALUES(reason) + """ + database.execute_query(query, (self.user_id, reason)) + + @staticmethod + def is_user_blacklisted(user_id: int) -> bool: + """ + Checks if a user is currently blacklisted. + + Args: + user_id (int): The ID of the user to check. + + Returns: + bool: True if the user is blacklisted, False otherwise. + """ + query: str = """ + SELECT active + FROM blacklist_user + WHERE user_id = %s + """ + result: list[tuple[bool]] = database.select_query(query, (user_id,)) + return any(active for (active,) in result)