Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 当管理员尝试ban真寻时将被反杀 #1628

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions zhenxun/builtin_plugins/admin/ban/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from nonebot.adapters import Bot
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot_plugin_session import EventSession
from nonebot_plugin_alconna import (
Alconna,
Arparma,
At,
Match,
Option,
Alconna,
Arparma,
on_alconna,
store_true,
)
from nonebot_plugin_session import EventSession

from zhenxun.configs.config import Config
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.message import MessageUtils
from zhenxun.utils.rules import admin_check
from zhenxun.utils.message import MessageUtils
from zhenxun.configs.config import Config, BotConfig
from zhenxun.configs.utils import RegisterConfig, PluginExtraData

from ._data_source import BanManage

Expand Down Expand Up @@ -160,7 +160,9 @@ async def _(
duration: Match[int],
group_id: Match[str],
):
user_id = None
user_id = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): 考虑重构禁令功能以改善代码结构和可读性。

禁令功能确实随着反向禁令保护功能的添加变得更加复杂。虽然此功能对于防止滥用至关重要,但我们可以改进代码结构以增强可读性和可维护性。以下是一些建议:

  1. 将反向禁令逻辑提取到一个单独的函数中:
def handle_reverse_ban(session, bot, gid, arparma):
    if not user_id or user_id == bot.self_id and session.id1 not in bot.config.superusers:
        duration = 30
        logger.info(
            f"尝试ban {BotConfig.self_nickname} 反被拿下",
            arparma.header_result,
            session=session,
        )
        return session.id1, duration, "半 分钟"
    return None, None, None

# 在主函数中:
reverse_ban_user, reverse_ban_duration, reverse_ban_text = handle_reverse_ban(session, bot, gid, arparma)
if reverse_ban_user:
    await BanManage.ban(reverse_ban_user, gid, reverse_ban_duration, session, True)
    await MessageUtils.build_message([
        "倒反天罡,小小管理速速退下!对 ",
        At(flag="user", target=reverse_ban_user),
        f" 狠狠惩戒了一番,一脚踢进了小黑屋! 在里面乖乖呆 {reverse_ban_text}吧!",
    ]).finish(reply_to=True)
    return
  1. 简化主要禁令执行:
def execute_ban(user_id, gid, duration, session, is_superuser):
    BanManage.ban(user_id, gid, duration, session, is_superuser)
    logger.info(
        "管理员Ban" if not is_superuser else "超级用户Ban",
        arparma.header_result,
        session=session,
        target=f"{gid}:{user_id}",
    )

# 在主函数中:
execute_ban(user_id, gid, _duration, session, session.id1 in bot.config.superusers)
  1. 简化消息构建:
def construct_ban_message(user_id, user_result, duration_text):
    return [
        "对 ",
        At(flag="user", target=user_id) if isinstance(user_result, At) else user_id,
        f" 狠狠惩戒了一番,一脚踢进了小黑屋! 在里面乖乖呆 {duration_text} 吧!",
    ]

# 在主函数中:
await MessageUtils.build_message(construct_ban_message(user_id, user.result, _duration_text)).finish(reply_to=True)

这些更改将保持所有新功能,同时减少嵌套并改善代码的整体结构。主函数将更易于阅读和理解,提取的函数可以在需要时独立重用或测试。

Original comment in English

issue (complexity): Consider refactoring the ban function to improve code structure and readability.

The ban function has indeed become more complex with the addition of the reverse ban protection feature. While this feature is crucial for preventing abuse, we can improve the code structure to enhance readability and maintainability. Here are some suggestions:

  1. Extract the reverse ban logic into a separate function:
def handle_reverse_ban(session, bot, gid, arparma):
    if not user_id or user_id == bot.self_id and session.id1 not in bot.config.superusers:
        duration = 30
        logger.info(
            f"尝试ban {BotConfig.self_nickname} 反被拿下",
            arparma.header_result,
            session=session,
        )
        return session.id1, duration, "半 分钟"
    return None, None, None

# In the main function:
reverse_ban_user, reverse_ban_duration, reverse_ban_text = handle_reverse_ban(session, bot, gid, arparma)
if reverse_ban_user:
    await BanManage.ban(reverse_ban_user, gid, reverse_ban_duration, session, True)
    await MessageUtils.build_message([
        "倒反天罡,小小管理速速退下!对 ",
        At(flag="user", target=reverse_ban_user),
        f" 狠狠惩戒了一番,一脚踢进了小黑屋! 在里面乖乖呆 {reverse_ban_text}吧!",
    ]).finish(reply_to=True)
    return
  1. Simplify the main ban execution:
def execute_ban(user_id, gid, duration, session, is_superuser):
    BanManage.ban(user_id, gid, duration, session, is_superuser)
    logger.info(
        "管理员Ban" if not is_superuser else "超级用户Ban",
        arparma.header_result,
        session=session,
        target=f"{gid}:{user_id}",
    )

# In the main function:
execute_ban(user_id, gid, _duration, session, session.id1 in bot.config.superusers)
  1. Simplify the message construction:
def construct_ban_message(user_id, user_result, duration_text):
    return [
        "对 ",
        At(flag="user", target=user_id) if isinstance(user_result, At) else user_id,
        f" 狠狠惩戒了一番,一脚踢进了小黑屋! 在里面乖乖呆 {duration_text} 吧!",
    ]

# In the main function:
await MessageUtils.build_message(construct_ban_message(user_id, user.result, _duration_text)).finish(reply_to=True)

These changes will maintain all the new functionality while reducing nesting and improving the overall structure of the code. The main function will be easier to read and understand, and the extracted functions can be reused or tested independently if needed.

if not session.id1:
await MessageUtils.build_message("用户id为空...").finish(reply_to=True)
if user.available:
if isinstance(user.result, At):
user_id = user.result.target
Expand All @@ -169,35 +171,61 @@ async def _(
await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
user_id = user.result
_duration = duration.result * 60 if duration.available else -1
_duration_text = f"{duration.result} 分钟" if duration.available else " 到世界湮灭"
if (gid := session.id3 or session.id2) and not group_id.available:
if group_id.available:
gid = group_id.result
if (
not user_id
or user_id == bot.self_id
and session.id1 not in bot.config.superusers
):
_duration = 0.5
await MessageUtils.build_message("倒反天罡,小小管理速速退下!").send()
await BanManage.ban(session.id1, gid, 30, session, True)
_duration_text = "半 分钟"
logger.info(
f"尝试ban {BotConfig.self_nickname} 反被拿下",
arparma.header_result,
session=session,
)
await MessageUtils.build_message(
[
"对 ",
At(flag="user", target=session.id1),
" 狠狠惩戒了一番,一脚踢进了小黑屋!"
f" 在里面乖乖呆 {_duration_text}吧!",
]
).finish(reply_to=True)
await BanManage.ban(
user_id, gid, _duration, session, session.id1 in bot.config.superusers
)
logger.info(
f"管理员Ban",
"管理员Ban",
arparma.header_result,
session=session,
target=f"{gid}:{user_id}",
)
await MessageUtils.build_message(
[
"对 ",
At(flag="user", target=user_id) if isinstance(user.result, At) else user_id, # type: ignore
f" 狠狠惩戒了一番,一脚踢进了小黑屋!",
(
At(flag="user", target=user_id)
if isinstance(user.result, At)
else user_id
), # type: ignore
" 狠狠惩戒了一番,一脚踢进了小黑屋!",
f" 在里面乖乖呆 {_duration_text} 吧!",
]
).finish(reply_to=True)
elif session.id1 in bot.config.superusers:
_group_id = group_id.result if group_id.available else None
await BanManage.ban(user_id, _group_id, _duration, session, True)
logger.info(
f"超级用户Ban",
"超级用户Ban",
arparma.header_result,
session=session,
target=f"{_group_id}:{user_id}",
)
at_msg = user_id if user_id else f"群组:{_group_id}"
at_msg = user_id or f"群组:{_group_id}"
await MessageUtils.build_message(
f"对 {at_msg} 狠狠惩戒了一番,一脚踢进了小黑屋!"
).finish(reply_to=True)
Expand All @@ -211,7 +239,7 @@ async def _(
user: Match[str | At],
group_id: Match[str],
):
user_id = None
user_id = ""
if user.available:
if isinstance(user.result, At):
user_id = user.result.target
Expand All @@ -228,36 +256,40 @@ async def _(
user_id, gid, session, session.id1 in bot.config.superusers
):
logger.info(
f"管理员UnBan",
"管理员UnBan",
arparma.header_result,
session=session,
target=f"{gid}:{user_id}",
)
await MessageUtils.build_message(
[
"将 ",
At(flag="user", target=user_id) if isinstance(user.result, At) else u_d, # type: ignore
f" 从黑屋中拉了出来并急救了一下!",
(
At(flag="user", target=user_id)
if isinstance(user.result, At)
else u_d
), # type: ignore
" 从黑屋中拉了出来并急救了一下!",
]
).finish(reply_to=True)
else:
await MessageUtils.build_message(f"该用户不在黑名单中捏...").finish(
await MessageUtils.build_message("该用户不在黑名单中捏...").finish(
reply_to=True
)
elif session.id1 in bot.config.superusers:
_group_id = group_id.result if group_id.available else None
if await BanManage.unban(user_id, _group_id, session, True):
logger.info(
f"超级用户UnBan",
"超级用户UnBan",
arparma.header_result,
session=session,
target=f"{_group_id}:{user_id}",
)
at_msg = user_id if user_id else f"群组:{_group_id}"
at_msg = user_id or f"群组:{_group_id}"
await MessageUtils.build_message(
f"对 {at_msg} 从黑屋中拉了出来并急救了一下!"
).finish(reply_to=True)
else:
await MessageUtils.build_message(f"该用户不在黑名单中捏...").finish(
await MessageUtils.build_message("该用户不在黑名单中捏...").finish(
reply_to=True
)
3 changes: 1 addition & 2 deletions zhenxun/builtin_plugins/admin/ban/_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from nonebot_plugin_session import EventSession

from zhenxun.models.ban_console import BanConsole
from zhenxun.models.level_user import LevelUser
from zhenxun.models.ban_console import BanConsole
from zhenxun.utils.image_utils import BuildImage, ImageTemplate


class BanManage:

@classmethod
async def build_ban_image(
cls,
Expand Down
Loading