File size: 2,848 Bytes
7667461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-

from dm_player import DmPlayer

import asyncio
import http.cookies
import random
from typing import *

import aiohttp

import blivedm
import blivedm.models.web as web_models

# 直播间ID的取值看直播间URL
TEST_ROOM_ID = 9792252

# 这里填一个已登录账号的cookie的SESSDATA字段的值。不填也可以连接,但是收到弹幕的用户名会打码,UID会变成0
SESSDATA = ''

session: Optional[aiohttp.ClientSession] = None

dmplayer = DmPlayer()

async def dm_monitor():
    init_session()
    try:
        await run_single_client()
    finally:
        await session.close()


def init_session():
    cookies = http.cookies.SimpleCookie()
    cookies['SESSDATA'] = SESSDATA
    cookies['SESSDATA']['domain'] = 'bilibili.com'

    global session
    session = aiohttp.ClientSession()
    session.cookie_jar.update_cookies(cookies)


async def run_single_client():
    room_id = TEST_ROOM_ID
    client = blivedm.BLiveClient(room_id, session=session)
    handler = MyHandler()
    client.set_handler(handler)

    client.start()
    try:
        await client.join()
    finally:
        await client.stop_and_close()

def test_callback(user, msg):
    # 可以给消息写类似的callback,从而设置l2d表情、换人说话等
    print(f'{user}: {msg}')

class MyHandler(blivedm.BaseHandler):
    _CMD_CALLBACK_DICT = blivedm.BaseHandler._CMD_CALLBACK_DICT.copy()
    
    def __interact_word_callback(self, client: blivedm.BLiveClient, command: dict):
        uname = command['data']['uname']
        msg_type = command['data']['msg_type']
        if msg_type == 2:
            dmplayer.Add(f'谢谢{uname}的关注喵', "bubu.wav")

    _CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback

    def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
        pass

    def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
        dmplayer.Add(message.msg, "bubu.wav", test_callback, message.uname, message.msg) # 带有callback的加入队列

    def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
        dmplayer.Add(f'感谢{message.uname}赠送的{message.num}{message.gift_name},阿里嘎多!', "po.wav") # 普通的加入队列

    def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
        dmplayer.Add(f'感谢{message.uname}{message.gift_name},阿里嘎多!', "po.wav")

    def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
        dmplayer.Add(f'{message.uname}说:{message.message}', "dingding.wav")


if __name__ == '__main__':
    dmplayer.Start()
    dmplayer.LoadCharacter("圣园未花")
    dmplayer.SetVoiceOption(0.6, 0.668, 0.9)
    asyncio.run(dm_monitor())