增加协程支持,优化部分逻辑,增加自定义配置

This commit is contained in:
liyp 2024-04-03 21:53:57 +08:00
parent 1f0618c69d
commit cd5f9707bc
9 changed files with 63 additions and 21 deletions

View file

@ -26,3 +26,8 @@
}
```
表示只接收以 ! 开头的指令,同时修改配置文件默认中间件锚点的事件过滤器文件目录 `filter: filter.json` ,还要修改上报数据类型为 `array`
- 自定义配置
修改项目目录下的 `config.toml` 文件,`APIURL` 配置为 `go-cqhttp``post` 配置的 `url` 地址。
`Port` 配置为 `go-cqhttp``http` 配置的 `address` 地址。
后面的 [Group] 按需求配置,注意里面的群名要加双引号,因为程序里读取的是 `string` 类型。

View file

@ -1,9 +1,34 @@
package config
var APIURL string
var PORT string
import (
"log"
func LoadConfig() {
// 读取配置文件等操作
"github.com/BurntSushi/toml"
)
type Config struct {
Server struct {
APIURL string
Port string
}
Group struct {
AllowGroup []string
AllowUser []string
AllowRole []string
BlockGroup []string
BlockUser []string
GroupNotAllow []string
UserNotAllow []string
RoleNotAllow []string
}
}
var GlobalConfig Config
func init() {
// var config Config
if _, err := toml.DecodeFile("config.toml", &GlobalConfig); err != nil {
log.Fatal(err)
}
// fmt.Println(config.Group)
}

2
go.mod
View file

@ -1,3 +1,5 @@
module go-bot
go 1.21.0
require github.com/BurntSushi/toml v1.3.2

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=

19
main.go
View file

@ -20,7 +20,7 @@ func handlePost(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
// fmt.Println(string(body))
fmt.Println("handlePost:", string(body))
// 解码 JSON 数据到 map[string]interface{} 类型的变量
var data map[string]interface{}
err = json.Unmarshal(body, &data)
@ -34,11 +34,18 @@ func handlePost(w http.ResponseWriter, r *http.Request) {
}
func main() {
config.LoadConfig()
config.APIURL = "http://127.0.0.1:5700/"
config.PORT = ":5580"
http.HandleFunc("/", handlePost)
APIURL := config.GlobalConfig.Server.APIURL
PORT := config.GlobalConfig.Server.Port
fmt.Println(APIURL)
fmt.Println(PORT)
// http.HandleFunc("/", handlePost)
// // 协程支持
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
go handlePost(w, r)
})
fmt.Println("Server listening on port 5580...")
http.ListenAndServe(config.PORT, nil)
// APIURL 为 go-cqhttp 配置的事件上报地址
http.ListenAndServe(APIURL, nil)
}

View file

@ -12,7 +12,7 @@ func main() {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("输入指令(不要带/)")
fmt.Print("输入指令(不要带/)")
raw_msg, _ := reader.ReadString('\n')
// 去除末尾的换行符
raw_msg = strings.TrimRight(raw_msg, "\r\n")
@ -20,7 +20,7 @@ func main() {
parms := strings.Split(raw_msg, " ")
worker := workers.NewWorker(parms, "11", "111", "111", "222", raw_msg)
fmt.Println("Test:", worker.CheckPermission())
message := worker.GetMsg()
fmt.Println("message:", message)

View file

@ -27,7 +27,7 @@ func Router(data map[string]interface{}) {
worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
// fmt.Println("router:", parms[0])
// fmt.Println(worker.CheckPermission())
fmt.Println("CheckPermission:", worker.CheckPermission())
// ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg)
message := worker.CheckPermission()
if message == "0" {

View file

@ -33,11 +33,11 @@ func NewStdAns(parms []string, uid, gid, role, mid, rawMsg string) *StdAns {
Role: role,
MID: mid,
RawMsg: rawMsg,
AllowGroup: []string{},
AllowUser: []string{},
AllowRole: []string{},
BlockGroup: []string{},
BlockUser: []string{},
AllowGroup: config.GlobalConfig.Group.AllowGroup,
AllowUser: config.GlobalConfig.Group.AllowUser,
AllowRole: config.GlobalConfig.Group.AllowRole,
BlockGroup: config.GlobalConfig.Group.BlockGroup,
BlockUser: config.GlobalConfig.Group.BlockUser,
GroupNotAllow: "汝所在的群组不被允许这样命令咱呢.",
UserNotAllow: "汝不被允许呢.",
RoleNotAllow: "汝的角色不被允许哦.",
@ -98,8 +98,9 @@ func (s *StdAns) SendMsg(msg string) bool {
return false
}
fmt.Println(string(re))
config.LoadConfig()
url := config.APIURL
url := config.GlobalConfig.Server.APIURL
// println("core:", url)
fmt.Println("请求地址:", url)
fmt.Println("响应信息:", msg)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(requestBody))

View file

@ -52,7 +52,7 @@ func (a *Pkg) GetMsg() string {
defer req.Body.Close()
re, err := io.ReadAll(req.Body)
fmt.Println(string(re))
// fmt.Println(string(re))
if err != nil {
fmt.Println(err)
}