Skip to content

Commit

Permalink
Merge d793307 into 504c164
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko committed Feb 25, 2024
2 parents 504c164 + d793307 commit 0a94ff4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
66 changes: 66 additions & 0 deletions Processor/Processor.go
Expand Up @@ -2,11 +2,13 @@
package Processor

import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -252,9 +254,73 @@ func (p *Processors) BroadcastMessageToAll(message map[string]interface{}) error
return fmt.Errorf(strings.Join(errors, "; "))
}

//判断是否填写了反向post地址
if !allEmpty(config.GetPostUrl()) {
PostMessageToUrls(message)
}

return nil
}

// allEmpty checks if all the strings in the slice are empty.
func allEmpty(addresses []string) bool {
for _, addr := range addresses {
if addr != "" {
return false
}
}
return true
}

// 上报信息给反向Http
func PostMessageToUrls(message map[string]interface{}) {
// 获取上报 URL 列表
postUrls := config.GetPostUrl()

// 检查 postUrls 是否为空
if len(postUrls) > 0 {

// 转换 message 为 JSON 字符串
jsonString, err := handlers.ConvertMapToJSONString(message)
if err != nil {
mylog.Printf("Error converting message to JSON: %v", err)
return
}

for _, url := range postUrls {
// 创建请求体
reqBody := bytes.NewBufferString(jsonString)

// 创建 POST 请求
req, err := http.NewRequest("POST", url, reqBody)
if err != nil {
mylog.Printf("Error creating POST request to %s: %v", url, err)
continue
}

// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 设置 X-Self-ID
selfid := config.GetAppIDStr()
req.Header.Set("X-Self-ID", selfid)

// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
mylog.Printf("Error sending POST request to %s: %v", url, err)
continue
}

// 处理响应
defer resp.Body.Close()
// 可以添加更多的响应处理逻辑,如检查状态码等

mylog.Printf("Posted to %s successfully", url)
}
}
}

func (p *Processors) HandleFrameworkCommand(messageText string, data interface{}, Type string, s *discordgo.Session) error {
// 正则表达式匹配转换后的 CQ 码
cqRegex := regexp.MustCompile(`\[CQ:at,qq=\d+\]`)
Expand Down
2 changes: 1 addition & 1 deletion handlers/send_guild_channel_msg.go
Expand Up @@ -87,7 +87,7 @@ func HandleSendGuildChannelMsg(client callapi.Client, s *discordgo.Session, mess
mylog.Printf("生成消息失败: %v", err)
return "", err
}

mylog.Printf("频道发信息channelID:%v replyMsg:%v", channelID, replyMsg)
_, err = s.ChannelMessageSendComplex(channelID, replyMsg)
if err != nil {
mylog.Printf("发送消息失败: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions httpapi/httpapi.go
@@ -1,6 +1,7 @@
package httpapi

import (
"fmt"
"net/http"
"strconv"

Expand All @@ -14,6 +15,7 @@ import (
func CombinedMiddleware(s *discordgo.Session) gin.HandlerFunc {
return func(c *gin.Context) {
// 检查路径和处理对应的请求
fmt.Printf("s2:%v", s)
if c.Request.URL.Path == "/send_group_msg" {
handleSendGroupMessage(c, s)
return
Expand Down
8 changes: 6 additions & 2 deletions main.go
Expand Up @@ -35,6 +35,7 @@ type Event interface{}

// 消息处理器
var p *Processor.Processors
var globalBotId string

func main() {
// 定义faststart命令行标志。默认为false。
Expand Down Expand Up @@ -100,7 +101,7 @@ func main() {
mylog.Println("并且到discord developer网站,获取机器人的token并填入config")
} else {
// 创建一个新的 Discord 会话
dg, err := discordgo.New("Bot " + conf.Settings.Token)
dg, err = discordgo.New("Bot " + conf.Settings.Token)
if err != nil {
mylog.Printf("创建 Discord 会话时出错: %v\n", err)
return // 或其他错误处理
Expand Down Expand Up @@ -140,6 +141,7 @@ func main() {
if configURL == "" { //初始化handlers
handlers.BotID = dg.State.User.ID
mylog.Printf("本机器人id:%v\n", handlers.BotID)
globalBotId = dg.State.User.ID
} else { //初始化handlers
handlers.BotID = config.GetDevBotid()
}
Expand Down Expand Up @@ -478,7 +480,9 @@ func guildMessagesHandler(s *discordgo.Session, i interface{}) {
// mylog.Println("Event type mismatch: expected *discordgo.MessageCreate")
return
}

if event.Author.ID == globalBotId {
return
}
// 如果 GuildID 为空,则认为是私信
if event.GuildID == "" {
// 处理私信
Expand Down
13 changes: 13 additions & 0 deletions template/config_template.go
Expand Up @@ -125,6 +125,19 @@ settings:
#穿透\cos\oss类配置(可选!)
frp_port : "0" #不使用请保持为0,frp的端口,frp有内外端口,请在frp软件设置gensokyo的port,并将frp显示的对外端口填入这里
#HTTP API配置
#正向http
http_address: "" #http监听地址 与websocket独立 示例:0.0.0.0:5700 为空代表不开启
http_version : 11 #暂时只支持11
http_timeout: 5 #反向 HTTP 超时时间, 单位秒,<5 时将被忽略
#反向http
post_url: [""] #反向HTTP POST地址列表 为空代表不开启 示例:http://192.168.0.100:5789
post_secret: [""] #密钥
post_max_retries: [3] #最大重试,0 时禁用
post_retries_interval: [1500] #重试时间,单位毫秒,0 时立即
`
const Logo = `
'
Expand Down

0 comments on commit 0a94ff4

Please sign in to comment.