diff --git a/README.md b/README.md index 9bd83e1..9812f67 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,8 @@ } ``` 表示只接收以 ! 开头的指令,同时修改配置文件默认中间件锚点的事件过滤器文件目录 `filter: filter.json` ,还要修改上报数据类型为 `array` 。 + +- 自定义配置 + 修改项目目录下的 `config.toml` 文件,`APIURL` 配置为 `go-cqhttp` 的 `post` 配置的 `url` 地址。 + `Port` 配置为 `go-cqhttp` 的 `http` 配置的 `address` 地址。 + 后面的 [Group] 按需求配置,注意里面的群名要加双引号,因为程序里读取的是 `string` 类型。 \ No newline at end of file diff --git a/config/config.go b/config/config.go index e30ad9f..869a983 100644 --- a/config/config.go +++ b/config/config.go @@ -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) } diff --git a/go.mod b/go.mod index 08a12ea..604a2d9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module go-bot go 1.21.0 + +require github.com/BurntSushi/toml v1.3.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ef0f966 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 46a5040..19698ee 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/test/test.go b/test/test.go index 7c95e11..807a0b8 100644 --- a/test/test.go +++ b/test/test.go @@ -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) diff --git a/utils/router.go b/utils/router.go index 21b3316..1a6e44a 100644 --- a/utils/router.go +++ b/utils/router.go @@ -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" { diff --git a/workers/core.go b/workers/core.go index bcc4a32..a87a6f9 100644 --- a/workers/core.go +++ b/workers/core.go @@ -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)) diff --git a/workers/pkg.go b/workers/pkg.go index 6f27c8e..14edcd5 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -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) }