blacklist DSPL 💔 2 (#1294)
This commit is contained in:
+29
-10
@@ -34,7 +34,7 @@ from wcwidth import wcswidth
|
||||
# BOT/__init__.py has already put BOTS/SHARED on sys.path; re-export it
|
||||
# under a public name so peer modules can use it for asset paths.
|
||||
from . import SHARED_DIR # noqa: F401 — re-exported for siblings
|
||||
from shared_store import check_user_blacklist, blacklisted_guilds
|
||||
from shared_store import check_user_blacklist, check_guild_blacklist, blacklisted_guilds
|
||||
from data_parser import (
|
||||
LangTableReader,
|
||||
UnitTags,
|
||||
@@ -471,8 +471,17 @@ class AdminCheckFailure(app_commands.CheckFailure):
|
||||
|
||||
|
||||
class BlacklistCheckFailure(app_commands.CheckFailure):
|
||||
"""Raised when a blacklisted user or guild attempts to run a command."""
|
||||
pass
|
||||
"""Raised when a blacklisted user or guild attempts to run a command.
|
||||
|
||||
``reason`` carries the optional BLACKLIST.json reason; ``is_guild`` marks
|
||||
whether the block came from the guild (vs the invoking user) so the error
|
||||
handler can render a server-specific message.
|
||||
"""
|
||||
|
||||
def __init__(self, reason: Optional[str] = None, *, is_guild: bool = False):
|
||||
super().__init__(reason or "")
|
||||
self.reason = reason
|
||||
self.is_guild = is_guild
|
||||
|
||||
|
||||
class TierGateFailure(app_commands.CheckFailure):
|
||||
@@ -536,8 +545,10 @@ def is_blacklisted():
|
||||
"""
|
||||
async def predicate(interaction: discord.Interaction):
|
||||
guild = interaction.guild
|
||||
if guild is not None and guild.id in blacklisted_guilds():
|
||||
raise BlacklistCheckFailure(t("en", "common.access_denied_desc"))
|
||||
if guild is not None:
|
||||
g_blocked, g_reason = check_guild_blacklist(guild.id)
|
||||
if g_blocked:
|
||||
raise BlacklistCheckFailure(g_reason, is_guild=True)
|
||||
|
||||
blocked, reason = check_user_blacklist(interaction.user.id)
|
||||
if blocked:
|
||||
@@ -575,17 +586,25 @@ async def permission_fail(interaction: discord.Interaction, error):
|
||||
"""Handle permission-related errors with appropriate embeds."""
|
||||
lang = await guild_lang(interaction.guild_id) if interaction.guild_id else "en"
|
||||
if isinstance(error, BlacklistCheckFailure):
|
||||
reason = error.args[0] if error.args else None
|
||||
reason = getattr(error, "reason", None)
|
||||
is_guild = getattr(error, "is_guild", False)
|
||||
logging.warning(
|
||||
"Blacklisted command attempt blocked: user_id=%s guild_id=%s command=%s reason=%r",
|
||||
"Blacklisted command attempt blocked: user_id=%s guild_id=%s command=%s is_guild=%s reason=%r",
|
||||
getattr(interaction.user, "id", None),
|
||||
interaction.guild_id,
|
||||
interaction.command.qualified_name if interaction.command else None,
|
||||
is_guild,
|
||||
reason,
|
||||
)
|
||||
desc = t(lang, "permission.blacklisted_desc")
|
||||
if reason:
|
||||
desc += "\n" + t(lang, "permission.reason_line", reason=reason)
|
||||
if is_guild:
|
||||
if reason:
|
||||
desc = t(lang, "common.server_blacklisted_reason", reason=reason)
|
||||
else:
|
||||
desc = t(lang, "common.access_denied_desc")
|
||||
else:
|
||||
desc = t(lang, "permission.blacklisted_desc")
|
||||
if reason:
|
||||
desc += "\n" + t(lang, "permission.reason_line", reason=reason)
|
||||
embed = discord.Embed(
|
||||
title=t(lang, "permission.blacklisted_title"),
|
||||
description=desc,
|
||||
|
||||
Reference in New Issue
Block a user