2024-04-01 09:42:29 +08:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"go-bot/config"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StdAns struct {
|
2024-06-30 21:56:34 +08:00
|
|
|
AllowGroup []interface{}
|
|
|
|
AllowUser []interface{}
|
|
|
|
AllowRole []interface{}
|
|
|
|
BlockGroup []interface{}
|
|
|
|
BlockUser []interface{}
|
2024-04-01 09:42:29 +08:00
|
|
|
GroupNotAllow string
|
|
|
|
UserNotAllow string
|
|
|
|
RoleNotAllow string
|
|
|
|
Parms []string
|
|
|
|
UID string
|
|
|
|
GID string
|
|
|
|
Role string
|
|
|
|
MID string
|
|
|
|
RawMsg string
|
|
|
|
}
|
|
|
|
|
2024-06-30 16:12:26 +08:00
|
|
|
var cfg map[string]interface{}
|
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
// func init() {
|
|
|
|
// cfg = config.GetConfig()
|
|
|
|
// }
|
2024-04-01 09:42:29 +08:00
|
|
|
func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns {
|
2024-06-30 21:56:34 +08:00
|
|
|
// var cfg map[string]interface{}
|
2024-06-30 16:12:26 +08:00
|
|
|
|
2024-06-30 21:56:34 +08:00
|
|
|
cfg = config.GetConfig()
|
|
|
|
// println("AllowGroup:", cfg["AllowGroup"].([]interface{}))
|
2024-04-01 09:42:29 +08:00
|
|
|
return &StdAns{
|
|
|
|
Parms: parms,
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
Role: role,
|
|
|
|
MID: mid,
|
|
|
|
RawMsg: rawMsg,
|
2024-06-30 21:56:34 +08:00
|
|
|
AllowGroup: cfg["AllowGroup"].([]interface{}),
|
|
|
|
AllowUser: cfg["AllowUser"].([]interface{}),
|
|
|
|
AllowRole: cfg["AllowRole"].([]interface{}),
|
|
|
|
BlockGroup: cfg["BlockGroup"].([]interface{}),
|
|
|
|
BlockUser: cfg["BlockUser"].([]interface{}),
|
2024-04-01 09:42:29 +08:00
|
|
|
GroupNotAllow: "汝所在的群组不被允许这样命令咱呢.",
|
|
|
|
UserNotAllow: "汝不被允许呢.",
|
|
|
|
RoleNotAllow: "汝的角色不被允许哦.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StdAns) CheckPermission() string {
|
|
|
|
if (len(s.AllowGroup) > 0 && !contains(s.AllowGroup, s.GID)) || contains(s.BlockGroup, s.GID) {
|
|
|
|
return s.GroupNotAllow
|
|
|
|
}
|
|
|
|
if (len(s.AllowUser) > 0 && !contains(s.AllowUser, s.UID)) || contains(s.BlockUser, s.UID) {
|
|
|
|
return s.UserNotAllow
|
|
|
|
}
|
|
|
|
if len(s.AllowRole) > 0 && !contains(s.AllowRole, s.Role) {
|
|
|
|
return s.RoleNotAllow
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
2024-06-30 21:56:34 +08:00
|
|
|
func contains(slice []interface{}, value string) bool {
|
2024-04-01 09:42:29 +08:00
|
|
|
for _, item := range slice {
|
|
|
|
if item == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StdAns) GetMsg() string {
|
2024-04-01 11:19:52 +08:00
|
|
|
// 暂时设置权限校验不通过什么都不提示
|
|
|
|
return "-1"
|
|
|
|
// if s.Parms[0] != "" {
|
|
|
|
// return "咱也不知道 " + s.Parms[0] + "是啥呢!"
|
|
|
|
// } else {
|
2024-04-01 09:42:29 +08:00
|
|
|
|
2024-04-01 11:19:52 +08:00
|
|
|
// return "汝再调戏咱,咱可就生气了!!"
|
|
|
|
// }
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StdAns) SendMsg(msg string) bool {
|
2024-04-01 11:19:52 +08:00
|
|
|
if msg == "-1" {
|
|
|
|
return false
|
|
|
|
}
|
2024-04-01 09:42:29 +08:00
|
|
|
requestBody, err := json.Marshal(map[string]interface{}{
|
|
|
|
"action": "send_msg",
|
|
|
|
"params": map[string]interface{}{
|
|
|
|
"group_id": s.GID,
|
|
|
|
"message": msg,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error marshaling JSON:", err)
|
|
|
|
return false
|
|
|
|
}
|
2024-04-15 15:26:46 +08:00
|
|
|
_, err = json.MarshalIndent(requestBody, "", " ")
|
2024-04-01 09:42:29 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error marshaling JSON:", err)
|
|
|
|
return false
|
|
|
|
}
|
2024-04-15 15:26:46 +08:00
|
|
|
// fmt.Println(string(re))
|
2024-04-03 21:53:57 +08:00
|
|
|
|
2024-06-30 16:12:26 +08:00
|
|
|
url := cfg["POSTURL"].(string)
|
2024-04-03 21:53:57 +08:00
|
|
|
// println("core:", url)
|
2024-04-15 15:26:46 +08:00
|
|
|
// fmt.Println("请求地址:", url)
|
|
|
|
fmt.Println("响应信息:\n", msg)
|
2024-04-01 09:42:29 +08:00
|
|
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error sending request:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2024-04-01 11:19:52 +08:00
|
|
|
// res, _ := io.ReadAll(resp.Body)
|
|
|
|
// fmt.Println("响应返回:", string(res))
|
2024-04-01 09:42:29 +08:00
|
|
|
return true
|
|
|
|
}
|