Skip to content

Commit

Permalink
Merge pull request #24 from eric2788/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 authored Apr 16, 2022
2 parents 9b4bfe6 + bd6153a commit dab0b13
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 38 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ B站直播WS讯息监控指令
"show_cover": true, // 开播通知时是否显示封面
"show_gift_danmu": false, // 是否显示礼物弹幕
"auto_check_update": true, // 是否自动每天检查更新
"show_detail_list": true, // 显示高亮/直播/注视列表时是否顺带显示用户名称(需要更多时间刷取)
"debug_mode": false // 是否啟用 debug 模式
}
```

Expand Down
12 changes: 7 additions & 5 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

### 更新

- 可设定是否过滤抽奖和红包弹幕
- 新增表情包发送
- 更详细的 logging, 並设有档案记录 /logs/
- 新增每天自动检查 (只在黑窗回报)
- 新增顯示列表時順帶顯示用戶名稱(列表內的需要重新添加才能生效) (開播時刷新)
- 修復高亮列表顯示 Object object 而不是用戶名稱
- 新增設定是否顯示詳細列表
- 新增注视用户详细列表名单
- 新增大航海用户捕捉
- 新增 debug 模式設定

### settings.json 模版参考

Expand All @@ -34,6 +34,8 @@
"show_cover": true,
"show_gift_danmu": false,
"auto_check_update": true,
"show_detail_list": true,
"debug_mode": false
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vup_monitors",
"version": "0.1.9",
"version": "0.1.11",
"bin": "src/index.js",
"main": "src/index.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions src/command_listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ module.exports = async({data, ws, http}) => {
console.debug(`正在处理 ${is_group ? '群' : '私聊'} 指令: ${cmd}, 参数: ${args}`)
await invoke(actions, cmd, args)
}catch(err){
console.warn('执行指令时出现错误')
console.error(err)
console.warn('执行指令时出现错误: '+err)
}
}

Expand Down
29 changes: 18 additions & 11 deletions src/commands/blive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { CommandExecutor } = require("../el/types");
const messager = require('../el/api/message-source')
const { toRealRoom: validRoom } = require('../el/utils')
const settings = require('../el/data-storer').settings

class BLiveListen extends CommandExecutor {

Expand Down Expand Up @@ -58,20 +59,26 @@ class BLiveListening extends CommandExecutor {

const displays = []

await send(`正在刷取监听房间资讯,可能需要几分钟...`)
if (settings.show_detail_list){
await send(`正在刷取监听房间资讯,可能需要几分钟...`)
}

for (const room of set) {
try {
let res;
if (roomMap.has(room)) {
res = roomMap.get(room)
}else{
res = await messager.getRoomUserName(room)
roomMap.set(room, res)
if (settings.show_detail_list){
try {
let res;
if (roomMap.has(room)) {
res = roomMap.get(room)
}else{
res = await messager.getRoomUserName(room)
roomMap.set(room, res)
}
displays.push(`${room}(${res?.name})`)
}catch(err){
console.warn(`獲取房間資訊錯誤: ${err}`)
displays.push(`${room}`)
}
displays.push(`${room}(${res?.name})`)
}catch(err){
console.error(`獲取房間資訊錯誤: ${err}`)
}else{
displays.push(`${room}`)
}
}
Expand Down
39 changes: 36 additions & 3 deletions src/commands/focus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { CommandExecutor } = require('../el/types');
const storer = require('../el/data-storer')
const { validUser } = require('../el/utils');
const level = require('../el/cachedb')

class AddFocus extends CommandExecutor {

Expand Down Expand Up @@ -36,6 +37,12 @@ class AddFocus extends CommandExecutor {
data.group_id = group_id
}

const username = await validUser(uid)
if (!username) {
await send(`找不到用户 ${uid}`)
return
}

const json = await storer.read()
const blive = json.blive

Expand All @@ -50,14 +57,14 @@ class AddFocus extends CommandExecutor {
}

if (focus_users[data.group_id].includes(uid)) {
await send(`用户 ${uid} 已经在群 ${data.group_id} 的注视用户名单内。`)
await send(`用户 ${username}(${uid}) 已经在群 ${data.group_id} 的注视用户名单内。`)
return
}

focus_users[data.group_id].push(uid)

storer.save(json)
await send(`已成功添加用户 ${uid} 到群 ${data.group_id} 的注视用户名单内。`)
await send(`已成功添加用户 ${username}(${uid}) 到群 ${data.group_id} 的注视用户名单内。`)

}
}
Expand Down Expand Up @@ -136,7 +143,33 @@ class Focusing extends CommandExecutor {
}

const focusing = focus_users[data.group_id] ?? []
await send(`群 ${data.group_id} 的注视用户列表: ${focusing}`)

const displays = []

if (storer.settings.show_detail_list){
await send(`正在刷取群 ${data.group_id} 的注视用户资讯,可能需要几分钟...`)
}

for (const uid of focusing){
if (storer.settings.show_detail_list){
try {
const user = await level.getUser(uid)
if (user) {
displays.push(`${user.name}(${uid})`)
} else {
console.warn(`缓存找不到用户 ${uid}, 你可能需要重新手动添加才能生效`)
displays.push(`${uid}`)
}
} catch (err) {
console.warn(`獲取用戶資訊錯誤: ${err}`)
displays.push(`${uid}`)
}
}else{
displays.push(`${uid}`)
}
}

await send(`群 ${data.group_id} 的注视用户列表: ${displays}`)
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/commands/highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,28 @@ class HighLighting extends CommandExecutor {
const highlight = blive[key]
const users = highlight[id] ?? []


if (storer.settings.show_detail_list){
await send(`正在刷取用户资讯,可能需要几分钟...`)
}

const displays = []

for (const uid of users) {
try {
const username = await level.getUser(uid)
if (username) {
displays.push(`${username}(${uid})`)
} else {
if (storer.settings.show_detail_list){
try {
const user = await level.getUser(uid)
if (user) {
displays.push(`${user.name}(${uid})`)
} else {
console.warn(`缓存找不到用户 ${uid}, 你可能需要重新手动添加才能生效`)
displays.push(`${uid}`)
}
} catch (err) {
console.warn(`獲取用戶資訊錯誤: ${err}`)
displays.push(`${uid}`)
}
} catch (err) {
console.error(`獲取用戶資訊錯誤: ${err}`)
}else{
displays.push(`${uid}`)
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const room_enter = require('./ws_handles/room_enter')
const superchat_msg = require('./ws_handles/superchat_msg')
const live_broadcast = require('./ws_handles/live_broadcast')
const live_end = require('./ws_handles/live_end')
const entry_effect = require('./ws_handles/entry_effect')

class Help extends CommandExecutor {

Expand All @@ -36,6 +37,7 @@ module.exports = {
'LIVE': live_broadcast,
'PREPARING': live_end,
'SUPER_CHAT_MESSAGE': superchat_msg,
'INTERACT_WORD': room_enter
'INTERACT_WORD': room_enter,
'ENTRY_EFFECT': entry_effect
}
}
4 changes: 2 additions & 2 deletions src/el/api/websocket_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { handleMessage } = require('../message-handler');

const IDENTIFIER = 'vup_monitors'

class WebSocketSouce extends MessageSource {
class WebSocketSource extends MessageSource {


constructor(){
Expand Down Expand Up @@ -98,5 +98,5 @@ async function initWebSocket(url){


module.exports = {
'websocket': WebSocketSouce
'websocket': WebSocketSource
}
2 changes: 1 addition & 1 deletion src/el/command-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const invoke = async ({ send, data, commands }, command, args) => {
await executor.execute({ send, data }, args)
}catch(err){
console.error(`执行指令 ${cmd} 时出现错误: ${err?.message}`)
console.error(err)
console.debug(err.stack)
await send(`执行时发生错误: ${err?.message ?? err}`)
}
return
Expand Down
4 changes: 3 additions & 1 deletion src/el/data-storer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const DEFAULT_CONFIG = {
"enable_live_broadcast": false,
"show_cover": true,
"show_gift_danmu": false,
"auto_check_update": true
"auto_check_update": true,
"show_detail_list": true,
"debug_mode": false
}


Expand Down
2 changes: 1 addition & 1 deletion src/el/message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function handleMessage(string){
}
handle({ws, http}, message).catch(err => {
console.warn(`执行指令 ${message.command} 时出现错误: ${err?.message}`)
console.error(err)
console.debug(err.stack)
})
}catch(err) {
console.warn(`接收 redis 数据时出现错误: ${err?.message ?? err}`)
Expand Down
2 changes: 1 addition & 1 deletion src/el/updater.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { default: axios } = require('axios')
const semver = require('semver');

const VERSION = '0.1.10'
const VERSION = '0.1.11'

const updaterApi = axios.create({
baseURL: 'https://api.github.com/repos/eric2788/vup_monitors',
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function executeCommands(data) {
await onCommand({ data, ws, http })
} catch (err) {
console.warn(`执行指令时出现错误: ${err?.message}`)
console.error(err)
console.debug(err.stack)
}
}

Expand All @@ -37,6 +37,6 @@ Promise.all([ws.startWS(), messager.connect(), db.initDB(), updater.checkUpdate(

})
.catch(err => {
console.error('启动程序时错误:')
console.error('启动程序时错误: ')
console.error(err.stack)
})
62 changes: 62 additions & 0 deletions src/ws_handles/entry_effect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { sendMessage, sendMessagePrivate, filterAndBroadcast, validUser } = require("../el/utils")
const storer = require('../el/data-storer')
const { send } = require("../bot/http")

module.exports = async ({ws, http}, data) => {
const { uid } = data.content.data
const liveName = data.live_info.name

const blive = (await storer.read())?.blive
const { highlight, highlight_private, focus_users } = blive ?? { highlight: {}, highlight_private: {}, focus_users: {} }

let group_ids = Object.keys(highlight ?? {})
if (focus_users) {
group_ids = group_ids.filter((group_id) => {
const users = focus_users[group_id]
// 没有注视用户,则全部广播,
if (!users || users.length == 0) return true
// 那个用户正是那个群的注视用户,所以所有DD行为都要广播
if (users.includes(uid)) return true
// 否则,只检查注视用户的直播间
return users.includes(data.live_info.uid)
})
}

const group_highlight = {}

group_ids.forEach((group_id) => group_highlight[group_id] = highlight[group_id])


const anyPrivate = Object.entries(highlight_private).some(([id, users]) => users.includes(uid))
const anyGroup = Object.entries(group_highlight).some(([id, users]) => users.includes(uid))

// 没有群和人关注这个uid时则返回防止请求api频繁
if (!anyPrivate && !anyGroup){
return
}

const uname = await validUser(uid)
const messages = `噔噔咚!你所关注的大航海用户 ${uname} 进入了` + (uname == liveName ? `自己` : ` ${liveName} `) + `的直播间。`

// === 广播到群聊 ===
const sends = filterAndBroadcast(group_highlight, uid, sendMessage, ws, messages)
if (sends.length > 0) {
Promise.all(sends)
.then(sent => console.log(`房间进入通知已发送给 ${sent.length} 个QQ群组。`))
.catch(err => {
console.warn(`發送广播通知时出现错误: ${err?.message}`)
console.warn(err)
})
}

// === 广播到 QQ 号 ===
const private_sends = filterAndBroadcast(highlight_private, uid, sendMessagePrivate, ws, messages)
if (private_sends.length > 0){
Promise.all(private_sends)
.then(sent => console.log(`房间进入通知已发送给 ${sent.length} 个QQ号。`))
.catch(err => {
console.warn(`發送广播通知时出现错误: ${err?.message}`)
console.warn(err)
})
}
}
10 changes: 10 additions & 0 deletions test/highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ async function testRemoveHighlight(){
assert(!(data.blive.highlight[group_id] ?? []).includes(user), '未成功移除高亮');
}

async function testShowHighlight(){
await (new highlights['列表']()).execute(cmdExecute)
}


const focus_users = [
1576121,
Expand All @@ -57,9 +61,15 @@ async function testRemoveFocus(){
assert(!(data.blive.focus_users[group_id] ?? []).includes(user), '未成功移除注视用户');
}

async function testShowFocus(){
await (new focus['列表']()).execute(cmdExecute)
}

describe('高亮用户修改测试', () => {
it('新增高亮用户', testAddHighlight)
it('移除高亮用户', testRemoveHighlight)
it('显示高亮用户列表', testShowHighlight)
it('新增注视用户', testAddFocus)
it('移除注视用户', testRemoveFocus)
it('显示注视用户列表', testShowFocus)
})

0 comments on commit dab0b13

Please sign in to comment.