{"id":20163593,"url":"https://github.com/akegarasu/blivedm-go","last_synced_at":"2025-04-05T12:07:34.550Z","repository":{"id":37083156,"uuid":"449689075","full_name":"Akegarasu/blivedm-go","owner":"Akegarasu","description":"go 版 b站/bilibili 直播/bili直播 弹幕库 - websocket协议","archived":false,"fork":false,"pushed_at":"2024-10-30T08:46:59.000Z","size":124,"stargazers_count":89,"open_issues_count":1,"forks_count":15,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-29T11:09:45.696Z","etag":null,"topics":["bilibili","go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Akegarasu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-19T12:48:58.000Z","updated_at":"2025-02-22T18:29:25.000Z","dependencies_parsed_at":"2022-07-20T11:32:22.703Z","dependency_job_id":"2f584937-7d74-4a2f-8d60-583009619690","html_url":"https://github.com/Akegarasu/blivedm-go","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akegarasu%2Fblivedm-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akegarasu%2Fblivedm-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akegarasu%2Fblivedm-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akegarasu%2Fblivedm-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Akegarasu","download_url":"https://codeload.github.com/Akegarasu/blivedm-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332609,"owners_count":20921853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bilibili","go","golang"],"created_at":"2024-11-14T00:30:23.127Z","updated_at":"2025-04-05T12:07:34.519Z","avatar_url":"https://github.com/Akegarasu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# blivedm-go\n\nbilibili 直播弹幕 golang 库\n\n## 安装\n```shell\ngo get github.com/Akegarasu/blivedm-go\n```\n\n## 快速开始\n\n### 基础使用\n\n该库支持以下几种基本事件，并且支持监听自定义事件。\n- 弹幕\n- 醒目留言\n- 礼物\n- 上舰\n- 开播\n- USER_TOAST_MSG\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/Akegarasu/blivedm-go/client\"\n\t\"github.com/Akegarasu/blivedm-go/message\"\n\t_ \"github.com/Akegarasu/blivedm-go/utils\"\n\tlog \"github.com/sirupsen/logrus\"\n\t\"github.com/tidwall/gjson\"\n)\n\nfunc main() {\n\tlog.SetLevel(log.DebugLevel)\n\tc := client.NewClient(732) // 房间号\n\tc.SetCookie(\"\") // 由于 B站 反爬虫改版，现在需要使用已登陆账号的 Cookie 才可以正常获取弹幕。如果不设置 Cookie，获取到的弹幕昵称、UID都被限制。还有可能弹幕限流，无法获取到全部内容。\n\t//弹幕事件\n\tc.OnDanmaku(func(danmaku *message.Danmaku) {\n\t\tif danmaku.Type == message.EmoticonDanmaku {\n\t\t\tfmt.Printf(\"[弹幕表情] %s：表情URL： %s\\n\", danmaku.Sender.Uname, danmaku.Emoticon.Url)\n\t\t} else {\n\t\t\tfmt.Printf(\"[弹幕] %s：%s\\n\", danmaku.Sender.Uname, danmaku.Content)\n\t\t}\n\t})\n\t// 醒目留言事件\n\tc.OnSuperChat(func(superChat *message.SuperChat) {\n\t\tfmt.Printf(\"[SC|%d元] %s: %s\\n\", superChat.Price, superChat.UserInfo.Uname, superChat.Message)\n\t})\n\t// 礼物事件\n\tc.OnGift(func(gift *message.Gift) {\n\t\tif gift.CoinType == \"gold\" {\n\t\t\tfmt.Printf(\"[礼物] %s 的 %s %d 个 共%.2f元\\n\", gift.Uname, gift.GiftName, gift.Num, float64(gift.Num*gift.Price)/1000)\n\t\t}\n\t})\n\t// 上舰事件\n\tc.OnGuardBuy(func(guardBuy *message.GuardBuy) {\n\t\tfmt.Printf(\"[大航海] %s 开通了 %d 等级的大航海，金额 %d 元\\n\", guardBuy.Username, guardBuy.GuardLevel, guardBuy.Price/1000)\n\t})\n\t// 监听自定义事件\n\tc.RegisterCustomEventHandler(\"STOP_LIVE_ROOM_LIST\", func(s string) {\n\t\tdata := gjson.Get(s, \"data\").String()\n\t\tfmt.Printf(\"STOP_LIVE_ROOM_LIST: %s\\n\", data)\n\t})\n\n\terr := c.Start()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tlog.Println(\"started\")\n\t// 需要自行阻塞什么方法都可以\n\tselect {}\n}\n\n```\n\n### 进阶使用\n\n#### 监听自定义事件\n\n通过自定义监听事件，可以支持更多事件处理。  \n其中，`cmd`为要监听的`cmd`名（下附常见`cmd`名）， `handler`为接收事件消息（字符串的JSON）的函数  \n**注意**  \n优先执行自定义 eventHandler ，会**覆盖库内自带的 handler**  \n例如，如果你`RegisterCustomEventHandler(\"DANMU_MSG\", ...`  \n那么你使用`OnDanmaku`则不会再生效\n```go\nfunc (c *Client) RegisterCustomEventHandler(cmd string, handler func(s string))\n```\n```go\n// 监听自定义事件\nc.RegisterCustomEventHandler(\"STOP_LIVE_ROOM_LIST\", func(s string) {\n    data := gjson.Get(s, \"data\").String()\n    fmt.Printf(data)\n})\n```\n\n### 常见 CMD\n注：来自blivedm\n```python\ncmd = (\n        'INTERACT_WORD', 'ROOM_BANNER', 'ROOM_REAL_TIME_MESSAGE_UPDATE', 'NOTICE_MSG', 'COMBO_SEND',\n        'COMBO_END', 'ENTRY_EFFECT', 'WELCOME_GUARD', 'WELCOME', 'ROOM_RANK', 'ACTIVITY_BANNER_UPDATE_V2',\n        'PANEL', 'SUPER_CHAT_MESSAGE_JPN', 'USER_TOAST_MSG', 'ROOM_BLOCK_MSG', 'LIVE', 'PREPARING',\n        'room_admin_entrance', 'ROOM_ADMINS', 'ROOM_CHANGE'\n    )\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakegarasu%2Fblivedm-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakegarasu%2Fblivedm-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakegarasu%2Fblivedm-go/lists"}