Skip to content

Commit

Permalink
支持多机器人
Browse files Browse the repository at this point in the history
  • Loading branch information
kitUIN committed Aug 10, 2023
1 parent a149761 commit 929d420
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
11 changes: 6 additions & 5 deletions nonebot-plugin-ncm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ async def music_receive(bot: Bot, event: Union[GroupMessageEvent, PrivateMessage
regroup: Tuple[Any, ...] = RegexGroup()):
nid = regroup[1]
logger.info(f"已识别NID:{nid}的歌曲")
nncm.get_song(nid=nid, message_id=event.message_id)

nncm.get_song(nid=nid, message_id=event.message_id, bot_id=bot.self_id)


@playlist_regex.handle()
Expand All @@ -156,7 +157,7 @@ async def music_reply_receive(bot: Bot, event: Union[GroupMessageEvent, PrivateM
info = nncm.check_message(int(event.dict()["reply"]["message_id"]))
if info is None:
return
if info["type"] == "song" and await song_is_open(event):
if info["type"] == "song" and await song_is_open(event) and info["bot_id"] == bot.self_id:
await bot.send(event=event, message="少女祈祷中🙏...上传时间较久,请勿重复发送命令")
data = await nncm.music_check(info["nid"])
if isinstance(data, list):
Expand All @@ -170,17 +171,17 @@ async def music_reply_receive(bot: Bot, event: Union[GroupMessageEvent, PrivateM
logger.error("数据库中未有该音乐地址数据")
await bot.send(event=event, message="数据库中未有该音乐地址数据")

elif info["type"] == "playlist" and await playlist_is_open(event):
elif info["type"] == "playlist" and await playlist_is_open(event) and info["bot_id"] == bot.self_id:
await bot.send(event=event, message=info["lmsg"] + "\n下载中,上传时间较久,请勿重复发送命令")
not_zips = await nncm.download(ids=info["ids"], lid=info["lid"], is_zip=ncm_config.ncm_playlist_zip)
filename = f"{info['lid']}.zip"
data = Path.cwd().joinpath("music").joinpath(filename)
if ncm_config.ncm_playlist_zip:
logger.debug(f"Upload:{filename}")
if isinstance(event, GroupMessageEvent):
await nncm.upload_group_file(group_id=event.group_id, file=str(data), name=filename)
await nncm.upload_group_file(group_id=event.group_id, file=str(data), name=filename,bot_id=info["bot_id"])
elif isinstance(event, PrivateMessageEvent):
await nncm.upload_private_file(user_id=event.user_id, file=str(data), name=filename)
await nncm.upload_private_file(user_id=event.user_id, file=str(data), name=filename,bot_id=info["bot_id"])
else:
for i in not_zips:
file = i["file"]
Expand Down
22 changes: 12 additions & 10 deletions nonebot-plugin-ncm/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def check_message(message_id: int):
"""检查缓存中是否存在解析
:return:
"""
info = ncm_check_cache.search(Q.message_id == message_id)
return info[0] if info else None
flag = ncm_check_cache.search(Q.message_id == message_id)
return flag[0] if flag else None

@staticmethod
def get_song(nid: int, message_id: int):
def get_song(nid: int, message_id: int, bot_id: str):
"""解析歌曲id,并且加入缓存
:param message_id:
Expand All @@ -178,9 +178,10 @@ def get_song(nid: int, message_id: int):
"lid": 0,
"ids": [],
"lmsg": "",
"bot_id": bot_id,
"time": int(time.time())})

def get_playlist(self, lid: int, message_id: int):
def get_playlist(self, lid: int, message_id: int, bot_id: str):
lid = int(lid)
data = self.api.playlist.GetPlaylistInfo(lid)
# logger.info(data)
Expand All @@ -196,6 +197,7 @@ def get_playlist(self, lid: int, message_id: int):
"lmsg": f"歌单:{raw['name']}\r\n创建者:{raw['creator']['nickname']}\r\n歌曲总数:{raw['trackCount']}\r\n"
f"标签:{tags}\r\n播放次数:{raw['playCount']}\r\n收藏:{raw['subscribedCount']}\r\n"
f"评论:{raw['commentCount']}\r\n分享:{raw['shareCount']}\r\nListID:{lid}",
"bot_id": bot_id,
"time": int(time.time())})

async def upload_group_data_file(self, group_id: int, data: Dict[str, Union[str, int]]):
Expand All @@ -205,13 +207,13 @@ async def upload_private_data_file(self, user_id: int, data: Dict[str, Union[str
await self.upload_private_file(user_id=user_id, file=data["file"], name=data["filename"])

@staticmethod
async def upload_group_file(group_id: int, file: str, name: str):
async def upload_group_file(group_id: int, file: str, name: str, bot_id: str):
try:
bot: Bot = nonebot.get_bot()
bot: Bot = nonebot.get_bot(bot_id)
await bot.upload_group_file(group_id=group_id, file=file, name=name)
except (ActionFailed, NetworkError) as e:
logger.error(e)
bot: Bot = nonebot.get_bot()
bot: Bot = nonebot.get_bot(bot_id)
if isinstance(e, ActionFailed) and e.info["wording"] == "server" \
" requires unsupported ftn upload":
await bot.send_group_msg(group_id=group_id, message=Message(MessageSegment.text(
Expand All @@ -223,14 +225,14 @@ async def upload_group_file(group_id: int, file: str, name: str):
"上传超时(一般来说还在传,建议等待五分钟)")))

@staticmethod
async def upload_private_file(user_id: int, file: str, name: str):
async def upload_private_file(user_id: int, file: str, name: str, bot_id: str):
try:
bot: Bot = nonebot.get_bot()
bot: Bot = nonebot.get_bot(bot_id)
await bot.upload_private_file(user_id=user_id, file=file, name=name)
except (ActionFailed, NetworkError) as e:
logger.error(e)
if isinstance(e, NetworkError):
bot: Bot = nonebot.get_bot()
bot: Bot = nonebot.get_bot(bot_id)
await bot.send_private_msg(user_id=user_id, message=Message(MessageSegment.text(
"[ERROR] 文件上传失败\r\n[原因] 上传超时(一般来说还在传,建议等待五分钟)")))

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-ncm"
version = "1.6.2"
version = "1.6.3"
description = "基于go-cqhttp与nonebot2的 网易云 无损音乐下载"
license = "Apache License 2.0"
authors = ["kitUIN <[email protected]>"]
Expand Down

0 comments on commit 929d420

Please sign in to comment.