完善插件消息

This commit is contained in:
John Smith 2024-01-15 01:15:31 +08:00
parent be5b49fae4
commit 263bf80546
5 changed files with 124 additions and 27 deletions

View File

@ -68,7 +68,8 @@ def make_text_message_data(
translation: str = '',
content_type: int = ContentType.TEXT,
content_type_params: list = None,
uid: int = 0
uid: int = 0,
medal_name: str = '',
):
# 为了节省带宽用list而不是dict
return [
@ -105,7 +106,9 @@ def make_text_message_data(
# 15: textEmoticons
[], # 已废弃,保留
# 16: uid
uid
uid,
# 17: medalName
medal_name,
]

View File

@ -217,6 +217,10 @@ class AddTextMsg:
"""内容类型见ContentType"""
content_type_params: Union[dict, list] = dataclasses.field(default_factory=dict)
"""跟内容类型相关的参数"""
uid: int = 0
"""用户ID"""
medal_name: str = ''
"""勋章名"""
@classmethod
def from_command(cls, data: list):
@ -241,6 +245,8 @@ class AddTextMsg:
translation=data[12],
content_type=content_type,
content_type_params=content_type_params,
uid=data[16],
medal_name=data[17],
)
@ -257,11 +263,25 @@ class AddGiftMsg:
author_name: str = ''
"""用户名"""
total_coin: int = 0
"""总价瓜子数1000金瓜子 = 1元"""
"""总价付费瓜子数1000金瓜子 = 1元"""
total_free_coin: int = 0
"""总价免费瓜子数"""
gift_name: str = ''
"""礼物名"""
num: int = 0
"""数量"""
gift_id: int = 0
"""礼物ID"""
gift_icon_url: str = ''
"""礼物图标URL"""
uid: int = 0
"""用户ID"""
privilege_type: int = GuardLevel.NONE.value
"""舰队等级见GuardLevel"""
medal_level: int = 0
"""勋章等级如果没戴当前房间勋章则为0"""
medal_name: str = ''
"""勋章名"""
@classmethod
def from_command(cls, data: dict):
@ -271,8 +291,15 @@ class AddGiftMsg:
timestamp=data['timestamp'],
author_name=data['authorName'],
total_coin=data['totalCoin'],
total_free_coin=data['totalFreeCoin'],
gift_name=data['giftName'],
num=data['num'],
gift_id=data['giftId'],
gift_icon_url=data['giftIconUrl'],
uid=data['uid'],
privilege_type=data['privilegeType'],
medal_level=data['medalLevel'],
medal_name=data['medalName'],
)
@ -290,6 +317,16 @@ class AddMemberMsg:
"""用户名"""
privilege_type: int = GuardLevel.NONE.value
"""舰队等级见GuardLevel"""
num: int = 0
"""数量"""
unit: str = ''
"""单位(月)"""
uid: int = 0
"""用户ID"""
medal_level: int = 0
"""勋章等级如果没戴当前房间勋章则为0"""
medal_name: str = ''
"""勋章名"""
@classmethod
def from_command(cls, data: dict):
@ -299,6 +336,11 @@ class AddMemberMsg:
timestamp=data['timestamp'],
author_name=data['authorName'],
privilege_type=data['privilegeType'],
num=data['num'],
unit=data['unit'],
uid=data['uid'],
medal_level=data['medalLevel'],
medal_name=data['medalName'],
)
@ -320,6 +362,14 @@ class AddSuperChatMsg:
"""内容"""
translation: str = ''
"""内容翻译"""
uid: int = 0
"""用户ID"""
privilege_type: int = GuardLevel.NONE.value
"""舰队等级见GuardLevel"""
medal_level: int = 0
"""勋章等级如果没戴当前房间勋章则为0"""
medal_name: str = ''
"""勋章名"""
@classmethod
def from_command(cls, data: dict):
@ -331,6 +381,10 @@ class AddSuperChatMsg:
price=data['price'],
content=data['content'],
translation=data['translation'],
uid=data['uid'],
privilege_type=data['privilegeType'],
medal_level=data['medalLevel'],
medal_name=data['medalName'],
)

View File

@ -144,6 +144,10 @@ export default class ChatClientRelay {
break
}
case COMMAND_ADD_GIFT: {
// TODO 显示免费礼物
if (data.totalCoin === 0) { // 丢人
return
}
data = new chatModels.AddGiftMsg(data)
this.msgHandler.onAddGift(data)
break

View File

@ -75,10 +75,17 @@ class MsgHandler(blcsdk.BaseHandler):
if extra.is_from_plugin:
return
room = _get_or_add_room(extra.room_id)
room.log(
f'[gift] {message.author_name} 赠送了 {message.gift_name} x {message.num}'
f'总价 {message.total_coin / 1000}'
)
if message.total_coin != 0:
content = (
f'[paid_gift] {message.author_name} 赠送了 {message.gift_name} x {message.num}'
f'总价 {message.total_coin / 1000}'
)
else:
content = (
f'[free_gift] {message.author_name} 赠送了 {message.gift_name} x {message.num}'
f'总价 {message.total_free_coin} 银瓜子'
)
room.log(content)
def _on_add_member(
self, client: blcsdk.BlcPluginClient, message: sdk_models.AddMemberMsg, extra: sdk_models.ExtraData
@ -94,8 +101,7 @@ class MsgHandler(blcsdk.BaseHandler):
guard_name = '总督'
else:
guard_name = '未知舰队等级'
# TODO 可以加上时长
room.log(f'[guard] {message.author_name} 购买了 {guard_name}')
room.log(f'[guard] {message.author_name} 购买了 {message.num}{message.unit} {guard_name}')
def _on_add_super_chat(
self, client: blcsdk.BlcPluginClient, message: sdk_models.AddSuperChatMsg, extra: sdk_models.ExtraData

View File

@ -525,7 +525,9 @@ class LiveMsgHandler(blivedm.BaseHandler):
translation=translation,
content_type=content_type,
content_type_params=content_type_params,
uid=message.uid
# 给插件用的字段
uid=message.uid,
medal_name='' if message.medal_room_id != client.room_id else message.medal_name,
)
room.send_cmd_data(api.chat.Command.ADD_TEXT, data)
services.plugin.broadcast_cmd_data(
@ -539,23 +541,27 @@ class LiveMsgHandler(blivedm.BaseHandler):
avatar_url = services.avatar.process_avatar_url(message.face)
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
# 丢人
if message.coin_type != 'gold':
return
room = client_room_manager.get_room(client.room_key)
if room is None:
return
is_paid_gift = message.coin_type == 'gold'
data = {
'id': uuid.uuid4().hex,
'avatarUrl': avatar_url,
'timestamp': message.timestamp,
'authorName': message.uname,
'totalCoin': message.total_coin,
'totalCoin': 0 if not is_paid_gift else message.total_coin,
'totalFreeCoin': 0 if is_paid_gift else message.total_coin,
'giftName': message.gift_name,
'num': message.num,
'uid': message.uid
# 给插件用的字段
'giftId': message.gift_id,
'giftIconUrl': '',
'uid': message.uid,
'privilegeType': message.guard_level,
'medalLevel': 0,
'medalName': '',
}
room.send_cmd_data(api.chat.Command.ADD_GIFT, data)
services.plugin.broadcast_cmd_data(
@ -580,7 +586,12 @@ class LiveMsgHandler(blivedm.BaseHandler):
'timestamp': message.start_time,
'authorName': message.username,
'privilegeType': message.guard_level,
'uid': message.uid
# 给插件用的字段
'num': message.num,
'unit': '', # 单位在USER_TOAST_MSG消息里不想改消息。现在没有别的单位web接口也很少有人用了先写死吧
'uid': message.uid,
'medalLevel': 0,
'medalName': '',
}
room.send_cmd_data(api.chat.Command.ADD_MEMBER, data)
services.plugin.broadcast_cmd_data(
@ -615,7 +626,11 @@ class LiveMsgHandler(blivedm.BaseHandler):
'price': message.price,
'content': message.message,
'translation': translation,
'uid': message.uid
# 给插件用的字段
'uid': message.uid,
'privilegeType': message.guard_level,
'medalLevel': 0,
'medalName': '',
}
room.send_cmd_data(api.chat.Command.ADD_SUPER_CHAT, data)
services.plugin.broadcast_cmd_data(
@ -722,7 +737,9 @@ class LiveMsgHandler(blivedm.BaseHandler):
translation=translation,
content_type=content_type,
content_type_params=content_type_params,
uid=message.uid
# 给插件用的字段
uid=message.uid,
medal_name='' if not message.fans_medal_wearing_status else message.fans_medal_name,
)
room.send_cmd_data(api.chat.Command.ADD_TEXT, data)
services.plugin.broadcast_cmd_data(
@ -736,23 +753,27 @@ class LiveMsgHandler(blivedm.BaseHandler):
avatar_url = services.avatar.process_avatar_url(message.uface)
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
# 丢人
if not message.paid:
return
room = client_room_manager.get_room(client.room_key)
if room is None:
return
total_coin = message.price * message.gift_num
data = {
'id': message.msg_id,
'avatarUrl': avatar_url,
'timestamp': message.timestamp,
'authorName': message.uname,
'totalCoin': message.price * message.gift_num,
'totalCoin': 0 if not message.paid else total_coin,
'totalFreeCoin': 0 if message.paid else total_coin,
'giftName': message.gift_name,
'num': message.gift_num,
'uid': message.uid
# 给插件用的字段
'giftId': message.gift_id,
'giftIconUrl': message.gift_icon,
'uid': message.uid,
'privilegeType': message.guard_level,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,
}
room.send_cmd_data(api.chat.Command.ADD_GIFT, data)
services.plugin.broadcast_cmd_data(
@ -773,7 +794,12 @@ class LiveMsgHandler(blivedm.BaseHandler):
'timestamp': message.timestamp,
'authorName': message.user_info.uname,
'privilegeType': message.guard_level,
'uid': message.user_info.uid
# 给插件用的字段
'num': message.guard_num,
'unit': message.guard_unit,
'uid': message.user_info.uid,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,
}
room.send_cmd_data(api.chat.Command.ADD_MEMBER, data)
services.plugin.broadcast_cmd_data(
@ -808,7 +834,11 @@ class LiveMsgHandler(blivedm.BaseHandler):
'price': message.rmb,
'content': message.message,
'translation': translation,
'uid': message.uid
# 给插件用的字段
'uid': message.uid,
'privilegeType': message.guard_level,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,
}
room.send_cmd_data(api.chat.Command.ADD_SUPER_CHAT, data)
services.plugin.broadcast_cmd_data(