Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 support Satori 0.12 #53

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dev = [
"nonebot-adapter-onebot>=2.4.3",
"nonebot-adapter-qq>=1.4.4",
"nonebot-adapter-red>=0.9.0",
"nonebot-adapter-satori>=0.11.4",
"nonebot-adapter-satori>=0.12.0",
"nonebot-adapter-telegram>=0.1.0b17",
"nonebot-adapter-kritor>=0.1.1",
"nonebot-adapter-tailchat>=0.1.0b7",
Expand Down
5 changes: 2 additions & 3 deletions src/nonebot_plugin_alconna/uniseg/adapters/satori/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ def get_satori_version():


class Loader(BaseLoader):

def __init__(self):
if version := get_satori_version():
if tuple(map(int, version.split(".")[:2])) < (0, 11):
raise ImportError("nonebot-adapter-satori>=0.11 is required.")
if tuple(map(int, version.split(".")[:2])) < (0, 12):
raise ImportError("nonebot-adapter-satori>=0.12 is required.")

def get_adapter(self) -> SupportAdapter:
return SupportAdapter.satori
Expand Down
8 changes: 4 additions & 4 deletions src/nonebot_plugin_alconna/uniseg/adapters/satori/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nonebot.adapters.satori.message import STYLE_TYPE_MAP
from nonebot.adapters.satori.event import NoticeEvent, MessageEvent
from nonebot.adapters.satori.message import Message, MessageSegment
from nonebot.adapters.satori.models import ChannelType, InnerMessage
from nonebot.adapters.satori.models import ChannelType, MessageObject

from nonebot_plugin_alconna.uniseg.target import Target
from nonebot_plugin_alconna.uniseg.constraint import SupportScope
Expand Down Expand Up @@ -171,7 +171,7 @@ async def send_to(self, target: Union[Target, Event], bot: Bot, message: Message

async def recall(self, mid: Any, bot: Bot, context: Union[Target, Event]):
assert isinstance(bot, SatoriBot)
_mid: InnerMessage = cast(InnerMessage, mid)
_mid: MessageObject = cast(MessageObject, mid)
if isinstance(context, Target):
if context.private:
channel = await bot.user_channel_create(user_id=context.id)
Expand All @@ -190,7 +190,7 @@ async def edit(self, new: Message, mid: Any, bot: Bot, context: Union[Target, Ev
if TYPE_CHECKING:
assert isinstance(new, self.get_message_type())

_mid: InnerMessage = cast(InnerMessage, mid)
_mid: MessageObject = cast(MessageObject, mid)
if isinstance(context, Target):
if context.private:
channel = await bot.user_channel_create(user_id=context.id)
Expand All @@ -202,5 +202,5 @@ async def edit(self, new: Message, mid: Any, bot: Bot, context: Union[Target, Ev
return await bot.update_message(channel.id, _mid.id, new)

def get_reply(self, mid: Any):
_mid: InnerMessage = cast(InnerMessage, mid)
_mid: MessageObject = cast(MessageObject, mid)
return Reply(_mid.id)
8 changes: 6 additions & 2 deletions tests/fake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from datetime import datetime
from typing import TYPE_CHECKING, Literal

from nonebot.adapters.satori.models import Login, LoginStatus

FAKE_SATORI_LOGIN = Login(status=LoginStatus.ONLINE)

if TYPE_CHECKING:
from nonebot.adapters.qq import MessageCreateEvent as MessageCreateEvent
from nonebot.adapters.satori.event import MessageEvent as SatoriMessageEvent
Expand Down Expand Up @@ -89,7 +93,7 @@ def fake_message_event_satori(**field) -> "SatoriMessageEvent":
from pydantic import create_model
from nonebot.adapters.satori import Message
from nonebot.adapters.satori.event import MessageEvent
from nonebot.adapters.satori.models import User, Channel, ChannelType, InnerMessage
from nonebot.adapters.satori.models import User, Channel, ChannelType, MessageObject

_Fake = create_model("_Fake", __base__=MessageEvent)

Expand All @@ -101,7 +105,7 @@ class FakeEvent(_Fake):
timestamp: datetime = datetime.fromtimestamp(1000000)
channel: Channel = Channel(id="1", type=ChannelType.TEXT)
user: User = User(id="1", name="test")
message: InnerMessage = InnerMessage(id="1", content="text")
message: MessageObject = MessageObject(id="1", content="text")
to_me: bool = False

class Config:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_builtin_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from nonebot import get_adapter
from nonebot.adapters.satori import Bot, Adapter, Message, MessageSegment

from tests.fake import fake_message_event_satori
from tests.fake import FAKE_SATORI_LOGIN, fake_message_event_satori


@pytest.mark.asyncio()
Expand All @@ -14,7 +14,7 @@ async def test_echo(app: App):

async with app.test_matcher() as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = "/echo" + MessageSegment.image(raw=b"123", mime="image/png")
event = fake_message_event_satori(message=msg, id=123)
ctx.receive_event(bot, event)
Expand Down Expand Up @@ -42,7 +42,7 @@ async def tt_h():

async with app.test_matcher() as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = Message("/help")
event = fake_message_event_satori(message=msg, id=123)
ctx.receive_event(bot, event)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nonebot.adapters.satori import Bot, Message
from arclet.alconna import Args, Alconna, CommandMeta

from tests.fake import fake_message_event_satori
from tests.fake import FAKE_SATORI_LOGIN, fake_message_event_satori


@pytest.mark.asyncio()
Expand All @@ -20,7 +20,7 @@ async def tt_h(userid: str, ctx: dict):

async with app.test_matcher(test_cmd) as ctx:
# adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, login=FAKE_SATORI_LOGIN, info=None)
msg = Message("test $(event.get_user_id())")
event = fake_message_event_satori(message=msg, id=123, user=User(id="456", name="test"))
ctx.receive_event(bot, event)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_filehost_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from arclet.alconna import Alconna
from nonebot.adapters.satori import Bot, Adapter, Message, MessageSegment

from tests.fake import fake_message_event_satori
from tests.fake import FAKE_SATORI_LOGIN, fake_message_event_satori


@pytest.mark.asyncio()
Expand All @@ -20,7 +20,7 @@ async def tt_h():
apply_filehost()
async with app.test_matcher(test_cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = Message("test")
event = fake_message_event_satori(message=msg, id=123)
ctx.receive_event(bot, event)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_saa_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nonebot import require, get_adapter
from nonebot.adapters.satori import Bot, Adapter, MessageSegment

from tests.fake import fake_message_event_satori
from tests.fake import FAKE_SATORI_LOGIN, fake_message_event_satori


@pytest.mark.asyncio()
Expand All @@ -29,7 +29,7 @@ async def tt_h(target: At):

async with app.test_matcher(test_cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = "test" + MessageSegment.at("234")
event = fake_message_event_satori(message=msg, id=123)
ctx.receive_event(bot, event)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nonebot.adapters.satori.element import parse
from nonebot.adapters.satori import Bot, Adapter, Message, MessageSegment

from tests.fake import fake_message_event_satori
from tests.fake import FAKE_SATORI_LOGIN, fake_message_event_satori


def test_message_rollback():
Expand Down Expand Up @@ -82,7 +82,7 @@ async def tt_h(img: Image):

async with app.test_matcher(test_cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter, platform="satori", info=None)
bot = ctx.create_bot(base=Bot, adapter=adapter, login=FAKE_SATORI_LOGIN, info=None)
msg = "test" + MessageSegment.image(raw=b"123", mime="image/png")
event = fake_message_event_satori(message=msg, id=123)
ctx.receive_event(bot, event)
Expand Down
15 changes: 11 additions & 4 deletions tests/test_target_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from nonebot.adapters.onebot.v12 import Adapter as Onebot12Adapter
from nonebot.adapters.satori.models import User, Guild, Channel, PageResult, ChannelType

from tests.fake import FAKE_SATORI_LOGIN

FAKE_SATORI_CHRONOCAT_LOGIN = FAKE_SATORI_LOGIN.model_copy(update={"platform": "chronocat"})
FAKE_SATORI_QQ_LOGIN = FAKE_SATORI_LOGIN.model_copy(update={"platform": "qq"})


@pytest.mark.asyncio()
async def test_bots(app: App):
Expand All @@ -25,9 +30,11 @@ async def test_bots(app: App):
qq_bot = ctx.create_bot(base=QQBot, adapter=qq_adapter, self_id="1", bot_info=None)
satori_adapter = get_adapter(SatoriAdapter)
satori_bot_cc = ctx.create_bot(
base=SatoriBot, adapter=satori_adapter, self_id="2", platform="chronocat", info=None
base=SatoriBot, adapter=satori_adapter, self_id="2", login=FAKE_SATORI_CHRONOCAT_LOGIN, info=None
)
satori_bot_qq = ctx.create_bot(
base=SatoriBot, adapter=satori_adapter, self_id="3", login=FAKE_SATORI_QQ_LOGIN, info=None
)
satori_bot_qq = ctx.create_bot(base=SatoriBot, adapter=satori_adapter, self_id="3", platform="qq", info=None)
onebot11_adapter = get_adapter(Onebot11Adapter)
onebot11_bot = ctx.create_bot(base=Onebot11Bot, adapter=onebot11_adapter, self_id="4")
onebot12_adapter = get_adapter(Onebot12Adapter)
Expand Down Expand Up @@ -70,7 +77,7 @@ async def test_enable(app: App, mocker: MockerFixture):
async with app.test_api() as ctx:
satori_adapter = get_adapter(SatoriAdapter)
satori_bot1 = ctx.create_bot(
base=SatoriBot, adapter=satori_adapter, self_id="1", platform="chronocat", info=None
base=SatoriBot, adapter=satori_adapter, self_id="1", login=FAKE_SATORI_CHRONOCAT_LOGIN, info=None
)

ctx.should_call_api("friend_list", {}, PageResult(data=[User(id="11", name="test1")]))
Expand All @@ -81,7 +88,7 @@ async def test_enable(app: App, mocker: MockerFixture):
)
await asyncio.sleep(0.1)
satori_bot2 = ctx.create_bot(
base=SatoriBot, adapter=satori_adapter, self_id="2", platform="chronocat", info=None
base=SatoriBot, adapter=satori_adapter, self_id="2", login=FAKE_SATORI_CHRONOCAT_LOGIN, info=None
)
ctx.should_call_api("friend_list", {}, PageResult(data=[User(id="21", name="test1")]))

Expand Down