🆕 新增指令插件,新增雷神加速器操作指令
This commit is contained in:
84
plugin/plugins/command.go
Normal file
84
plugin/plugins/command.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"go-wechat/plugin"
|
||||
"go-wechat/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Command
|
||||
// @description: 自定义指令
|
||||
// @param m
|
||||
func Command(m *plugin.MessageContext) {
|
||||
// 判断是不是指令
|
||||
if !strings.HasPrefix(m.Content, "/") {
|
||||
return
|
||||
}
|
||||
|
||||
// 用空格分割消息,下标0表示指令
|
||||
msgArray := strings.Split(m.Content, " ")
|
||||
cmd := msgArray[0]
|
||||
|
||||
switch cmd {
|
||||
case "/帮助", "/h", "/help", "/?", "/?":
|
||||
helpCmd(m)
|
||||
case "/ls", "/雷神":
|
||||
leiGodCmd(m.FromUser, msgArray[1], msgArray[2:]...)
|
||||
default:
|
||||
utils.SendMessage(m.FromUser, m.GroupUser, "指令错误", 0)
|
||||
}
|
||||
|
||||
// 中止后续消息处理
|
||||
m.Abort()
|
||||
}
|
||||
|
||||
// helpCmd
|
||||
// @description: 帮助指令
|
||||
func helpCmd(m *plugin.MessageContext) {
|
||||
str := `帮助菜单:
|
||||
指令消息必须以'/'开头,比如: '/帮助'。
|
||||
支持的指令:
|
||||
|
||||
#1. 雷神加速器
|
||||
/ls option args
|
||||
option: 指令选项,可选值:
|
||||
绑定账户:'绑定'、'b',参数: 账户名 密码 [-f],-f表示强制绑定,非必传项
|
||||
详情: '详情'、'i'
|
||||
暂停: '暂停'、'p'
|
||||
示例: 绑定:
|
||||
/ls 绑定 123456 123456 或者 /ls b 123456 123456
|
||||
`
|
||||
utils.SendMessage(m.FromUser, m.GroupUser, str, 0)
|
||||
|
||||
}
|
||||
|
||||
// leiGodCmd
|
||||
// @description: 雷神加速器指令
|
||||
// @param userId
|
||||
// @param cmd
|
||||
// @param args
|
||||
// @return string
|
||||
func leiGodCmd(userId, cmd string, args ...string) {
|
||||
lg := newLeiGod(userId)
|
||||
|
||||
var replyMsg string
|
||||
switch cmd {
|
||||
case "绑定", "b":
|
||||
var force bool
|
||||
if len(args) == 3 && args[2] == "-f" {
|
||||
force = true
|
||||
}
|
||||
replyMsg = lg.binding(args[0], args[1], force)
|
||||
case "详情", "i":
|
||||
replyMsg = lg.info()
|
||||
case "暂停", "p":
|
||||
replyMsg = lg.pause()
|
||||
default:
|
||||
replyMsg = "指令错误"
|
||||
}
|
||||
|
||||
// 返回消息
|
||||
if strings.TrimSpace(replyMsg) != "" {
|
||||
utils.SendMessage(userId, "", replyMsg, 0)
|
||||
}
|
||||
}
|
||||
173
plugin/plugins/leigod.go
Normal file
173
plugin/plugins/leigod.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-wechat/client"
|
||||
"go-wechat/entity"
|
||||
"go-wechat/model"
|
||||
"go-wechat/utils"
|
||||
"go-wechat/vo"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
)
|
||||
|
||||
// leiGod
|
||||
// @description: 雷神加速器相关接口
|
||||
type leiGodI interface {
|
||||
binding(string, string, bool) string // 绑定雷神加速器账号
|
||||
info() string // 账户详情
|
||||
pause() string // 暂停加速
|
||||
}
|
||||
|
||||
type leiGod struct {
|
||||
userId string // 用户Id
|
||||
}
|
||||
|
||||
// newLeiGod
|
||||
// @description: 创建一个雷神加速器实例
|
||||
// @param userId
|
||||
// @return leiGodI
|
||||
func newLeiGod(userId string) leiGodI {
|
||||
return &leiGod{userId: userId}
|
||||
}
|
||||
|
||||
// binding
|
||||
// @description: 绑定雷神加速器账号
|
||||
// @receiver l
|
||||
// @param account
|
||||
// @param password
|
||||
// @param force
|
||||
// @return flag
|
||||
func (l leiGod) binding(account, password string, force bool) (replyMsg string) {
|
||||
log.Printf("用户[%s]绑定雷神加速器账号[%s] -> %s", l.userId, account, password)
|
||||
|
||||
// 取出已绑定的账号
|
||||
var data entity.PluginData
|
||||
client.MySQL.Where("user_id = ?", l.userId).Where("plugin_code = 'leigod'").First(&data)
|
||||
|
||||
var ac vo.LeiGodAccount
|
||||
if data.UserId != "" {
|
||||
if err := json.Unmarshal([]byte(data.Data), &ac); err != nil {
|
||||
log.Printf("用户[%s]已绑定雷神账号解析失败: %v", l.userId, err)
|
||||
return
|
||||
}
|
||||
log.Printf("用户[%s]已绑定账号[%s]", l.userId, ac.Account)
|
||||
}
|
||||
|
||||
// 如果已经绑定账号,且不是强制绑定,则返回
|
||||
if ac.Account != "" && !force {
|
||||
replyMsg = "您已绑定账号[" + ac.Account + "],如需更换请使用 -f 参数: \n/雷神 绑定 账号 密码 -f"
|
||||
return
|
||||
}
|
||||
|
||||
accountStr := fmt.Sprintf("{\"account\": \"%s\", \"password\":\"%s\"}", account, password)
|
||||
|
||||
// 绑定账号
|
||||
var err error
|
||||
if data.UserId != "" {
|
||||
// 修改
|
||||
err = client.MySQL.Model(&data).
|
||||
Where("user_id = ?", l.userId).
|
||||
Where("plugin_code = 'leigod'").
|
||||
Update("data", accountStr).Error
|
||||
} else {
|
||||
// 新增
|
||||
data = entity.PluginData{
|
||||
UserId: l.userId,
|
||||
PluginCode: "leigod",
|
||||
Data: accountStr,
|
||||
}
|
||||
err = client.MySQL.Create(&data).Error
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("用户[%s]绑定雷神账号失败: %v", l.userId, err)
|
||||
replyMsg = "绑定失败: " + err.Error()
|
||||
} else {
|
||||
replyMsg = "绑定成功"
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// info
|
||||
// @description: 账户详情
|
||||
// @receiver l
|
||||
// @return replyMsg
|
||||
func (l leiGod) info() (replyMsg string) {
|
||||
log.Printf("用户[%s]获取雷神账户详情", l.userId)
|
||||
|
||||
// 取出已绑定的账号
|
||||
var data entity.PluginData
|
||||
err := client.MySQL.Where("user_id = ?", l.userId).Where("plugin_code = 'leigod'").First(&data).Error
|
||||
if err != nil {
|
||||
log.Printf("用户[%s]获取雷神账户详情失败: %v", l.userId, err)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
replyMsg = "您还未绑定账号,请先绑定后再使用,绑定指定:\n/雷神 绑定 你的账号 你的密码"
|
||||
} else {
|
||||
replyMsg = "系统错误: " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 解析为结构体
|
||||
var ac vo.LeiGodAccount
|
||||
if err = json.Unmarshal([]byte(data.Data), &ac); err != nil {
|
||||
log.Printf("用户[%s]已绑定雷神账号解析失败: %v", l.userId, err)
|
||||
replyMsg = "系统炸了,请耐心等待修复"
|
||||
return
|
||||
}
|
||||
|
||||
lgu := utils.LeiGodUtil(ac.Account, ac.Password)
|
||||
if err = lgu.Login(); err != nil {
|
||||
return "登录失败: " + err.Error()
|
||||
}
|
||||
var ui model.LeiGodUserInfoResp
|
||||
if ui, err = lgu.Info(); err != nil {
|
||||
return "获取详情失败: " + err.Error()
|
||||
}
|
||||
replyMsg = fmt.Sprintf("#账户 %s\n#剩余时长 %s\n#暂停状态 %s\n#最后暂停时间 %s",
|
||||
ui.Mobile, ui.ExpiryTime, ui.PauseStatus, ui.LastPauseTime)
|
||||
return
|
||||
}
|
||||
|
||||
// pause
|
||||
// @description: 暂停加速
|
||||
// @receiver l
|
||||
// @return flag
|
||||
func (l leiGod) pause() (replyMsg string) {
|
||||
log.Printf("用户[%s]暂停加速", l.userId)
|
||||
|
||||
// 取出已绑定的账号
|
||||
var data entity.PluginData
|
||||
err := client.MySQL.Where("user_id = ?", l.userId).Where("plugin_code = 'leigod'").First(&data).Error
|
||||
if err != nil {
|
||||
log.Printf("用户[%s]获取雷神账户详情失败: %v", l.userId, err)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
replyMsg = "您还未绑定账号,请先绑定后再使用,绑定指定:\n/雷神 绑定 你的账号 你的密码"
|
||||
} else {
|
||||
replyMsg = "系统错误: " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 解析为结构体
|
||||
var ac vo.LeiGodAccount
|
||||
if err = json.Unmarshal([]byte(data.Data), &ac); err != nil {
|
||||
log.Printf("用户[%s]已绑定雷神账号解析失败: %v", l.userId, err)
|
||||
replyMsg = "系统炸了,请耐心等待修复"
|
||||
return
|
||||
}
|
||||
|
||||
lgu := utils.LeiGodUtil(ac.Account, ac.Password)
|
||||
if err = lgu.Login(); err != nil {
|
||||
return "登录失败: " + err.Error()
|
||||
}
|
||||
if err = lgu.Pause(); err != nil {
|
||||
return "暂停失败: " + err.Error()
|
||||
}
|
||||
|
||||
return "暂停成功"
|
||||
}
|
||||
Reference in New Issue
Block a user