From 6de125ef561602bfdfdf16ea21dd92d07f875a27 Mon Sep 17 00:00:00 2001 From: mengnitj Date: Mon, 7 Sep 2026 14:45:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8E=A5=E5=85=A5SendGiftV2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接入SendGiftV2,需安装依赖protobuf --- starbot/core/room.py | 44 ++++++++++++++++++++++++++++++++++ starbot/utils/gift_pb2.py | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 starbot/utils/gift_pb2.py diff --git a/starbot/core/room.py b/starbot/core/room.py index 7b46c70..3fcae90 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,48 @@ 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) + base = { + "giftId": v.gift.gift_id, + "giftName": v.gift.gift_name, + "uid": v.sender.uid, + "num": v.gift.num, + "total_coin": v.gift.discount_price, + "discount_price": v.gift.total_coin, + '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..638e71a --- /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\"\xc5\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\x1c\n\x04gift\x18\n \x01(\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\x06 \x01(\r\x12\x16\n\x0e\x64iscount_price\x18\x07 \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=218 + _globals['_MEDALINFO']._serialized_start=221 + _globals['_MEDALINFO']._serialized_end=391 + _globals['_BLINDGIFT']._serialized_start=393 + _globals['_BLINDGIFT']._serialized_end=516 + _globals['_GIFTDATA']._serialized_start=519 + _globals['_GIFTDATA']._serialized_end=941 + _globals['_RECEIVER']._serialized_start=943 + _globals['_RECEIVER']._serialized_end=981 + _globals['_GIFTEFFECT']._serialized_start=983 + _globals['_GIFTEFFECT']._serialized_end=1046 + _globals['_USERINFO']._serialized_start=1048 + _globals['_USERINFO']._serialized_end=1101 + _globals['_USERBASE']._serialized_start=1103 + _globals['_USERBASE']._serialized_end=1142 +# @@protoc_insertion_point(module_scope) From de3174bcaf327d1a3b31525efa0720d8191819fd Mon Sep 17 00:00:00 2001 From: mengnitj Date: Mon, 7 Sep 2026 23:20:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9E=E6=8A=BD?= =?UTF-8?q?=E7=9B=B2=E7=9B=92=E7=BB=9F=E8=AE=A1=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- starbot/core/room.py | 48 ++++++++++++++++++++------------------- starbot/utils/gift_pb2.py | 32 +++++++++++++------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/starbot/core/room.py b/starbot/core/room.py index 3fcae90..fe19f2e 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -331,30 +331,32 @@ async def on_gift_v2(event): v = SendGiftV2() v.ParseFromString(rawData) base = { - "giftId": v.gift.gift_id, - "giftName": v.gift.gift_name, - "uid": v.sender.uid, - "num": v.gift.num, - "total_coin": v.gift.discount_price, - "discount_price": v.gift.total_coin, - '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, + 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 } - 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) + 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) diff --git a/starbot/utils/gift_pb2.py b/starbot/utils/gift_pb2.py index 638e71a..b6c390a 100644 --- a/starbot/utils/gift_pb2.py +++ b/starbot/utils/gift_pb2.py @@ -24,7 +24,7 @@ -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\ngift.proto\x12\x04gift\"\xc5\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\x1c\n\x04gift\x18\n \x01(\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\x06 \x01(\r\x12\x16\n\x0e\x64iscount_price\x18\x07 \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') +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) @@ -32,19 +32,19 @@ if not _descriptor._USE_C_DESCRIPTORS: DESCRIPTOR._loaded_options = None _globals['_SENDGIFTV2']._serialized_start=21 - _globals['_SENDGIFTV2']._serialized_end=218 - _globals['_MEDALINFO']._serialized_start=221 - _globals['_MEDALINFO']._serialized_end=391 - _globals['_BLINDGIFT']._serialized_start=393 - _globals['_BLINDGIFT']._serialized_end=516 - _globals['_GIFTDATA']._serialized_start=519 - _globals['_GIFTDATA']._serialized_end=941 - _globals['_RECEIVER']._serialized_start=943 - _globals['_RECEIVER']._serialized_end=981 - _globals['_GIFTEFFECT']._serialized_start=983 - _globals['_GIFTEFFECT']._serialized_end=1046 - _globals['_USERINFO']._serialized_start=1048 - _globals['_USERINFO']._serialized_end=1101 - _globals['_USERBASE']._serialized_start=1103 - _globals['_USERBASE']._serialized_end=1142 + _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) From f5bf643e5d3095561ebdc9f0768ab2bc562f3167 Mon Sep 17 00:00:00 2001 From: mengnitj Date: Mon, 7 Sep 2026 23:21:35 +0800 Subject: [PATCH 3/3] Update room.py --- starbot/core/room.py | 1 - 1 file changed, 1 deletion(-) diff --git a/starbot/core/room.py b/starbot/core/room.py index fe19f2e..6fd2f0c 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -330,7 +330,6 @@ async def on_gift_v2(event): rawData = base64.b64decode(pb) v = SendGiftV2() v.ParseFromString(rawData) - base = { for gift in v.gifts: base = { "giftId": gift.gift_id,