mirror of
https://github.com/xfgryujk/blivechat.git
synced 2026-08-19 09:43:28 +08:00
发送房间消息给插件
This commit is contained in:
@@ -28,22 +28,11 @@ def _make_msg_callback(method_name, message_cls):
|
||||
def callback(self: 'BaseHandler', client: cli.BlcPluginClient, command: dict):
|
||||
method = getattr(self, method_name)
|
||||
msg = message_cls.from_command(command['data'])
|
||||
extra = _get_extra(command)
|
||||
extra = models.ExtraData.from_dict(command.get('extra', {}))
|
||||
return method(client, msg, extra)
|
||||
return callback
|
||||
|
||||
|
||||
def _get_extra(command: dict):
|
||||
extra = command.get('extra', {})
|
||||
room_key_dict = extra.get('roomKey', None)
|
||||
if room_key_dict is not None:
|
||||
extra['roomKey'] = models.RoomKey(
|
||||
type=models.RoomKeyType(room_key_dict['type']),
|
||||
value=room_key_dict['value'],
|
||||
)
|
||||
return extra
|
||||
|
||||
|
||||
class BaseHandler(HandlerInterface):
|
||||
"""一个简单的消息处理器实现,带消息分发和消息类型转换。继承并重写_on_xxx方法即可实现自己的处理器"""
|
||||
|
||||
@@ -54,17 +43,14 @@ class BaseHandler(HandlerInterface):
|
||||
Any
|
||||
]]
|
||||
] = {
|
||||
# 收到弹幕
|
||||
models.Command.ADD_ROOM: _make_msg_callback('_on_add_room', models.AddRoomMsg),
|
||||
models.Command.ROOM_INIT: _make_msg_callback('_on_room_init', models.RoomInitMsg),
|
||||
models.Command.DEL_ROOM: _make_msg_callback('_on_del_room', models.DelRoomMsg),
|
||||
models.Command.ADD_TEXT: _make_msg_callback('_on_add_text', models.AddTextMsg),
|
||||
# 有人送礼
|
||||
models.Command.ADD_GIFT: _make_msg_callback('_on_add_gift', models.AddGiftMsg),
|
||||
# 有人上舰
|
||||
models.Command.ADD_MEMBER: _make_msg_callback('_on_add_member', models.AddMemberMsg),
|
||||
# 醒目留言
|
||||
models.Command.ADD_SUPER_CHAT: _make_msg_callback('_on_add_super_chat', models.AddSuperChatMsg),
|
||||
# 删除醒目留言
|
||||
models.Command.DEL_SUPER_CHAT: _make_msg_callback('_on_del_super_chat', models.DelSuperChatMsg),
|
||||
# 更新翻译
|
||||
models.Command.UPDATE_TRANSLATION: _make_msg_callback('_on_update_translation', models.UpdateTranslationMsg),
|
||||
}
|
||||
"""cmd -> 处理回调"""
|
||||
@@ -75,20 +61,31 @@ class BaseHandler(HandlerInterface):
|
||||
if callback is not None:
|
||||
callback(self, client, command)
|
||||
|
||||
def _on_add_text(self, client: cli.BlcPluginClient, message: models.AddTextMsg):
|
||||
def _on_add_room(self, client: cli.BlcPluginClient, message: models.AddRoomMsg, extra: models.ExtraData):
|
||||
"""添加房间"""
|
||||
|
||||
def _on_room_init(self, client: cli.BlcPluginClient, message: models.RoomInitMsg, extra: models.ExtraData):
|
||||
"""房间初始化"""
|
||||
|
||||
def _on_del_room(self, client: cli.BlcPluginClient, message: models.DelRoomMsg, extra: models.ExtraData):
|
||||
"""删除房间"""
|
||||
|
||||
def _on_add_text(self, client: cli.BlcPluginClient, message: models.AddTextMsg, extra: models.ExtraData):
|
||||
"""收到弹幕"""
|
||||
|
||||
def _on_add_gift(self, client: cli.BlcPluginClient, message: models.AddGiftMsg):
|
||||
def _on_add_gift(self, client: cli.BlcPluginClient, message: models.AddGiftMsg, extra: models.ExtraData):
|
||||
"""有人送礼"""
|
||||
|
||||
def _on_add_member(self, client: cli.BlcPluginClient, message: models.AddMemberMsg):
|
||||
def _on_add_member(self, client: cli.BlcPluginClient, message: models.AddMemberMsg, extra: models.ExtraData):
|
||||
"""有人上舰"""
|
||||
|
||||
def _on_add_super_chat(self, client: cli.BlcPluginClient, message: models.AddSuperChatMsg):
|
||||
def _on_add_super_chat(self, client: cli.BlcPluginClient, message: models.AddSuperChatMsg, extra: models.ExtraData):
|
||||
"""醒目留言"""
|
||||
|
||||
def _on_del_super_chat(self, client: cli.BlcPluginClient, message: models.DelSuperChatMsg):
|
||||
def _on_del_super_chat(self, client: cli.BlcPluginClient, message: models.DelSuperChatMsg, extra: models.ExtraData):
|
||||
"""删除醒目留言"""
|
||||
|
||||
def _on_update_translation(self, client: cli.BlcPluginClient, message: models.UpdateTranslationMsg):
|
||||
def _on_update_translation(
|
||||
self, client: cli.BlcPluginClient, message: models.UpdateTranslationMsg, extra: models.ExtraData
|
||||
):
|
||||
"""更新翻译"""
|
||||
|
||||
@@ -41,6 +41,9 @@ class RoomKey(NamedTuple):
|
||||
class Command(enum.IntEnum):
|
||||
HEARTBEAT = 0
|
||||
BLC_INIT = 1
|
||||
ADD_ROOM = 2
|
||||
ROOM_INIT = 3
|
||||
DEL_ROOM = 4
|
||||
|
||||
ADD_TEXT = 20
|
||||
ADD_GIFT = 21
|
||||
@@ -50,6 +53,78 @@ class Command(enum.IntEnum):
|
||||
UPDATE_TRANSLATION = 25
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class ExtraData:
|
||||
"""一些消息共用的附加信息"""
|
||||
|
||||
room_id: Optional[int] = None
|
||||
"""房间ID"""
|
||||
room_key: Optional[RoomKey] = None
|
||||
"""blivechat用来标识一个房间的key"""
|
||||
is_from_plugin: bool = False
|
||||
"""消息是插件生成的"""
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict):
|
||||
room_key_dict = data.get('roomKey', None)
|
||||
if room_key_dict is not None:
|
||||
room_key = RoomKey(
|
||||
type=RoomKeyType(room_key_dict['type']),
|
||||
value=room_key_dict['value'],
|
||||
)
|
||||
else:
|
||||
room_key = None
|
||||
|
||||
return cls(
|
||||
room_id=data.get('roomId', None),
|
||||
room_key=room_key,
|
||||
is_from_plugin=data.get('isFromPlugin', False),
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class AddRoomMsg:
|
||||
"""
|
||||
添加房间消息。房间信息在extra里
|
||||
|
||||
此时room_id是None,因为还没有初始化
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def from_command(cls, _data: dict):
|
||||
return cls()
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class RoomInitMsg:
|
||||
"""
|
||||
房间初始化消息。房间信息在extra里
|
||||
|
||||
一个房间创建后可能被多次初始化,处理时注意去重
|
||||
"""
|
||||
|
||||
is_success: bool = False
|
||||
|
||||
@classmethod
|
||||
def from_command(cls, data: dict):
|
||||
return cls(
|
||||
is_success=data['isSuccess'],
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class DelRoomMsg:
|
||||
"""
|
||||
删除房间消息。房间信息在extra里
|
||||
|
||||
注意此时room_id可能是None
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def from_command(cls, _data: dict):
|
||||
return cls()
|
||||
|
||||
|
||||
class AuthorType(enum.IntEnum):
|
||||
NORMAL = 0
|
||||
GUARD = 1
|
||||
|
||||
Reference in New Issue
Block a user