Skip to content

Commit

Permalink
perf: 可设置统计黑白名单中的群
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Jan 6, 2025
1 parent 6ab1edc commit 63d0c05
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 91 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pnpm install --filter=steam-plugin

- steam喜加一
- steam游戏详情
- steam用户统计

## 联系方式

Expand Down Expand Up @@ -129,7 +128,7 @@ pnpm install --filter=steam-plugin
> 🌟 星光闪烁,你们的智慧如同璀璨的夜空。感谢所有为 **steam-plugin** 做出贡献的人!
<a href="https://github.com/XasYer/steam-plugin/graphs/contributors">
<img src="https://contrib.rocks/image?repo=XasYer%2Fsteam-plugin" />
<img src="https://contrib.rocks/image?repo=XasYer/steam-plugin" />
</a>

![Alt](https://repobeats.axiom.co/api/embed/aafe6a6a7a72df285ae3965974546314c467db8d.svg "Repobeats analytics image")
Expand Down
40 changes: 7 additions & 33 deletions apps/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const rule = {
push: {
reg: App.getReg('(?:开启|关闭)推送\\s*(\\d*)'),
fnc: async e => {
if (!await checkGroup(e)) {
const g = utils.bot.checkGroup(e.group_id)
if (!g.success) {
await e.reply(g.message)
return true
}
const textId = rule.push.reg.exec(e.msg)[1]
Expand Down Expand Up @@ -63,7 +65,9 @@ const rule = {
list: {
reg: App.getReg('(本群)?推送列表'),
fnc: async e => {
if (!await checkGroup(e)) {
const g = utils.bot.checkGroup(e.group_id)
if (!g.success) {
await e.reply(g.message)
return true
}
const list = await db.PushTableGetDataByGroupId(e.group_id, true)
Expand Down Expand Up @@ -156,7 +160,7 @@ const rule = {
desc: utils.steam.getPersonaState(i.personastate),
image: await utils.bot.getUserAvatar(userInfo.botId, userInfo.userId, userInfo.groupId) || (Config.other.steamAvatar ? i.avatarfull : `https://q.qlogo.cn/g?b=qq&s=100&nk=${userInfo.userId}`),
isAvatar: true,
descBgColor: getColor(i.personastate)
descBgColor: utils.steam.getStateColor(i.personastate)
})
}
}
Expand All @@ -180,33 +184,3 @@ const rule = {
}

export const app = new App(appInfo, rule).create()

async function checkGroup (e) {
if (!e.group_id) {
return false
}
if (!Config.push.enable) {
await e.reply('主人没有开启推送功能哦')
return false
}
if (Config.push.whiteGroupList.length && !Config.push.whiteGroupList.some(id => id == e.group_id)) {
await e.reply('本群没有在推送白名单中, 请联系主人添加~')
return false
}
if (Config.push.blackGroupList.length && Config.push.blackGroupList.some(id => id == e.group_id)) {
await e.reply('本群在推送黑名单中, 请联系主人解除~')
return false
}
return true
}

function getColor (state) {
switch (Number(state)) {
case 1:
return '#beee11'
case 0:
return '#999999'
default:
return '#8fbc8b'
}
}
23 changes: 3 additions & 20 deletions apps/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const rule = {
tips: true
},
fnc: async e => {
if (!checkGroup(e.group_id)) {
const g = utils.bot.checkGroup(e.group_id)
if (!g.success) {
await e.reply(g.message)
return true
}
const limit = Math.max(1, Number(Config.other.statsCount) || 10)
Expand Down Expand Up @@ -159,23 +161,4 @@ const rule = {
}
}

async function checkGroup (e) {
if (!e.group_id) {
return false
}
if (!Config.push.enable) {
await e.reply('主人没有开启推送功能哦')
return false
}
if (Config.push.whiteGroupList.length && !Config.push.whiteGroupList.some(id => id == e.group_id)) {
await e.reply('本群没有在推送白名单中, 请联系主人添加~')
return false
}
if (Config.push.blackGroupList.length && Config.push.blackGroupList.some(id => id == e.group_id)) {
await e.reply('本群在推送黑名单中, 请联系主人解除~')
return false
}
return true
}

export const app = new App(appInfo, rule).create()
6 changes: 6 additions & 0 deletions components/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Config {
* time: number,
* defaultPush: boolean,
* randomBot: boolean,
* statusFilterGroup: boolean,
* blackBotList: string[],
* whiteBotList: string[],
* blackGroupList: string[],
Expand Down Expand Up @@ -181,6 +182,11 @@ class Config {
* inventoryEmptyTips: string,
* recentPlayEmptyTips: string,
* wishListEmptyTips: string,
* privateUseTips: string,
* pushDisabledTips: string,
* blackGroupTips: string,
* noWhiteGroupTips: string,
* makeImageFailedTips: string,
* }}
*/
get tips () {
Expand Down
2 changes: 1 addition & 1 deletion components/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const Render = {
if (img) {
return img
} else {
return '制作图片出错辣!再试一次吧'
return Config.tips.makeImageFailedTips
}
},
async simpleRender (path, params) {
Expand Down
6 changes: 5 additions & 1 deletion config/default_config/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ defaultPush: true
# 是否随机Bot进行推送, 有多个Bot在同一群群时随机选择一个在线的Bot推送状态 (仅限TRSS)
randomBot: false

# 群统计是否过滤掉黑名单群和白名单群
# 如果关闭则每次会获取忽略黑白名单的所有群 但是不会推送 仅统计
statusFilterGroup: true

# 推送的Bot黑名单, 不开启推送的Bot将不会被推送, 比如腾讯QQBot限制主动消息
blackBotList:
- 3889000138
Expand All @@ -65,7 +69,7 @@ whiteBotList: []

# 推送黑名单群
blackGroupList:
- 123456
- 741577559

# 推送白名单群
whiteGroupList: []
15 changes: 15 additions & 0 deletions config/default_config/tips.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ recentPlayEmptyTips: "最近电子阳痿了"

# 愿望单为空或未公开时的提示语
wishListEmptyTips: "愿望当场就实现了, 羡慕"

# 在私聊中使用群聊限定的指令时的提示语
privateUseTips: "请在群聊中使用此功能~"

# 没有开启推送功能时的提示语
pushDisabledTips: "主人没有开启推送功能哦"

# 在推送黑名单中的提示语
blackGroupTips: "本群在推送黑名单中, 请联系主人解除~"

# 不在推送白名单中的提示语
noWhiteGroupTips: "本群没有在推送白名单中, 请联系主人添加~"

# 制作图片失败时的提示语
makeImageFailedTips: "制作图片出错辣!再试一次吧"
1 change: 0 additions & 1 deletion models/db/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export async function UserTableAddSteamIdByUserId (userId, steamId) {
}).then(result => result?.dataValues)
// 更换绑定为新的steamId
await UserTableBindSteamIdByUserId(userId, steamId, true)
// TODO: 是否默认开启推送 添加到推送表
return res
}

Expand Down
37 changes: 22 additions & 15 deletions models/setting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ export const cfgSchema = {
def: true,
desc: '是否推送下线'
},
time: {
title: '推送间隔',
key: '推送间隔',
def: 5,
min: 1,
type: 'number',
input: (n) => {
if (n >= 0) {
return n * 1
} else {
return 5
}
},
desc: '间隔多少分钟推送一次'
},
pushApi: {
title: '推送请求api',
key: '推送api',
Expand Down Expand Up @@ -160,6 +175,13 @@ export const cfgSchema = {
def: false,
desc: '有多个Bot在同一群群时随机选择一个在线的Bot推送状态 (仅限TRSS)'
},
statusFilterGroup: {
title: '统计过滤黑白名单',
key: '统计过滤群',
type: 'boolean',
def: true,
desc: '群统计是否过滤掉黑名单群和白名单群 关闭则每次都会获取所有群的状态'
},
blackBotList: {
title: '推送黑名单机器人',
key: '推送bot黑名单',
Expand Down Expand Up @@ -189,21 +211,6 @@ export const cfgSchema = {
type: 'array',
def: [],
desc: '只推送白名单群的状态'
},
time: {
title: '推送间隔',
key: '推送间隔',
def: 5,
min: 1,
type: 'number',
input: (n) => {
if (n >= 0) {
return n * 1
} else {
return 5
}
},
desc: '间隔多少分钟推送一次'
}
}
},
Expand Down
25 changes: 11 additions & 14 deletions models/task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import _ from 'lodash'

let timer = null

// TODO: 改成sqlite?
const redisKey = 'steam-plugin:user-play:'

export function startTimer () {
Expand All @@ -22,7 +21,7 @@ export function startTimer () {
// 获取现在的时间
const now = Math.floor(Date.now() / 1000)
// 从推送表中获取所有数据
const PushData = await db.PushTableGetAllData(true)
const PushData = await db.PushTableGetAllData(Config.push.statusFilterGroup)
// 所有的steamId
const steamIds = _.uniq(PushData.map(i => i.steamId))
// 获取所有steamId现在的状态
Expand Down Expand Up @@ -113,7 +112,7 @@ export function startTimer () {
desc: `已${utils.steam.getPersonaState(player.personastate)}`,
image: avatar || (Config.other.steamAvatar ? i.avatarfull : ''),
isAvatar: true,
descBgColor: getColor(player.personastate)
descBgColor: utils.steam.getStateColor(player.personastate)
})
}
}
Expand Down Expand Up @@ -165,11 +164,11 @@ export function startTimer () {
const path = Config.push.pushMode === 2 ? 'inventory/index' : 'game/game'
const img = await Render.render(path, { data })
if (typeof img !== 'string') {
await utils.bot.sendGroupMsg(botId, gid, img)
await sendGroupMsg(botId, gid, img)
}
} else {
for (const msg of data) {
await utils.bot.sendGroupMsg(botId, gid, msg)
await sendGroupMsg(botId, gid, msg)
}
}
}
Expand All @@ -180,14 +179,12 @@ export function startTimer () {
}, 1000 * 60 * Config.push.time)
}

// TODO:
function getColor (state) {
switch (Number(state)) {
case 1:
return '#beee11'
case 0:
return '#999999'
default:
return '#8fbc8b'
async function sendGroupMsg (bid, gid, msg) {
if (typeof msg === 'string') {
return
}
if (!Config.push.statusFilterGroup && !utils.bot.checkGroup(gid).success) {
return
}
await utils.bot.sendGroupMsg(bid, gid, msg)
}
35 changes: 35 additions & 0 deletions models/utils/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,38 @@ export async function makeForwardMsg (e, msg) {
return ''
}
}

/**
* 检查黑白名单 返回ture表示通过
* @param {string} gid
* @returns {{success: boolean, message?: string}}
*/
export function checkGroup (gid) {
if (!gid) {
return {
success: false,
message: Config.tips.privateUseTips
}
}
if (!Config.push.enable) {
return {
success: false,
message: Config.tips.pushDisabledTips
}
}
if (Config.push.whiteGroupList.length && !Config.push.whiteGroupList.some(id => id == gid)) {
return {
success: false,
message: Config.tips.noWhiteGroupTips
}
}
if (Config.push.blackGroupList.length && Config.push.blackGroupList.some(id => id == gid)) {
return {
success: false,
message: Config.tips.blackGroupTips
}
}
return {
success: true
}
}
4 changes: 3 additions & 1 deletion models/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export default async function request (url, options = {}, retry = { count: 0, ke
let { retKeys, retKey } = await getKey(retry.keys)
key = retKey
keys = retKeys
logger.info(`获取请求的key: ${key.slice(0, 5)}...${key.slice(-5)}`)
if (retKeys.length > 1) {
logger.info(`获取请求的key: ${key.slice(0, 5)}...${key.slice(-5)}`)
}
}
return await axios.request({
url,
Expand Down
16 changes: 16 additions & 0 deletions models/utils/steam.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ export async function getUserSummaries (steamIds) {
})
})
}

/**
* 获取状态对应的颜色
* @param {number} state
* @returns {string}
*/
export function getStateColor (state) {
switch (Number(state)) {
case 1:
return '#beee11'
case 0:
return '#999999'
default:
return '#8fbc8b'
}
}
3 changes: 0 additions & 3 deletions resources/setting/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63d0c05

Please sign in to comment.