diff --git a/starbot/core/room.py b/starbot/core/room.py index 7b46c70..6fd2f0c 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -1,4 +1,5 @@ import asyncio +import base64 import time import typing from asyncio import AbstractEventLoop @@ -16,6 +17,7 @@ from ..utils import config, redis from ..utils.network import request from ..utils.utils import get_credential, timestamp_format, get_unames_and_faces_by_uids +from ..utils.gift_pb2 import SendGiftV2 if typing.TYPE_CHECKING: from .sender import Bot @@ -315,6 +317,49 @@ async def on_danmu(event): "box", "gift", "box_ranking", "box_profit_ranking", "gift_ranking", "box_profit_diagram", "box_diagram", "gift_diagram" ] + + if not config.get("ONLY_HANDLE_NECESSARY_EVENT") or self.__any_live_report_item_enabled(gift_items): + @self.__room.on("SEND_GIFT_V2") + async def on_gift_v2(event): + """ + 礼物事件 + """ + # logger.info(f"{self.uname} (SEND_GIFT_V2): {event}") + try: + pb = event.get('data', {}).get('data', {}).get("pb", "") + rawData = base64.b64decode(pb) + v = SendGiftV2() + v.ParseFromString(rawData) + for gift in v.gifts: + base = { + "giftId": gift.gift_id, + "giftName": gift.gift_name, + "uid": v.sender.uid, + "num": gift.num, + "total_coin": gift.total_coin, + "discount_price": gift.discount_price, + 'blind_gift': None + } + if v.HasField("blind"): + base['blind_gift'] = { + "original_gift_id": v.blind.original_gift_id, + "original_gift_name": v.blind.original_gift_name, + "blind_price": v.blind.blind_price, + } + newEvent = { + 'room_display_id': event.get("room_display_id", ), + 'room_real_id': event.get("room_real_id", ), + 'data': { + "data": base, + "cmd": 'SEND_GIFT', + }, + 'type': 'SEND_GIFT', + } + self.dispatch('SEND_GIFT', newEvent) + + except Exception as ex: + logger.exception("SEND_GIFT_V2异常", ex) + if not config.get("ONLY_HANDLE_NECESSARY_EVENT") or self.__any_live_report_item_enabled(gift_items): @self.__room.on("SEND_GIFT") async def on_gift(event): diff --git a/starbot/utils/gift_pb2.py b/starbot/utils/gift_pb2.py new file mode 100644 index 0000000..b6c390a --- /dev/null +++ b/starbot/utils/gift_pb2.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# NO CHECKED-IN PROTOBUF GENCODE +# source: gift.proto +# Protobuf Python Version: 7.35.1 +"""Generated protocol buffer code.""" +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import runtime_version as _runtime_version +from google.protobuf import symbol_database as _symbol_database +from google.protobuf.internal import builder as _builder +_runtime_version.ValidateProtobufRuntimeVersion( + _runtime_version.Domain.PUBLIC, + 7, + 35, + 1, + '', + 'gift.proto' +) +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\ngift.proto\x12\x04gift\"\xc6\x01\n\nSendGiftV2\x12\x0b\n\x03uid\x18\x01 \x01(\x04\x12\r\n\x05uname\x18\x02 \x01(\t\x12\x0c\n\x04\x66\x61\x63\x65\x18\x03 \x01(\t\x12\x1e\n\x05medal\x18\x08 \x01(\x0b\x32\x0f.gift.MedalInfo\x12\x1e\n\x05\x62lind\x18\t \x01(\x0b\x32\x0f.gift.BlindGift\x12\x1d\n\x05gifts\x18\n \x03(\x0b\x32\x0e.gift.GiftData\x12\x0f\n\x07\x66ield11\x18\x0b \x01(\r\x12\x1e\n\x06sender\x18\x0f \x01(\x0b\x32\x0e.gift.UserInfo\"\xaa\x01\n\tMedalInfo\x12\x12\n\nanchor_uid\x18\x01 \x01(\x04\x12\x13\n\x0bmedal_level\x18\x05 \x01(\r\x12\x12\n\nmedal_name\x18\x06 \x01(\t\x12\x13\n\x0b\x63olor_start\x18\x07 \x01(\r\x12\r\n\x05\x63olor\x18\x08 \x01(\r\x12\x14\n\x0c\x63olor_border\x18\t \x01(\r\x12\x11\n\tcolor_end\x18\n \x01(\r\x12\x13\n\x0bguard_level\x18\x0b \x01(\r\"{\n\tBlindGift\x12\x13\n\x0bgift_action\x18\x01 \x01(\r\x12\x18\n\x10original_gift_id\x18\x02 \x01(\r\x12\x1a\n\x12original_gift_name\x18\x03 \x01(\t\x12\x0e\n\x06\x61\x63tion\x18\x05 \x01(\t\x12\x13\n\x0b\x62lind_price\x18\x06 \x01(\r\"\xa6\x03\n\x08GiftData\x12\x0f\n\x07gift_id\x18\x01 \x01(\r\x12\x11\n\tgift_name\x18\x02 \x01(\t\x12\x0b\n\x03num\x18\x03 \x01(\r\x12\x11\n\tgift_type\x18\x04 \x01(\r\x12\r\n\x05price\x18\x05 \x01(\r\x12\x12\n\ntotal_coin\x18\x07 \x01(\r\x12\x16\n\x0e\x64iscount_price\x18\x06 \x01(\r\x12\x11\n\tcoin_type\x18\x08 \x01(\t\x12\x0b\n\x03tid\x18\t \x01(\t\x12\x11\n\ttimestamp\x18\n \x01(\x04\x12\r\n\x05super\x18\x0b \x01(\r\x12\x0b\n\x03rnd\x18\x0c \x01(\t\x12\x0f\n\x07\x66ield13\x18\r \x01(\r\x12\x0f\n\x07\x66ield14\x18\x0e \x01(\r\x12\x0f\n\x07\x66ield15\x18\x0f \x01(\r\x12\x12\n\ngift_ratio\x18\x10 \x01(\x02\x12\x0f\n\x07\x66ield17\x18\x11 \x01(\r\x12\x0e\n\x06\x61\x63tion\x18\x12 \x01(\t\x12\x0f\n\x07\x66ield24\x18\x18 \x01(\r\x12 \n\x08receiver\x18\x1d \x01(\x0b\x32\x0e.gift.Receiver\x12 \n\x06\x65\x66\x66\x65\x63t\x18# \x01(\x0b\x32\x10.gift.GiftEffect\x12\x0f\n\x07\x66ield36\x18$ \x01(\r\"&\n\x08Receiver\x12\r\n\x05uname\x18\x01 \x01(\t\x12\x0b\n\x03uid\x18\x02 \x01(\x04\"?\n\nGiftEffect\x12\x11\n\timg_basic\x18\x01 \x01(\t\x12\r\n\x05img_2\x18\x02 \x01(\t\x12\x0f\n\x07img_gif\x18\x05 \x01(\t\"5\n\x08UserInfo\x12\x0b\n\x03uid\x18\x01 \x01(\x04\x12\x1c\n\x04\x62\x61se\x18\x02 \x01(\x0b\x32\x0e.gift.UserBase\"\'\n\x08UserBase\x12\r\n\x05uname\x18\x01 \x01(\t\x12\x0c\n\x04\x62\x61se\x18\x02 \x01(\tb\x06proto3') + +_globals = globals() +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'gift_pb2', _globals) +if not _descriptor._USE_C_DESCRIPTORS: + DESCRIPTOR._loaded_options = None + _globals['_SENDGIFTV2']._serialized_start=21 + _globals['_SENDGIFTV2']._serialized_end=219 + _globals['_MEDALINFO']._serialized_start=222 + _globals['_MEDALINFO']._serialized_end=392 + _globals['_BLINDGIFT']._serialized_start=394 + _globals['_BLINDGIFT']._serialized_end=517 + _globals['_GIFTDATA']._serialized_start=520 + _globals['_GIFTDATA']._serialized_end=942 + _globals['_RECEIVER']._serialized_start=944 + _globals['_RECEIVER']._serialized_end=982 + _globals['_GIFTEFFECT']._serialized_start=984 + _globals['_GIFTEFFECT']._serialized_end=1047 + _globals['_USERINFO']._serialized_start=1049 + _globals['_USERINFO']._serialized_end=1102 + _globals['_USERBASE']._serialized_start=1104 + _globals['_USERBASE']._serialized_end=1143 +# @@protoc_insertion_point(module_scope)