Skip to main content

基于ws的b站弹幕监控

Project description

NoneBotPluginLogo

NoneBotPluginText

nonebot-plugin-SimpleToWrite

✨ NoneBot 插件描述 ✨

这是一个b站直播间弹幕监听插件

特别鸣谢

  • blivedm项目的灵感来源以及部分实现的参考

📖 介绍

通过ws连接b站直播间,监听弹幕 具体使用方法和配置见下面的配置和使用例子

💿 安装

使用 nb-cli 安装 在 nonebot2 项目的根目录下打开命令行, 输入以下指令即可安装
nb plugin install nonebot_plugin_bililivedm
使用包管理器安装 在 nonebot2 项目的插件目录下, 打开命令行, 根据你使用的包管理器, 输入相应的安装命令
pip
pip install nonebot_plugin_bililivedm

打开 nonebot2 项目根目录下的 pyproject.toml 文件, 在 [tool.nonebot] 部分追加写入

plugins = ["nonebot_plugin_bililivedm"]

⚙️ 配置

请在.env文件里面这样配置

bililiveid=[b站直播间id] 
bilitoken=  ##这里填一个已登录账号的cookie的SESSDATA字段的值。不填也可以连接,但是收到弹幕的用户名会打码,UID会变成0
bililivedown=on ##是否开启b站直播间弹幕监听功能,on为开启,其他为关闭
bilidmj=on ##是否开启b站直播间弹幕显示器,on为开启,其他为关闭
loadws= ##是否开启websocket功能,默认为ws://127.0.0.1:8000/onebot/v11/ws

如何使用b站弹幕功能

在.env文件里面配置好之后,在你自己的py文件里面这样使用ws 以默认的ws://127.0.0.1:8000/onebot/v11/ws为例

from aiohttp import web
import threading
import nonebot

async def websocket_handler(request):  ##千万别在这里使用async def websocket_handler(request,bot: Bot):不然会导致报错无法连接
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        reout = msg.data
        tes = json.loads(msg.data)
        send = tes["message"]
        (bot,) = nonebot.get_bots().values()
        await bot.send_private_msg(user_id=12335, message=send)

    return ws

def run_app():
    app = web.Application()
    app.router.add_get("/onebot/v11/ws", websocket_handler)
    web.run_app(app, host="127.0.0.1", port=8000,print=None)

# 在主线程中创建一个子线程,防止卡住nb进程(或者用py单独运行个.py文件也行)
thread = threading.Thread(target=run_app)
thread.start()

参数说明

{
    "user_id": int, 
    "nickname": str,
    "message": str,
    "room_id": int,
    "type": str, ##仅支持普通消息(message)和醒目留言(super_chat)
    "price":int
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_bililivedm-0.0.1.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

nonebot_plugin_bililivedm-0.0.1-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_bililivedm-0.0.1.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_bililivedm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8d50293e605a1de79441b692f4317b70f7e0b7be640ed0fb9096d7210888ccce
MD5 fcb880264d742c0c200341ac2fea61b1
BLAKE2b-256 5af4b8329737ec7e1cb5ed4a2cf899109169a9b4da1d7651129500b42dbcfb37

See more details on using hashes here.

File details

Details for the file nonebot_plugin_bililivedm-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_bililivedm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ecef32ef62803da891b500df0aaa0404dcb391844f52eb3bc494b56b20a4cf4
MD5 e2b0f98b07e1ab4e44039714e5b52332
BLAKE2b-256 232b838d552695b1d19869045f31f35c3efc0441fc358b453758b6e6695cba6f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page