Skip to content

Commit

Permalink
✨ support OnebotV11
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Nov 5, 2024
1 parent b37a1d4 commit a8c2b12
Show file tree
Hide file tree
Showing 26 changed files with 536 additions and 324 deletions.
37 changes: 36 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from typing import ClassVar, Generic, Literal, Optional, TypeVar, Union, cast

import yaml
from yarl import URL as _URL
from avilla.core import Selector
from avilla.core.account import BaseAccount
from avilla.core.elements import Notice
from avilla.onebot.v11.account import OneBot11Account
from avilla.onebot.v11.protocol import OneBot11ForwardConfig
from avilla.onebot.v11.protocol import OneBot11Protocol
from avilla.elizabeth.account import ElizabethAccount
from avilla.elizabeth.protocol import ElizabethConfig as _ElizabethConfig
from avilla.elizabeth.protocol import ElizabethProtocol
Expand Down Expand Up @@ -214,6 +218,37 @@ def administrators(self, channel: Optional[str] = None) -> list[Selector]:
def ensure(self, account: TA): ...


class OneBot11Config(BotConfig[OneBot11Account]):
type: Literal["onebot11"] = "onebot11"

host: str
"""onebot-v11 协议端的正向地址"""

port: int
"""onebot-v11 协议端的正向端口"""

access_token: str
"""onebot-v11 协议端的正向鉴权"""

def export(self):
return OneBot11Protocol, OneBot11ForwardConfig(
endpoint=_URL(f"ws://{self.host}:{self.port}"), access_token=self.access_token
)

def master(self, channel: Optional[str] = None) -> Selector:
if not channel:
return Selector.from_follows_pattern(f"land(qq).friend({self.master_id})")
return Selector.from_follows_pattern(f"land(qq).group({channel}).member({self.master_id})")

def administrators(self, channel: Optional[str] = None) -> list[Selector]:
if not channel:
return [Selector.from_follows_pattern(f"land(qq).friend({admin})") for admin in self.admins]
return [Selector.from_follows_pattern(f"land(qq).group({channel}).member({admin})") for admin in self.admins]

def ensure(self, account: OneBot11Account):
return int(self.account) in account.connection.accounts


class ElizabethConfig(BotConfig[ElizabethAccount]):
type: Literal["mirai"] = "mirai"

Expand Down Expand Up @@ -339,7 +374,7 @@ class RaianConfig(BaseConfig):
platform: PlatformConfig
"""外部平台接口相关配置"""

bots: list[Union[ElizabethConfig, QQAPIConfig]] = Field(default_factory=list)
bots: list[Union[ElizabethConfig, OneBot11Config, QQAPIConfig]] = Field(default_factory=list)
"""bot 配置"""

root: str = Field(default="config")
Expand Down
5 changes: 3 additions & 2 deletions app/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from avilla.core.elements import Notice, Text
from avilla.core.event import AvillaEvent
from avilla.elizabeth.account import ElizabethAccount
from avilla.onebot.v11.account import OneBot11Account
from avilla.qqapi.account import QQAPIAccount
from avilla.standard.core.message import MessageReceived
from avilla.standard.core.privilege import Privilege
Expand All @@ -23,7 +24,7 @@

def require_admin(only: bool = False, __record: Any = None):
async def __wrapper__(event: MessageReceived, serv: RaianBotService, bot: BotConfig, ctx: Context):
if not isinstance(ctx.account, ElizabethAccount):
if not isinstance(ctx.account, (ElizabethAccount, OneBot11Account)):
if ctx.scene.pattern.get("group"):
return True
if ctx.scene.pattern.get("friend"):
Expand Down Expand Up @@ -98,7 +99,7 @@ async def __wrapper__(event: AvillaEvent, serv: RaianBotService, ctx: Context):
await session.scalars(
select(Group)
.where(Group.id == ctx.scene.channel)
.where(Group.platform == ("qq" if isinstance(ctx.account, ElizabethAccount) else "qqapi"))
.where(Group.platform == ("qq" if isinstance(ctx.account, (ElizabethAccount, OneBot11Account)) else "qqapi"))
)
).one_or_none()
if not group:
Expand Down
4 changes: 4 additions & 0 deletions app/shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from avilla.core import Context
from avilla.core.account import BaseAccount
from avilla.core.elements import Picture
from avilla.onebot.v11.account import OneBot11Account
from avilla.elizabeth.resource import ElizabethImageResource
from avilla.onebot.v11.resource import OneBot11ImageResource
from avilla.qqapi.account import QQAPIAccount
from avilla.qqapi.resource import QQAPIImageResource
from graia.saya.factory import ensure_buffer
Expand All @@ -25,6 +27,8 @@ def is_qqapi_group(ctx: Context):
def picture(url: str, ctx: Context):
if isinstance(ctx.account, QQAPIAccount):
return Picture(QQAPIImageResource(ctx.scene.image(url), "image", url))
if isinstance(ctx.account, OneBot11Account):
return Picture(OneBot11ImageResource(ctx.scene.image(url), file="", url=url))
return Picture(ElizabethImageResource(ctx.scene.image(url), id="", url=url))


Expand Down
Loading

0 comments on commit a8c2b12

Please sign in to comment.