Skip to content

Commit

Permalink
🎨 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
HibiKier committed Sep 4, 2024
1 parent ce97ded commit c36bb97
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
60 changes: 31 additions & 29 deletions zhenxun/builtin_plugins/record_request.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import time
from datetime import datetime

from nonebot.plugin import PluginMetadata
from nonebot import on_message, on_request
from nonebot_plugin_session import EventSession
from nonebot_plugin_apscheduler import scheduler
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot.adapters.onebot.v11 import (
ActionFailed,
GroupRequestEvent,
FriendRequestEvent,
GroupRequestEvent,
)
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot.plugin import PluginMetadata
from nonebot_plugin_apscheduler import scheduler
from nonebot_plugin_session import EventSession

from zhenxun.services.log import logger
from zhenxun.utils.message import MessageUtils
from zhenxun.configs.config import BotConfig, Config
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.models.fg_request import FgRequest
from zhenxun.utils.platform import PlatformUtils
from zhenxun.models.friend_user import FriendUser
from zhenxun.configs.config import Config, BotConfig
from zhenxun.models.group_console import GroupConsole
from zhenxun.configs.utils import RegisterConfig, PluginExtraData
from zhenxun.utils.enum import PluginType, RequestType, RequestHandleType
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType, RequestHandleType, RequestType
from zhenxun.utils.message import MessageUtils
from zhenxun.utils.platform import PlatformUtils

base_config = Config.get("invite_manager")

Expand Down Expand Up @@ -77,14 +77,6 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi
# sex = user["sex"]
# age = str(user["age"])
comment = event.comment
if superuser:
await MessageUtils.build_message(
f"*****一份好友申请*****\n"
f"昵称:{nickname}({event.user_id})\n"
f"自动同意:{'√' if base_config.get('AUTO_ADD_FRIEND') else '×'}\n"
f"日期:{str(datetime.now()).split('.')[0]}\n"
f"备注:{event.comment}"
).send(target=PlatformUtils.get_target(bot, superuser))
if base_config.get("AUTO_ADD_FRIEND"):
logger.debug(
"已开启好友请求自动同意,成功通过该请求",
Expand All @@ -102,7 +94,7 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi
user_id=str(event.user_id),
handle_type__isnull=True,
).update(handle_type=RequestHandleType.EXPIRE)
await FgRequest.create(
f = await FgRequest.create(
request_type=RequestType.FRIEND,
platform=session.platform,
bot_id=bot.self_id,
Expand All @@ -111,13 +103,22 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi
nickname=nickname,
comment=comment,
)
if superuser:
await PlatformUtils.send_superuser(
bot,
f"*****一份好友申请*****\n"
f"ID: {f.id}"
f"昵称:{nickname}({event.user_id})\n"
f"自动同意:{'√' if base_config.get('AUTO_ADD_FRIEND') else '×'}\n"
f"日期:{str(datetime.now()).split('.')[0]}\n"
f"备注:{event.comment}",
)
else:
logger.debug("好友请求五分钟内重复, 已忽略", "好友请求", target=event.user_id)


@group_req.handle()
async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSession):
superuser = BotConfig.get_superuser("qq")
if event.sub_type == "invite":
if str(event.user_id) in bot.config.superusers:
try:
Expand Down Expand Up @@ -168,12 +169,6 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio
target=event.group_id,
)
nickname = await FriendUser.get_user_name(str(event.user_id))
await PlatformUtils.send_superuser(
bot,
f"*****一份入群申请*****\n申请人:{nickname}({event.user_id})\n群聊:"
f"{event.group_id}\n邀请日期:{datetime.now().replace(microsecond=0)}",
superuser,
)
await bot.send_private_msg(
user_id=event.user_id,
message=f"想要邀请我偷偷入群嘛~已经提醒{BotConfig.self_nickname}的管理员大人了\n"
Expand All @@ -187,7 +182,7 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio
group_id=str(event.group_id),
handle_type__isnull=True,
).update(handle_type=RequestHandleType.EXPIRE)
await FgRequest.create(
f = await FgRequest.create(
request_type=RequestType.GROUP,
platform=session.platform,
bot_id=bot.self_id,
Expand All @@ -196,6 +191,13 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio
nickname=nickname,
group_id=str(event.group_id),
)
await PlatformUtils.send_superuser(
bot,
f"*****一份入群申请*****\n"
"ID:{f.id}\n"
"申请人:{nickname}({event.user_id})\n群聊:"
f"{event.group_id}\n邀请日期:{datetime.now().replace(microsecond=0)}",
)
else:
logger.debug(
"群聊请求五分钟内重复, 已忽略",
Expand Down
8 changes: 4 additions & 4 deletions zhenxun/builtin_plugins/superuser/request_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ async def _(
await _id_img.circle_corner(10)
await background.paste(_id_img, (10, 0), center_type="height")
img_list.append(background)
A = await BuildImage.auto_paste(img_list, 1)
if A:
if img_list:
A = await BuildImage.auto_paste(img_list, 1)
result_image = BuildImage(
A.width, A.height + 30, color=(255, 255, 255), font_size=20
)
Expand All @@ -237,8 +237,8 @@ async def _(
await MessageUtils.build_message("没有任何请求喔...").finish(reply_to=True)
if len(req_image_list) == 1:
await MessageUtils.build_message(req_image_list[0]).finish()
width = sum([img.width for img in req_image_list])
height = max([img.height for img in req_image_list])
width = sum(img.width for img in req_image_list)
height = max(img.height for img in req_image_list)
background = BuildImage(width, height)
await background.paste(req_image_list[0])
await req_image_list[1].line((0, 10, 1, req_image_list[1].height - 10), width=1)
Expand Down
18 changes: 9 additions & 9 deletions zhenxun/utils/platform.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import random
from collections.abc import Awaitable, Callable
from typing import Literal
from collections.abc import Callable, Awaitable

import httpx
import nonebot
from pydantic import BaseModel
from nonebot.adapters import Bot
from nonebot.utils import is_coroutine_callable
from nonebot.adapters.discord import Bot as DiscordBot
from nonebot.adapters.dodo import Bot as DodoBot
from nonebot.adapters.kaiheila import Bot as KaiheilaBot
from nonebot.adapters.onebot.v11 import Bot as v11Bot
from nonebot.adapters.onebot.v12 import Bot as v12Bot
from nonebot.adapters.discord import Bot as DiscordBot
from nonebot.adapters.kaiheila import Bot as KaiheilaBot
from nonebot_plugin_alconna.uniseg import Target, Receipt, UniMessage
from nonebot.utils import is_coroutine_callable
from nonebot_plugin_alconna.uniseg import Receipt, Target, UniMessage
from pydantic import BaseModel

from zhenxun.services.log import logger
from zhenxun.configs.config import BotConfig
from zhenxun.utils.message import MessageUtils
from zhenxun.models.friend_user import FriendUser
from zhenxun.utils.exception import NotFindSuperuser
from zhenxun.models.group_console import GroupConsole
from zhenxun.services.log import logger
from zhenxun.utils.exception import NotFindSuperuser
from zhenxun.utils.message import MessageUtils


class UserData(BaseModel):
Expand Down

0 comments on commit c36bb97

Please sign in to comment.