mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-02 02:00:01 +08:00
perf👌: 实现优化webui群组/好友管理api
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Optional, Union, overload
|
||||
|
||||
from nonebot.adapters.onebot.v11 import ActionFailed, Bot
|
||||
|
||||
@@ -67,14 +67,24 @@ class RequestManager(StaticData):
|
||||
}
|
||||
self.save()
|
||||
|
||||
@overload
|
||||
def remove_request(self, type_: str, flag: str):
|
||||
...
|
||||
|
||||
@overload
|
||||
def remove_request(self, type_: str, id_: int):
|
||||
...
|
||||
|
||||
def remove_request(self, type_: str, id_: Union[int, str]):
|
||||
"""
|
||||
删除一个请求数据
|
||||
:param type_: 类型
|
||||
:param id_: id,user_id 或 group_id
|
||||
"""
|
||||
for x in self._data[type_].keys():
|
||||
if self._data[type_][x].get("id") == id_:
|
||||
a_id = self._data[type_][x].get("id")
|
||||
a_flag = self._data[type_][x].get("flag")
|
||||
if a_id == id_ or a_flag == id_:
|
||||
del self._data[type_][x]
|
||||
break
|
||||
self.save()
|
||||
@@ -88,8 +98,17 @@ class RequestManager(StaticData):
|
||||
if data:
|
||||
return data["invite_group"]
|
||||
return None
|
||||
|
||||
|
||||
@overload
|
||||
async def approve(self, bot: Bot, id_: int, type_: str) -> int:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def approve(self, bot: Bot, flag: str, type_: str) -> int:
|
||||
...
|
||||
|
||||
|
||||
async def approve(self, bot: Bot, id_: Union[int, str], type_: str) -> int:
|
||||
"""
|
||||
同意请求
|
||||
:param bot: Bot
|
||||
@@ -97,8 +116,16 @@ class RequestManager(StaticData):
|
||||
:param type_: 类型,private 或 group
|
||||
"""
|
||||
return await self._set_add_request(bot, id_, type_, True)
|
||||
|
||||
@overload
|
||||
async def refused(self, bot: Bot, id_: int, type_: str) -> int:
|
||||
...
|
||||
|
||||
async def refused(self, bot: Bot, id_: int, type_: str) -> Optional[int]:
|
||||
@overload
|
||||
async def refused(self, bot: Bot, flag: str, type_: str) -> int:
|
||||
...
|
||||
|
||||
async def refused(self, bot: Bot, id_: Union[int, str], type_: str) -> Optional[int]:
|
||||
"""
|
||||
拒绝请求
|
||||
:param bot: Bot
|
||||
@@ -120,18 +147,32 @@ class RequestManager(StaticData):
|
||||
self._data = {"private": {}, "group": {}}
|
||||
self.save()
|
||||
|
||||
@overload
|
||||
async def delete_request(self, id_: int, type_: str) -> int:
|
||||
...
|
||||
|
||||
@overload
|
||||
async def delete_request(self, flag: str, type_: str) -> int:
|
||||
...
|
||||
|
||||
def delete_request(
|
||||
self, id_: int, type_: str
|
||||
self, id_: Union[str, int], type_: str
|
||||
): # type_: Literal["group", "private"]
|
||||
"""
|
||||
删除请求
|
||||
:param id_: id
|
||||
:param type_: 类型
|
||||
"""
|
||||
id_ = str(id_)
|
||||
if self._data[type_].get(id_):
|
||||
del self._data[type_][id_]
|
||||
self.save()
|
||||
if type(id_) == int:
|
||||
if self._data[type_].get(id_):
|
||||
del self._data[type_][id_]
|
||||
self.save()
|
||||
else:
|
||||
for k, item in self._data[type_].items():
|
||||
if item['flag'] == id_:
|
||||
del self._data[type_][k]
|
||||
self.save()
|
||||
break
|
||||
|
||||
def set_group_name(self, group_name: str, group_id: int):
|
||||
"""
|
||||
@@ -239,7 +280,7 @@ class RequestManager(StaticData):
|
||||
return bk.pic2bs4()
|
||||
|
||||
async def _set_add_request(
|
||||
self, bot: Bot, idx: int, type_: str, approve: bool
|
||||
self, bot: Bot, idx: Union[str, int], type_: str, approve: bool
|
||||
) -> int:
|
||||
"""
|
||||
处理请求
|
||||
@@ -248,8 +289,13 @@ class RequestManager(StaticData):
|
||||
:param type_: 类型,private 或 group
|
||||
:param approve: 是否同意
|
||||
"""
|
||||
id_ = str(idx)
|
||||
if id_ in self._data[type_].keys():
|
||||
flag = None
|
||||
id_ = None
|
||||
if type(idx) == str:
|
||||
flag = idx
|
||||
else:
|
||||
id_ = str(idx)
|
||||
if id_ and id_ in self._data[type_].keys():
|
||||
try:
|
||||
if type_ == "private":
|
||||
await bot.set_friend_add_request(
|
||||
@@ -277,4 +323,25 @@ class RequestManager(StaticData):
|
||||
del self._data[type_][id_]
|
||||
self.save()
|
||||
return rid
|
||||
if flag:
|
||||
rm_id = None
|
||||
for k, item in self._data[type_].items():
|
||||
if item['flag'] == flag:
|
||||
rm_id = k
|
||||
if type_ == 'private':
|
||||
await bot.set_friend_add_request(
|
||||
flag=item['flag'], approve=approve
|
||||
)
|
||||
rid = item["id"]
|
||||
else:
|
||||
await bot.set_group_add_request(
|
||||
flag=item['flag'],
|
||||
sub_type="invite",
|
||||
approve=approve,
|
||||
)
|
||||
rid = item["invite_group"]
|
||||
if rm_id is not None:
|
||||
del self._data[type_][rm_id]
|
||||
self.save()
|
||||
return rid
|
||||
return 2 # 未找到id
|
||||
|
||||
Reference in New Issue
Block a user