go-bot/workers/lsp.go

60 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-04-23 20:38:37 +08:00
package workers
import (
"fmt"
"go-bot/config"
"slices"
2024-04-23 20:38:37 +08:00
"github.com/goccy/go-json"
"io"
"net/http"
)
func init() {
plugins := config.GetConfig()["PLUGINS"].([]interface{})
if slices.Contains(plugins, "lsp") {
RegisterWorkerFactory("lsp", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Lsp{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
}
})
}
}
2024-04-23 20:38:37 +08:00
type Lsp struct {
*StdAns
}
func (a *Lsp) GetMsg() string {
2024-04-23 21:32:59 +08:00
url := "https://api.lolicon.app/setu/v2?r18=0&size=small"
2024-04-23 20:38:37 +08:00
resp, err := http.Get(url)
if err != nil {
return "请求图片失败"
2024-04-23 20:38:37 +08:00
}
defer resp.Body.Close()
budy, err := io.ReadAll(resp.Body)
if err != nil {
return "读取失败"
}
2024-04-23 20:38:37 +08:00
var res map[string]interface{}
err = json.Unmarshal(budy, &res)
if err != nil {
return "解析失败"
}
code := res["error"].(string)
if code != "" {
return "获取失败"
}
data := res["data"].([]interface{})
uid := data[0].(map[string]interface{})["uid"].(float64)
urls := data[0].(map[string]interface{})["urls"].(map[string]interface{})
2024-04-23 21:32:59 +08:00
imgUrl := urls["small"].(string)
2024-04-23 20:38:37 +08:00
// title := data[0].(map[string]interface{})["title"].(string)
// println("标题:" + title + "\n" + imgUrl)
2024-04-23 21:32:59 +08:00
msg := fmt.Sprintf("[CQ:reply,id=%s]Pixiv ID:%d", a.MID, int64(uid))
2024-04-23 20:38:37 +08:00
a.SendMsg(msg)
2024-04-23 21:32:59 +08:00
return fmt.Sprintf("[CQ:image,file=%s]", imgUrl)
2024-04-23 20:38:37 +08:00
}