Skip to content

Commit

Permalink
feat: steam设置推送模式
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Nov 24, 2024
1 parent fb1a52d commit f62da12
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const rule = {
}
},
unbind: {
reg: /^#?steam(?:解除?绑定?|unbind|取消绑定)\s*(\d+)$/i,
reg: /^#?steam(?:解除?绑定?|unbind|取消绑定)\s*(\d+)?$/i,
fnc: async e => {
const textId = rule.unbind.reg.exec(e.msg)[1]
if (!textId) {
Expand Down
11 changes: 8 additions & 3 deletions apps/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ export const rule = {
const open = e.msg.includes('开启')
// 如果附带了steamId
if (textId) {
const steamId = utils.getSteamId(textId)
let steamId = utils.getSteamId(textId)
const user = await db.UserTableGetDataBySteamId(steamId)
// 如果没有人绑定这个steamId则判断是否为主人,主人才能添加推送
if (((!user && e.isMaster) || (user && user.userId == e.user_id))) {
const uid = e.isMaster ? utils.getAtUid(_.isEmpty(e.at) ? e.user_id : e.at, '0') : e.user_id
if (uid != '0') {
const userBindAll = await db.UserTableGetDataByUserId(uid)
const index = Number(textId) <= userBindAll.length ? Number(textId) - 1 : -1
steamId = index >= 0 ? userBindAll[index].steamId : steamId
}
if (open) {
await db.PushTableAddData(uid, steamId, e.self_id, e.group_id)
await e.reply([uid == '0' ? '' : segment.at(uid), `已开启推送${steamId}${e.group_id}`])
Expand Down Expand Up @@ -124,8 +129,8 @@ export const rule = {
if (i.gameid) {
playing.push({
name: i.gameextrainfo,
appid: i.gameid,
desc: `${nickname}(${i.personaname})`
appid: nickname,
desc: i.personaname
})
} else {
notPlaying.push({
Expand Down
1 change: 1 addition & 0 deletions components/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Config {
* 获取推送配置
* @returns {{
* enable: boolean,
* pushMode: number,
* time: number,
* defaultPush: boolean,
* blackGroupList: string[],
Expand Down
5 changes: 5 additions & 0 deletions config/default_config/push.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 是否开启游玩推送功能
enable: true

# 推送模式
# 1: 文字推送 一条消息就是一个群友 xxx正在玩xxx
# 2: 图片推送 一张图片展示所有群友
pushMode: 1

# Steam Web API 使用条款
# https://steamcommunity.com/dev/apiterms
# 其中说明: 每天对 Steam Web API 的调用次数限制为十万 (100,000) 次
Expand Down
16 changes: 16 additions & 0 deletions models/setting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ export const cfgSchema = {
def: true,
desc: '是否默认开启推送, 绑定steamId后自动开启推送'
},
pushMode: {
title: '推送模式',
key: '推送模式',
type: 'number',
def: 1,
min: 1,
max: 2,
input: (n) => {
if (n >= 1 && n <= 2) {
return n * 1
} else {
return 1
}
},
desc: '推送模式 1: 文字推送 2: 图片推送'
},
blackGroupList: {
title: '推送黑名单群',
key: '推送黑名单',
Expand Down
112 changes: 87 additions & 25 deletions models/task/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, Version } from '#components'
import { Config, Version, Render } from '#components'
import { Bot, logger, redis, segment } from '#lib'
import { api, db, utils } from '#models'
import _ from 'lodash'
Expand All @@ -22,6 +22,7 @@ export function startTimer () {
const steamIds = _.uniq(PushData.map(i => i.steamId))
// 获取所有steamId现在的状态
const result = await api.ISteamUser.GetPlayerSummaries(steamIds)
const userList = {}
for (const player of result) {
// 获取上一次的状态
let lastPlay = await redis.get(redisKey + player.steamid)
Expand All @@ -35,7 +36,8 @@ export function startTimer () {
// 找到所有的推送群
const pushGroups = PushData.filter(i => i.steamId === player.steamid)
const iconUrl = utils.getHeaderImgUrlByAppid(player.gameid || lastPlay.appid)
const iconBuffer = await utils.getImgUrlBuffer(iconUrl)
// 文本推送才获取图片
const iconBuffer = Config.push.pushMode != 2 ? await utils.getImgUrlBuffer(iconUrl) : null
for (const i of pushGroups) {
if (Version.BotName === 'Karin') {
if (!Bot.getBot(i.botId)) {
Expand All @@ -48,38 +50,98 @@ export function startTimer () {
const nickname = i.userId == '0' ? player.personaname : await utils.getUserName(i.botId, i.userId, i.groupId)
const msg = []
iconBuffer && msg.push(segment.image(iconBuffer))
// 如果有gameid就是开始玩
if (player.gameid) {
logger.info(`${player.personaname} 正在玩 ${player.gameextrainfo}`)
msg.push(`${nickname}(${player.personaname}) 正在玩 ${player.gameextrainfo}`)
// 看看上次有没有在玩别的游戏
const state = {
name: player.gameextrainfo,
appid: player.gameid,
time: now,
state: player.personastate
}
if (Config.push.pushMode == 2) {
// 图片推送 先收集所有要推送的用户
if (!userList[i.groupId]) {
userList[i.groupId] = {}
}
if (!userList[i.groupId][i.botId]) {
userList[i.groupId][i.botId] = {
start: [],
end: []
}
}
if (player.gameid) {
userList[i.groupId][i.botId].start.push({
name: player.gameextrainfo,
appid: nickname,
desc: player.personaname,
header_image: iconUrl
})
redis.set(redisKey + player.steamid, JSON.stringify(state))
}
if (lastPlay.name) {
msg.push(`\n已结束游玩 ${lastPlay.name} 时长 ${utils.formatDuration(now - lastPlay.time)}`)
userList[i.groupId][i.botId].end.push({
name: lastPlay.name,
appid: nickname,
desc: player.personaname,
header_image: iconUrl
})
redis.del(redisKey + player.steamid)
}
// 记录这一次的状态
redis.set(redisKey + player.steamid, JSON.stringify({
name: player.gameextrainfo,
appid: player.gameid,
time: now,
state: player.personastate
}))
// 如果有上次记录就是结束游玩
} else if (lastPlay.name) {
msg.push(`${nickname}(${player.personaname}) 已结束游玩 ${lastPlay.name} 时长 ${utils.formatDuration(now - lastPlay.time)}`)
redis.del(redisKey + player.steamid)
} else {
continue
}
try {
await utils.sendGroupMsg(i.botId, i.groupId, msg)
} catch (error) {
logger.error(`群消息发送失败: ${i.groupId}`, error)
// 如果有gameid就是开始玩
if (player.gameid) {
msg.push(`${nickname}(${player.personaname}) 正在玩 ${player.gameextrainfo}`)
// 看看上次有没有在玩别的游戏
if (lastPlay.name) {
msg.push(`\n已结束游玩 ${lastPlay.name} 时长 ${utils.formatDuration(now - lastPlay.time)}`)
}
// 记录这一次的状态
redis.set(redisKey + player.steamid, JSON.stringify(state))
// 如果有上次记录就是结束游玩
} else if (lastPlay.name) {
msg.push(`${nickname}(${player.personaname}) 已结束游玩 ${lastPlay.name} 时长 ${utils.formatDuration(now - lastPlay.time)}`)
redis.del(redisKey + player.steamid)
} else {
continue
}
try {
await utils.sendGroupMsg(i.botId, i.groupId, msg)
} catch (error) {
logger.error(`群消息发送失败: ${i.groupId}`, error)
}
}
}
} else {
// TODO: 上下线推送
}
}
for (const gid in userList) {
for (const botId in userList[gid]) {
const i = userList[gid][botId]
const data = []
if (i.start.length) {
data.push({
title: '开始玩游戏的群友',
games: i.start,
column: 3
})
}
if (i.end.length) {
data.push({
title: '结束玩游戏的群友',
games: i.end,
column: 3
})
}
if (!data.length) {
continue
}
try {
const img = await Render.render('inventory/index', { data })
await utils.sendGroupMsg(botId, gid, img)
} catch (error) {
logger.error(`群消息发送失败: ${gid}`, error)
}
}
}
} catch (error) {
logger.error('检查Steam游戏信息出现错误', error)
}
Expand Down

0 comments on commit f62da12

Please sign in to comment.