Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed May 1, 2024
1 parent 65fd79b commit a5866f3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 164 deletions.
2 changes: 1 addition & 1 deletion nonebot_plugin_multincm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

auto_resolve_tip = "▶ Bot 会自动解析你发送的网易云链接\n"

__version__ = "0.5.0.dev9"
__version__ = "0.5.0.dev10"
__plugin_meta__ = PluginMetadata(
name="MultiNCM",
description="网易云多选点歌",
Expand Down
21 changes: 14 additions & 7 deletions nonebot_plugin_multincm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from pathlib import Path
from typing import Dict, List, NoReturn, Optional, Type, Union, cast
from nonebot_plugin_multincm.utils import logged_suppress
from typing_extensions import Annotated

from httpx import AsyncClient
Expand Down Expand Up @@ -115,24 +116,30 @@ async def cache_song(song: BaseSong, session: Optional[str] = None):
chat_last_song_cache.set(session, SongCache(type(song), song.song_id))


async def send_song(song: BaseSong):
async def send_song(song: BaseSong, use_text: bool = False):
await cache_song(song)

bot = cast(Bot, current_bot.get())
matcher = current_matcher.get()
msg_type = "text" if use_text else "card"

try:
msg = await song.to_card_message(int(bot.self_id))
msg = await song.to_text_message() if use_text else await song.to_card_message()
except Exception:
logger.exception(f"Generate {song.calling} card failed")
await matcher.finish(f"生成{song.calling}卡片失败,请检查后台输出")
logger.exception(f"Generate {song.calling} {msg_type} message failed")
if use_text:
await matcher.finish(f"获取{song.calling}信息失败,请检查后台输出")
else:
await send_song(song, use_text=True)
return

try:
await matcher.send(msg)
except ActionFailed:
logger.warning(f"Send {song.calling} card failed")
logger.warning(f"Send {song.calling} failed")
logger.opt(exception=True).debug("Stacktrace")
await matcher.send(f"发送卡片失败\n{await song.get_url()}")
if not use_text:
await send_song(song, use_text=True)
return


async def get_class_from_link_type(type_name: str) -> Type[SongOrPlaylist]:
Expand Down
89 changes: 0 additions & 89 deletions nonebot_plugin_multincm/card_helper.py

This file was deleted.

58 changes: 27 additions & 31 deletions nonebot_plugin_multincm/providers/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import json
from abc import ABC, abstractmethod
from contextlib import suppress
from typing import (
Expand All @@ -18,7 +17,6 @@

from nonebot.adapters.onebot.v11 import Message, MessageSegment

from ..card_helper import construct_music_card
from ..config import config
from ..draw import TablePage
from ..types import PlaylistRespModelType, SearchRespModelType, SongInfoModelType
Expand Down Expand Up @@ -76,7 +74,7 @@ async def get_cover_url(self) -> str: ...
@abstractmethod
async def get_lyric(self) -> Optional[str]: ...

async def to_card_message(self, uin: int) -> Message:
async def to_card_message(self) -> Message:
url, playable_url, name, artists, cover_url = await asyncio.gather(
self.get_url(),
self.get_playable_url(),
Expand All @@ -85,37 +83,35 @@ async def to_card_message(self, uin: int) -> Message:
self.get_cover_url(),
)
content = "、".join(artists)
seg = (
MessageSegment(
"json",
{
"data": await construct_music_card(
uin=uin,
desc=content,
jump_url=url,
music_url=playable_url,
preview=cover_url,
title=name,
),
},
)
if config.ncm_use_json_segment
else MessageSegment(
"music",
{
"type": "custom",
"subtype": "163",
"url": url,
"jumpUrl": url, # icqq
"voice": playable_url,
"title": name,
"content": content,
"image": cover_url,
},
)
seg = MessageSegment(
"music",
{
"type": "custom",
"url": url,
"audio": playable_url,
"title": name,
"content": content,
"image": cover_url,
"subtype": "163", # gocq
"voice": playable_url, # gocq
"jumpUrl": url, # icqq
},
)
return Message(seg)

async def _to_text_message(self) -> Message:
name, artists, cover_url = await asyncio.gather(
self.get_name(),
self.get_artists(),
self.get_cover_url(),
)
content = "、".join(artists)
return MessageSegment.image(cover_url) + f"{name}\nBy: {content}"

async def to_text_message(self) -> Message:
msg, url = await asyncio.gather(self._to_text_message(), self.get_url())
return msg + f"\n{url}\n使用指令 `direct` 获取播放链接"


class BasePlaylist(
ABC,
Expand Down
36 changes: 0 additions & 36 deletions nonebot_plugin_multincm/res/card_template.json

This file was deleted.

12 changes: 12 additions & 0 deletions nonebot_plugin_multincm/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from contextlib import contextmanager
from typing import List, Optional, TypeVar, cast
from typing_extensions import ParamSpec

from nonebot import logger

from . import lrc_parser
from .config import config
from .types import Artist, Lyric, LyricData, User
Expand Down Expand Up @@ -70,3 +73,12 @@ def fmt_usr(usr: User) -> str:
lines.append(f"翻译贡献者:{fmt_usr(usr)}")

return "\n".join(lines).strip()


@contextmanager
def logged_suppress(msg: str):
try:
yield
except Exception:
logger.exception(msg)
return None

0 comments on commit a5866f3

Please sign in to comment.