Skip to content

Commit

Permalink
fix: 修复多机器人情况下的请求处理
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdaYH committed Aug 12, 2024
1 parent 9001e79 commit 4a4c9bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
40 changes: 24 additions & 16 deletions migang/core/core_plugins/group_friend_handle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,17 @@ async def _(bot: Bot, event: FriendRequestEvent):
except ActionFailed:
logger.info(f"无法获取用户 {event.user_id} 的信息")
for user in bot.config.superusers:
await bot.send_private_msg(
user_id=int(user),
message=f"*****一份好友申请*****\n"
f"昵称:{user_name}({event.user_id})\n"
f"状态:{_handle_friend}\n"
f"日期:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
f"备注:{event.comment}",
)
try:
await bot.send_private_msg(
user_id=int(user),
message=f"*****一份好友申请*****\n"
f"昵称:{user_name}({event.user_id})\n"
f"状态:{_handle_friend}\n"
f"日期:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
f"备注:{event.comment}",
)
except ActionFailed:
logger.error(f"发送 {user} 好友 {event.user_id} 请求处理消息失败")
if _handle_friend == "同意":
try:
await bot.set_friend_add_request(flag=event.flag, approve=True)
Expand All @@ -187,6 +190,7 @@ async def _(bot: Bot, event: FriendRequestEvent):
logger.info(f"拒绝好友请求失败:{event.user_id}")
else:
await request_manager.add(
self_id=bot.self_id,
user_name=user_name,
user_id=event.user_id,
sex=sex,
Expand Down Expand Up @@ -235,14 +239,17 @@ async def _(bot: Bot, event: GroupRequestEvent):
except ActionFailed:
logger.info(f"无法获取群 {event.group_id} 的信息")
for user in bot.config.superusers:
await bot.send_private_msg(
user_id=int(user),
message=f"*****一份入群申请*****\n"
f"群聊:{group_name}({event.group_id})\n"
f"申请人:{user_name}({event.user_id})\n"
f"状态:{_handle_group}\n"
f"日期:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n",
)
try:
await bot.send_private_msg(
user_id=int(user),
message=f"*****一份入群申请*****\n"
f"群聊:{group_name}({event.group_id})\n"
f"申请人:{user_name}({event.user_id})\n"
f"状态:{_handle_group}\n"
f"日期:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n",
)
except ActionFailed:
logger.error(f"发送 {user} 入群 {event.group_id} 请求处理消息失败")
if _handle_group == "同意":
try:
await bot.set_group_add_request(
Expand All @@ -266,6 +273,7 @@ async def _(bot: Bot, event: GroupRequestEvent):
if hint := await get_config("group_request_hint"):
await bot.send_private_msg(user_id=event.user_id, message=Message(hint))
await request_manager.add(
self_id=bot.self_id,
user_name=user_name,
user_id=event.user_id,
sex=sex,
Expand Down
5 changes: 4 additions & 1 deletion migang/core/core_plugins/group_friend_handle/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ async def build_request_img(
)
info.paste(id_img, (400, 10), True)
time_img = text2image(
text=request.time.strftime("%Y-%m-%d %H:%M:%S"),
text="bot:"
+ request.self_id
+ "\n"
+ request.time.strftime("%Y-%m-%d %H:%M:%S"),
padding=(1, 1),
fill=(140, 140, 143),
fontname="HONOR Sans CN",
Expand Down
7 changes: 7 additions & 0 deletions migang/core/manager/request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class GroupRequest(BaseModel):
comment: str
flag: str
time: datetime
self_id: str


class FriendRequest(BaseModel):
Expand All @@ -30,6 +31,7 @@ class FriendRequest(BaseModel):
comment: str
flag: str
time: datetime
self_id: str


class Requests(BaseModel):
Expand All @@ -53,6 +55,7 @@ async def save(self) -> None:

async def add(
self,
self_id: str,
user_name: Optional[str],
user_id: int,
sex: Optional[str],
Expand Down Expand Up @@ -86,6 +89,7 @@ async def add(
comment=comment,
flag=flag,
time=time,
self_id=self_id,
)
)
else:
Expand All @@ -100,6 +104,7 @@ async def add(
comment=comment,
flag=flag,
time=time,
self_id=self_id,
)
)
await self.save()
Expand Down Expand Up @@ -162,6 +167,8 @@ async def __handle_request(
if id_ >= len(target):
return f"请输入 0~{len(target) - 1} 之间的id!"
request = target[id_]
if request.self_id is not None and request.self_id != bot.self_id:
return f"无法{'同意' if approve else '拒绝'}id为{id_}{'入群' if type_=='group' else '好友'}请求,该请求属于bot:{request.self_id}"
try:
if type_ == "group":
await bot.set_group_add_request(
Expand Down

0 comments on commit 4a4c9bc

Please sign in to comment.