mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 16:23:12 +00:00
Add blacklist command
This commit is contained in:
parent
79d64f793c
commit
024c9cb643
2 changed files with 76 additions and 0 deletions
37
modules/admin/blacklist.py
Normal file
37
modules/admin/blacklist.py
Normal file
|
@ -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))
|
39
services/blacklist_service.py
Normal file
39
services/blacklist_service.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue