mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
🎨 改进代码样式 (#625)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
f1dba97922
commit
107149892c
@ -1,134 +1,134 @@
|
|||||||
package steam
|
package steam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FloatTech/floatbox/binary"
|
"github.com/FloatTech/floatbox/binary"
|
||||||
"github.com/FloatTech/floatbox/web"
|
"github.com/FloatTech/floatbox/web"
|
||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ----------------------- 远程调用 ----------------------
|
// ----------------------- 远程调用 ----------------------
|
||||||
const (
|
const (
|
||||||
URL = "https://api.steampowered.com/" // steam API 调用地址
|
URL = "https://api.steampowered.com/" // steam API 调用地址
|
||||||
StatusURL = "ISteamUser/GetPlayerSummaries/v2/?key=%+v&steamids=%+v" // 根据用户steamID获取用户状态
|
StatusURL = "ISteamUser/GetPlayerSummaries/v2/?key=%+v&steamids=%+v" // 根据用户steamID获取用户状态
|
||||||
steamapikeygid = 3
|
steamapikeygid = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var apiKey string
|
var apiKey string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
engine.OnRegex(`^steam绑定\s*api\s*key\s*(.*)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnRegex(`^steam绑定\s*api\s*key\s*(.*)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
apiKey = ctx.State["regex_matched"].([]string)[1]
|
apiKey = ctx.State["regex_matched"].([]string)[1]
|
||||||
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
_ = m.Manager.Response(steamapikeygid)
|
_ = m.Manager.Response(steamapikeygid)
|
||||||
err := m.Manager.SetExtra(steamapikeygid, apiKey)
|
err := m.Manager.SetExtra(steamapikeygid, apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: 保存apikey失败!"))
|
ctx.SendChain(message.Text("[steam] ERROR: 保存apikey失败!"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Text("保存apikey成功!"))
|
ctx.SendChain(message.Text("保存apikey成功!"))
|
||||||
})
|
})
|
||||||
engine.OnFullMatch("查看apikey", zero.OnlyPrivate, zero.SuperUserPermission, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnFullMatch("查看apikey", zero.OnlyPrivate, zero.SuperUserPermission, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
ctx.SendChain(message.Text("apikey为: ", apiKey))
|
ctx.SendChain(message.Text("apikey为: ", apiKey))
|
||||||
})
|
})
|
||||||
engine.OnFullMatch("拉取steam订阅", getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnFullMatch("拉取steam订阅", getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
su := zero.BotConfig.SuperUsers[0]
|
su := zero.BotConfig.SuperUsers[0]
|
||||||
// 获取所有处于监听状态的用户信息
|
// 获取所有处于监听状态的用户信息
|
||||||
infos, err := database.findAll()
|
infos, err := database.findAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 挂了就给管理员发消息
|
// 挂了就给管理员发消息
|
||||||
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err))
|
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 收集这波用户的streamId,然后查当前的状态,并建立信息映射表
|
// 收集这波用户的streamId,然后查当前的状态,并建立信息映射表
|
||||||
streamIds := make([]string, len(infos))
|
streamIds := make([]string, len(infos))
|
||||||
localPlayerMap := make(map[int64]*player)
|
localPlayerMap := make(map[int64]*player)
|
||||||
for i, info := range infos {
|
for i, info := range infos {
|
||||||
streamIds[i] = strconv.FormatInt(info.SteamID, 10)
|
streamIds[i] = strconv.FormatInt(info.SteamID, 10)
|
||||||
localPlayerMap[info.SteamID] = info
|
localPlayerMap[info.SteamID] = info
|
||||||
}
|
}
|
||||||
// 将所有用户状态查一遍
|
// 将所有用户状态查一遍
|
||||||
playerStatus, err := getPlayerStatus(streamIds...)
|
playerStatus, err := getPlayerStatus(streamIds...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 出错就发消息
|
// 出错就发消息
|
||||||
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err))
|
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 遍历返回的信息做对比,假如信息有变化则发消息
|
// 遍历返回的信息做对比,假如信息有变化则发消息
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
msg := make(message.Message, 0, len(playerStatus))
|
msg := make(message.Message, 0, len(playerStatus))
|
||||||
for _, playerInfo := range playerStatus {
|
for _, playerInfo := range playerStatus {
|
||||||
msg = msg[:0]
|
msg = msg[:0]
|
||||||
localInfo := localPlayerMap[playerInfo.SteamID]
|
localInfo := localPlayerMap[playerInfo.SteamID]
|
||||||
// 排除不需要处理的情况
|
// 排除不需要处理的情况
|
||||||
if localInfo.GameID == 0 && playerInfo.GameID == 0 {
|
if localInfo.GameID == 0 && playerInfo.GameID == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// 打开游戏
|
// 打开游戏
|
||||||
if localInfo.GameID == 0 && playerInfo.GameID != 0 {
|
if localInfo.GameID == 0 && playerInfo.GameID != 0 {
|
||||||
msg = append(msg, message.Text(playerInfo.PersonaName, "正在玩", playerInfo.GameExtraInfo))
|
msg = append(msg, message.Text(playerInfo.PersonaName, "正在玩", playerInfo.GameExtraInfo))
|
||||||
localInfo.LastUpdate = now.Unix()
|
localInfo.LastUpdate = now.Unix()
|
||||||
}
|
}
|
||||||
// 更换游戏
|
// 更换游戏
|
||||||
if localInfo.GameID != 0 && playerInfo.GameID != localInfo.GameID && playerInfo.GameID != 0 {
|
if localInfo.GameID != 0 && playerInfo.GameID != localInfo.GameID && playerInfo.GameID != 0 {
|
||||||
msg = append(msg, message.Text(playerInfo.PersonaName, "玩了", (now.Unix()-localInfo.LastUpdate)/60, "分钟后, 丢下了", localInfo.GameExtraInfo, ", 转头去玩", playerInfo.GameExtraInfo))
|
msg = append(msg, message.Text(playerInfo.PersonaName, "玩了", (now.Unix()-localInfo.LastUpdate)/60, "分钟后, 丢下了", localInfo.GameExtraInfo, ", 转头去玩", playerInfo.GameExtraInfo))
|
||||||
localInfo.LastUpdate = now.Unix()
|
localInfo.LastUpdate = now.Unix()
|
||||||
}
|
}
|
||||||
// 关闭游戏
|
// 关闭游戏
|
||||||
if playerInfo.GameID != localInfo.GameID && playerInfo.GameID == 0 {
|
if playerInfo.GameID != localInfo.GameID && playerInfo.GameID == 0 {
|
||||||
msg = append(msg, message.Text(playerInfo.PersonaName, "玩了", (now.Unix()-localInfo.LastUpdate)/60, "分钟后, 关掉了", localInfo.GameExtraInfo))
|
msg = append(msg, message.Text(playerInfo.PersonaName, "玩了", (now.Unix()-localInfo.LastUpdate)/60, "分钟后, 关掉了", localInfo.GameExtraInfo))
|
||||||
localInfo.LastUpdate = 0
|
localInfo.LastUpdate = 0
|
||||||
}
|
}
|
||||||
if len(msg) != 0 {
|
if len(msg) != 0 {
|
||||||
groups := strings.Split(localInfo.Target, ",")
|
groups := strings.Split(localInfo.Target, ",")
|
||||||
for _, groupString := range groups {
|
for _, groupString := range groups {
|
||||||
group, err := strconv.ParseInt(groupString, 10, 64)
|
group, err := strconv.ParseInt(groupString, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err, "\nOTHER: SteamID ", localInfo.SteamID))
|
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err, "\nOTHER: SteamID ", localInfo.SteamID))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ctx.SendGroupMessage(group, msg)
|
ctx.SendGroupMessage(group, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 更新数据
|
// 更新数据
|
||||||
localInfo.GameID = playerInfo.GameID
|
localInfo.GameID = playerInfo.GameID
|
||||||
localInfo.GameExtraInfo = playerInfo.GameExtraInfo
|
localInfo.GameExtraInfo = playerInfo.GameExtraInfo
|
||||||
if err = database.update(localInfo); err != nil {
|
if err = database.update(localInfo); err != nil {
|
||||||
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err, "\nEXP: 更新数据失败\nOTHER: SteamID ", localInfo.SteamID))
|
ctx.SendPrivateMessage(su, message.Text("[steam] ERROR: ", err, "\nEXP: 更新数据失败\nOTHER: SteamID ", localInfo.SteamID))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPlayerStatus 获取用户状态
|
// getPlayerStatus 获取用户状态
|
||||||
func getPlayerStatus(streamIds ...string) ([]*player, error) {
|
func getPlayerStatus(streamIds ...string) ([]*player, error) {
|
||||||
players := make([]*player, 0)
|
players := make([]*player, 0)
|
||||||
// 拼接请求地址
|
// 拼接请求地址
|
||||||
url := fmt.Sprintf(URL+StatusURL, apiKey, strings.Join(streamIds, ","))
|
url := fmt.Sprintf(URL+StatusURL, apiKey, strings.Join(streamIds, ","))
|
||||||
// 拉取并解析数据
|
// 拉取并解析数据
|
||||||
data, err := web.GetData(url)
|
data, err := web.GetData(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return players, err
|
return players, err
|
||||||
}
|
}
|
||||||
dataStr := binary.BytesToString(data)
|
dataStr := binary.BytesToString(data)
|
||||||
index := gjson.Get(dataStr, "response.players.#").Uint()
|
index := gjson.Get(dataStr, "response.players.#").Uint()
|
||||||
for i := uint64(0); i < index; i++ {
|
for i := uint64(0); i < index; i++ {
|
||||||
players = append(players, &player{
|
players = append(players, &player{
|
||||||
SteamID: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.steamid", i)).Int(),
|
SteamID: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.steamid", i)).Int(),
|
||||||
PersonaName: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.personaname", i)).String(),
|
PersonaName: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.personaname", i)).String(),
|
||||||
GameID: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.gameid", i)).Int(),
|
GameID: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.gameid", i)).Int(),
|
||||||
GameExtraInfo: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.gameextrainfo", i)).String(),
|
GameExtraInfo: gjson.Get(dataStr, fmt.Sprintf("response.players.%d.gameextrainfo", i)).String(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return players, nil
|
return players, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,158 +1,158 @@
|
|||||||
// Package steam 获取steam用户状态
|
// Package steam 获取steam用户状态
|
||||||
package steam
|
package steam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FloatTech/floatbox/binary"
|
"github.com/FloatTech/floatbox/binary"
|
||||||
"github.com/FloatTech/floatbox/math"
|
"github.com/FloatTech/floatbox/math"
|
||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
"github.com/FloatTech/zbputils/control"
|
"github.com/FloatTech/zbputils/control"
|
||||||
"github.com/FloatTech/zbputils/ctxext"
|
"github.com/FloatTech/zbputils/ctxext"
|
||||||
"github.com/FloatTech/zbputils/img/text"
|
"github.com/FloatTech/zbputils/img/text"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
engine = control.Register("steam", &ctrl.Options[*zero.Ctx]{
|
engine = control.Register("steam", &ctrl.Options[*zero.Ctx]{
|
||||||
DisableOnDefault: false,
|
DisableOnDefault: false,
|
||||||
Brief: "steam相关插件",
|
Brief: "steam相关插件",
|
||||||
Help: "- steam添加订阅 xxxxxxx (可输入需要绑定的 steamid)\n" +
|
Help: "- steam添加订阅 xxxxxxx (可输入需要绑定的 steamid)\n" +
|
||||||
"- steam删除订阅 xxxxxxx (删除你创建的对于 steamid 的绑定)\n" +
|
"- steam删除订阅 xxxxxxx (删除你创建的对于 steamid 的绑定)\n" +
|
||||||
"- steam查询订阅 (查询本群内所有的绑定对象)\n" +
|
"- steam查询订阅 (查询本群内所有的绑定对象)\n" +
|
||||||
"-----------------------\n" +
|
"-----------------------\n" +
|
||||||
"- steam绑定 api key xxxxxxx (密钥在steam网站申请, 申请地址: https://steamcommunity.com/dev/registerkey)\n" +
|
"- steam绑定 api key xxxxxxx (密钥在steam网站申请, 申请地址: https://steamcommunity.com/dev/registerkey)\n" +
|
||||||
"- 查看apikey (查询已经绑定的密钥)\n" +
|
"- 查看apikey (查询已经绑定的密钥)\n" +
|
||||||
"- 拉取steam订阅 (使用插件定时任务开始)\n" +
|
"- 拉取steam订阅 (使用插件定时任务开始)\n" +
|
||||||
"-----------------------\n" +
|
"-----------------------\n" +
|
||||||
"Tips: steamID在用户资料页的链接上面, 形如7656119820673xxxx\n" +
|
"Tips: steamID在用户资料页的链接上面, 形如7656119820673xxxx\n" +
|
||||||
"需要先私聊绑定apikey, 订阅用户之后使用job插件设置定时, 例: \n" +
|
"需要先私聊绑定apikey, 订阅用户之后使用job插件设置定时, 例: \n" +
|
||||||
"记录在\"@every 1m\"触发的指令\n" +
|
"记录在\"@every 1m\"触发的指令\n" +
|
||||||
"拉取steam订阅",
|
"拉取steam订阅",
|
||||||
PrivateDataFolder: "steam",
|
PrivateDataFolder: "steam",
|
||||||
}).ApplySingle(ctxext.DefaultSingle)
|
}).ApplySingle(ctxext.DefaultSingle)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// 创建绑定流程
|
// 创建绑定流程
|
||||||
engine.OnRegex(`^steam添加订阅\s*(\d+)$`, zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnRegex(`^steam添加订阅\s*(\d+)$`, zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
steamidstr := ctx.State["regex_matched"].([]string)[1]
|
steamidstr := ctx.State["regex_matched"].([]string)[1]
|
||||||
steamID := math.Str2Int64(steamidstr)
|
steamID := math.Str2Int64(steamidstr)
|
||||||
// 获取用户状态
|
// 获取用户状态
|
||||||
playerStatus, err := getPlayerStatus(steamidstr)
|
playerStatus, err := getPlayerStatus(steamidstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 添加失败, 获取用户信息错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 添加失败, 获取用户信息错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(playerStatus) == 0 {
|
if len(playerStatus) == 0 {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: 需要添加的用户不存在, 请检查id或url"))
|
ctx.SendChain(message.Text("[steam] ERROR: 需要添加的用户不存在, 请检查id或url"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
playerData := playerStatus[0]
|
playerData := playerStatus[0]
|
||||||
// 判断用户是否已经初始化:若未初始化,通过用户的steamID获取当前状态并初始化;若已经初始化则更新用户信息
|
// 判断用户是否已经初始化:若未初始化,通过用户的steamID获取当前状态并初始化;若已经初始化则更新用户信息
|
||||||
info, err := database.find(steamID)
|
info, err := database.find(steamID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 添加失败,数据库错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 添加失败,数据库错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 处理数据
|
// 处理数据
|
||||||
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
||||||
if info.Target == "" {
|
if info.Target == "" {
|
||||||
info = player{
|
info = player{
|
||||||
SteamID: steamID,
|
SteamID: steamID,
|
||||||
PersonaName: playerData.PersonaName,
|
PersonaName: playerData.PersonaName,
|
||||||
Target: groupID,
|
Target: groupID,
|
||||||
GameID: playerData.GameID,
|
GameID: playerData.GameID,
|
||||||
GameExtraInfo: playerData.GameExtraInfo,
|
GameExtraInfo: playerData.GameExtraInfo,
|
||||||
LastUpdate: time.Now().Unix(),
|
LastUpdate: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
} else if !strings.Contains(info.Target, groupID) {
|
} else if !strings.Contains(info.Target, groupID) {
|
||||||
info.Target = strings.Join([]string{info.Target, groupID}, ",")
|
info.Target = strings.Join([]string{info.Target, groupID}, ",")
|
||||||
}
|
}
|
||||||
// 更新数据库
|
// 更新数据库
|
||||||
if err = database.update(&info); err != nil {
|
if err = database.update(&info); err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 更新数据库失败"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 更新数据库失败"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Text("添加成功"))
|
ctx.SendChain(message.Text("添加成功"))
|
||||||
})
|
})
|
||||||
// 删除绑定流程
|
// 删除绑定流程
|
||||||
engine.OnRegex(`^steam删除订阅\s*(\d+)$`, zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnRegex(`^steam删除订阅\s*(\d+)$`, zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
steamID := math.Str2Int64(ctx.State["regex_matched"].([]string)[1])
|
steamID := math.Str2Int64(ctx.State["regex_matched"].([]string)[1])
|
||||||
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
||||||
// 判断是否已经绑定该steamID,若已绑定就将群列表从推送群列表钟去除
|
// 判断是否已经绑定该steamID,若已绑定就将群列表从推送群列表钟去除
|
||||||
info, err := database.findWithGroupID(steamID, groupID)
|
info, err := database.findWithGroupID(steamID, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if info.SteamID == 0 {
|
if info.SteamID == 0 {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: 所需要删除的用户不存在。"))
|
ctx.SendChain(message.Text("[steam] ERROR: 所需要删除的用户不存在。"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 从绑定列表中剔除需要删除的对象
|
// 从绑定列表中剔除需要删除的对象
|
||||||
targets := strings.Split(info.Target, ",")
|
targets := strings.Split(info.Target, ",")
|
||||||
newTargets := make([]string, 0)
|
newTargets := make([]string, 0)
|
||||||
for _, target := range targets {
|
for _, target := range targets {
|
||||||
if target == groupID {
|
if target == groupID {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newTargets = append(newTargets, target)
|
newTargets = append(newTargets, target)
|
||||||
}
|
}
|
||||||
if len(newTargets) == 0 {
|
if len(newTargets) == 0 {
|
||||||
if err = database.del(steamID); err != nil {
|
if err = database.del(steamID); err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
info.Target = strings.Join(newTargets, ",")
|
info.Target = strings.Join(newTargets, ",")
|
||||||
if err = database.update(&info); err != nil {
|
if err = database.update(&info); err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 删除失败,数据库错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Text("删除成功"))
|
ctx.SendChain(message.Text("删除成功"))
|
||||||
})
|
})
|
||||||
// 查询当前群绑定信息
|
// 查询当前群绑定信息
|
||||||
engine.OnFullMatch("steam查询订阅", zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
engine.OnFullMatch("steam查询订阅", zero.OnlyGroup, getDB).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
// 获取群信息
|
// 获取群信息
|
||||||
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
groupID := strconv.FormatInt(ctx.Event.GroupID, 10)
|
||||||
// 获取所有绑定信息
|
// 获取所有绑定信息
|
||||||
infos, err := database.findAll()
|
infos, err := database.findAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 查询订阅失败, 数据库错误"))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err, "\nEXP: 查询订阅失败, 数据库错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: 还未订阅过用户关系!"))
|
ctx.SendChain(message.Text("[steam] ERROR: 还未订阅过用户关系!"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 遍历所有信息,如果包含该群就收集对应的steamID
|
// 遍历所有信息,如果包含该群就收集对应的steamID
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
head := " 查询steam订阅成功, 该群订阅的用户有: \n"
|
head := " 查询steam订阅成功, 该群订阅的用户有: \n"
|
||||||
sb.WriteString(head)
|
sb.WriteString(head)
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
if strings.Contains(info.Target, groupID) {
|
if strings.Contains(info.Target, groupID) {
|
||||||
sb.WriteString(" ")
|
sb.WriteString(" ")
|
||||||
sb.WriteString(info.PersonaName)
|
sb.WriteString(info.PersonaName)
|
||||||
sb.WriteString(":")
|
sb.WriteString(":")
|
||||||
sb.WriteString(strconv.FormatInt(info.SteamID, 10))
|
sb.WriteString(strconv.FormatInt(info.SteamID, 10))
|
||||||
sb.WriteString("\n")
|
sb.WriteString("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if sb.String() == head {
|
if sb.String() == head {
|
||||||
ctx.SendChain(message.Text("查询成功,该群暂时还没有被绑定的用户!"))
|
ctx.SendChain(message.Text("查询成功,该群暂时还没有被绑定的用户!"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 组装并返回结果
|
// 组装并返回结果
|
||||||
data, err := text.RenderToBase64(sb.String(), text.FontFile, 400, 18)
|
data, err := text.RenderToBase64(sb.String(), text.FontFile, 400, 18)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Image("base64://" + binary.BytesToString(data)))
|
ctx.SendChain(message.Image("base64://" + binary.BytesToString(data)))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,117 +1,117 @@
|
|||||||
package steam
|
package steam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
fcext "github.com/FloatTech/floatbox/ctxext"
|
fcext "github.com/FloatTech/floatbox/ctxext"
|
||||||
sql "github.com/FloatTech/sqlite"
|
sql "github.com/FloatTech/sqlite"
|
||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
database streamDB
|
database streamDB
|
||||||
// 开启并检查数据库链接
|
// 开启并检查数据库链接
|
||||||
getDB = fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
|
getDB = fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
|
||||||
database.db.DBPath = engine.DataFolder() + "steam.db"
|
database.db.DBPath = engine.DataFolder() + "steam.db"
|
||||||
err := database.db.Open(time.Hour * 24)
|
err := database.db.Open(time.Hour * 24)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if err = database.db.Create(TableListenPlayer, &player{}); err != nil {
|
if err = database.db.Create(TableListenPlayer, &player{}); err != nil {
|
||||||
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
ctx.SendChain(message.Text("[steam] ERROR: ", err))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// 校验密钥是否初始化
|
// 校验密钥是否初始化
|
||||||
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
_ = m.Manager.Response(steamapikeygid)
|
_ = m.Manager.Response(steamapikeygid)
|
||||||
_ = m.Manager.GetExtra(steamapikeygid, &apiKey)
|
_ = m.Manager.GetExtra(steamapikeygid, &apiKey)
|
||||||
if apiKey == "" {
|
if apiKey == "" {
|
||||||
ctx.SendChain(message.Text("ERROR: 未设置steam apikey"))
|
ctx.SendChain(message.Text("ERROR: 未设置steam apikey"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
// streamDB 继承方法的存储结构
|
// streamDB 继承方法的存储结构
|
||||||
type streamDB struct {
|
type streamDB struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
db sql.Sqlite
|
db sql.Sqlite
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// TableListenPlayer 存储查询用户信息
|
// TableListenPlayer 存储查询用户信息
|
||||||
TableListenPlayer = "listen_player"
|
TableListenPlayer = "listen_player"
|
||||||
)
|
)
|
||||||
|
|
||||||
// player 用户状态存储结构体
|
// player 用户状态存储结构体
|
||||||
type player struct {
|
type player struct {
|
||||||
SteamID int64 `json:"steam_id"` // 绑定用户标识ID
|
SteamID int64 `json:"steam_id"` // 绑定用户标识ID
|
||||||
PersonaName string `json:"persona_name"` // 用户昵称
|
PersonaName string `json:"persona_name"` // 用户昵称
|
||||||
Target string `json:"target"` // 信息推送群组
|
Target string `json:"target"` // 信息推送群组
|
||||||
GameID int64 `json:"game_id"` // 游戏ID
|
GameID int64 `json:"game_id"` // 游戏ID
|
||||||
GameExtraInfo string `json:"game_extra_info"` // 游戏信息
|
GameExtraInfo string `json:"game_extra_info"` // 游戏信息
|
||||||
LastUpdate int64 `json:"last_update"` // 更新时间
|
LastUpdate int64 `json:"last_update"` // 更新时间
|
||||||
}
|
}
|
||||||
|
|
||||||
// update 如果主键不存在则插入一条新的数据,如果主键存在直接复写
|
// update 如果主键不存在则插入一条新的数据,如果主键存在直接复写
|
||||||
func (sql *streamDB) update(dbInfo *player) error {
|
func (sql *streamDB) update(dbInfo *player) error {
|
||||||
sql.Lock()
|
sql.Lock()
|
||||||
defer sql.Unlock()
|
defer sql.Unlock()
|
||||||
return sql.db.Insert(TableListenPlayer, dbInfo)
|
return sql.db.Insert(TableListenPlayer, dbInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// find 根据主键查信息
|
// find 根据主键查信息
|
||||||
func (sql *streamDB) find(steamID int64) (dbInfo player, err error) {
|
func (sql *streamDB) find(steamID int64) (dbInfo player, err error) {
|
||||||
sql.Lock()
|
sql.Lock()
|
||||||
defer sql.Unlock()
|
defer sql.Unlock()
|
||||||
condition := "where steam_id = " + strconv.FormatInt(steamID, 10)
|
condition := "where steam_id = " + strconv.FormatInt(steamID, 10)
|
||||||
if !sql.db.CanFind(TableListenPlayer, condition) {
|
if !sql.db.CanFind(TableListenPlayer, condition) {
|
||||||
return player{}, nil // 规避没有该用户数据的报错
|
return player{}, nil // 规避没有该用户数据的报错
|
||||||
}
|
}
|
||||||
err = sql.db.Find(TableListenPlayer, &dbInfo, condition)
|
err = sql.db.Find(TableListenPlayer, &dbInfo, condition)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// findWithGroupID 根据用户steamID和groupID查询信息
|
// findWithGroupID 根据用户steamID和groupID查询信息
|
||||||
func (sql *streamDB) findWithGroupID(steamID int64, groupID string) (dbInfo player, err error) {
|
func (sql *streamDB) findWithGroupID(steamID int64, groupID string) (dbInfo player, err error) {
|
||||||
sql.Lock()
|
sql.Lock()
|
||||||
defer sql.Unlock()
|
defer sql.Unlock()
|
||||||
condition := "where steam_id = " + strconv.FormatInt(steamID, 10) + " AND target LIKE '%" + groupID + "%'"
|
condition := "where steam_id = " + strconv.FormatInt(steamID, 10) + " AND target LIKE '%" + groupID + "%'"
|
||||||
if !sql.db.CanFind(TableListenPlayer, condition) {
|
if !sql.db.CanFind(TableListenPlayer, condition) {
|
||||||
return player{}, nil // 规避没有该用户数据的报错
|
return player{}, nil // 规避没有该用户数据的报错
|
||||||
}
|
}
|
||||||
err = sql.db.Find(TableListenPlayer, &dbInfo, condition)
|
err = sql.db.Find(TableListenPlayer, &dbInfo, condition)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// findAll 查询所有库信息
|
// findAll 查询所有库信息
|
||||||
func (sql *streamDB) findAll() (dbInfos []*player, err error) {
|
func (sql *streamDB) findAll() (dbInfos []*player, err error) {
|
||||||
sql.Lock()
|
sql.Lock()
|
||||||
defer sql.Unlock()
|
defer sql.Unlock()
|
||||||
var info player
|
var info player
|
||||||
num, err := sql.db.Count(TableListenPlayer)
|
num, err := sql.db.Count(TableListenPlayer)
|
||||||
if err != nil || num == 0 {
|
if err != nil || num == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dbInfos = make([]*player, 0, num)
|
dbInfos = make([]*player, 0, num)
|
||||||
err = sql.db.FindFor(TableListenPlayer, &info, "", func() error {
|
err = sql.db.FindFor(TableListenPlayer, &info, "", func() error {
|
||||||
if info.SteamID != 0 {
|
if info.SteamID != 0 {
|
||||||
dbInfos = append(dbInfos, &info)
|
dbInfos = append(dbInfos, &info)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// del 删除指定数据
|
// del 删除指定数据
|
||||||
func (sql *streamDB) del(steamID int64) error {
|
func (sql *streamDB) del(steamID int64) error {
|
||||||
sql.Lock()
|
sql.Lock()
|
||||||
defer sql.Unlock()
|
defer sql.Unlock()
|
||||||
return sql.db.Del(TableListenPlayer, "where steam_id = "+strconv.FormatInt(steamID, 10))
|
return sql.db.Del(TableListenPlayer, "where steam_id = "+strconv.FormatInt(steamID, 10))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user