go-bot/workers/hhsh.go

74 lines
1.6 KiB
Go
Raw Normal View History

2024-04-14 19:08:12 +08:00
package workers
import (
"fmt"
"go-bot/config"
"slices"
2024-04-14 19:08:12 +08:00
"strings"
"time"
2024-04-14 19:08:12 +08:00
"github.com/imroc/req/v3"
2024-04-14 19:08:12 +08:00
)
func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "hhsh") {
RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Hhsh{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
}
})
}
}
2024-04-14 19:08:12 +08:00
type Hhsh struct {
*StdAns
}
func (a *Hhsh) GetMsg() string {
if len(a.Parms) < 2 {
return "请输入参数 如:!hhsh awsl"
}
// 去除换行符
raw_msg := strings.TrimRight(a.RawMsg, "\n")
fmt.Println("raw_msg:", raw_msg)
parms := strings.Split(raw_msg, " ")
url := "https://api.beiyu.vip/api/hhsh?str=" + parms[1]
// 输出请求地址
2024-04-14 19:08:12 +08:00
// fmt.Println("url:", url)
var data map[string]interface{}
client := req.C().SetTimeout(time.Second * 10)
resp, err := client.R().
SetSuccessResult(&data).
Get(url)
2024-04-14 19:08:12 +08:00
if err != nil {
return "接口异常!"
}
defer resp.Body.Close()
fmt.Println("hhsh body:", resp.String())
2024-04-14 19:08:12 +08:00
// var pkg []Package
2024-04-14 19:08:12 +08:00
resultSlipe := data["data"].([]interface{})
2024-04-14 22:25:13 +08:00
fmt.Println("resultSlipe:", resultSlipe)
2024-04-14 19:08:12 +08:00
if trans, ok := resultSlipe[0].(map[string]interface{})["trans"]; ok {
// println("trans:", trans.([]interface{}))
transSlice := trans.([]interface{})
2024-04-14 20:46:34 +08:00
msg := "[CQ:reply,id=" + a.MID + "][CQ:at,qq=" + a.UID + "] "
2024-04-14 19:08:12 +08:00
for _, item := range transSlice {
// fmt.Print(item, " ")
2024-04-14 20:40:55 +08:00
msg += item.(string) + " "
2024-04-14 19:08:12 +08:00
}
2024-04-14 20:43:39 +08:00
return msg
2024-04-14 19:08:12 +08:00
}
// return string(resultSlipe[0].([]byte))
2024-04-14 22:25:13 +08:00
return "未找到关于 " + parms[1] + " 的释义"
2024-04-14 19:08:12 +08:00
}