diff --git a/Processor/Processor.go b/Processor/Processor.go index 85b95f6b..c10db3fb 100644 --- a/Processor/Processor.go +++ b/Processor/Processor.go @@ -268,52 +268,55 @@ func (p *Processors) BroadcastMessageToAll(message map[string]interface{}, api o failed++ } - // 检查是否所有尝试都失败了 - if failed == len(p.Wsclient)+len(p.WsServerClients) { - // 处理全部失败的情况 - fmt.Println("All message sending attempts failed.") - downtimemessgae := config.GetDowntimeMessage() - switch v := data.(type) { - case *dto.WSGroupATMessageData: - msgtocreate := &dto.MessageToCreate{ - Content: downtimemessgae, - MsgID: v.ID, - MsgSeq: 1, - MsgType: 0, // 默认文本类型 + // 仅对连接正反ws的bot应用这个判断 + if !p.Settings.HttpOnlyBot { + // 检查是否所有尝试都失败了 + if failed == len(p.Wsclient)+len(p.WsServerClients) { + // 处理全部失败的情况 + fmt.Println("All ws event sending attempts failed.") + downtimemessgae := config.GetDowntimeMessage() + switch v := data.(type) { + case *dto.WSGroupATMessageData: + msgtocreate := &dto.MessageToCreate{ + Content: downtimemessgae, + MsgID: v.ID, + MsgSeq: 1, + MsgType: 0, // 默认文本类型 + } + api.PostGroupMessage(context.Background(), v.GroupID, msgtocreate) + case *dto.WSATMessageData: + msgtocreate := &dto.MessageToCreate{ + Content: downtimemessgae, + MsgID: v.ID, + MsgSeq: 1, + MsgType: 0, // 默认文本类型 + } + api.PostMessage(context.Background(), v.ChannelID, msgtocreate) + case *dto.WSMessageData: + msgtocreate := &dto.MessageToCreate{ + Content: downtimemessgae, + MsgID: v.ID, + MsgSeq: 1, + MsgType: 0, // 默认文本类型 + } + api.PostMessage(context.Background(), v.ChannelID, msgtocreate) + case *dto.WSDirectMessageData: + msgtocreate := &dto.MessageToCreate{ + Content: downtimemessgae, + MsgID: v.ID, + MsgSeq: 1, + MsgType: 0, // 默认文本类型 + } + api.PostMessage(context.Background(), v.GuildID, msgtocreate) + case *dto.WSC2CMessageData: + msgtocreate := &dto.MessageToCreate{ + Content: downtimemessgae, + MsgID: v.ID, + MsgSeq: 1, + MsgType: 0, // 默认文本类型 + } + api.PostC2CMessage(context.Background(), v.Author.ID, msgtocreate) } - api.PostGroupMessage(context.Background(), v.GroupID, msgtocreate) - case *dto.WSATMessageData: - msgtocreate := &dto.MessageToCreate{ - Content: downtimemessgae, - MsgID: v.ID, - MsgSeq: 1, - MsgType: 0, // 默认文本类型 - } - api.PostMessage(context.Background(), v.ChannelID, msgtocreate) - case *dto.WSMessageData: - msgtocreate := &dto.MessageToCreate{ - Content: downtimemessgae, - MsgID: v.ID, - MsgSeq: 1, - MsgType: 0, // 默认文本类型 - } - api.PostMessage(context.Background(), v.ChannelID, msgtocreate) - case *dto.WSDirectMessageData: - msgtocreate := &dto.MessageToCreate{ - Content: downtimemessgae, - MsgID: v.ID, - MsgSeq: 1, - MsgType: 0, // 默认文本类型 - } - api.PostMessage(context.Background(), v.GuildID, msgtocreate) - case *dto.WSC2CMessageData: - msgtocreate := &dto.MessageToCreate{ - Content: downtimemessgae, - MsgID: v.ID, - MsgSeq: 1, - MsgType: 0, // 默认文本类型 - } - api.PostC2CMessage(context.Background(), v.Author.ID, msgtocreate) } } diff --git a/main.go b/main.go index 2318f1ce..48433c61 100644 --- a/main.go +++ b/main.go @@ -318,8 +318,24 @@ func main() { p = Processor.NewProcessor(api, apiV2, &conf.Settings, wsClients) } } else { - log.Println("提示,目前只启动了正向ws或httpapi") + // p一定需要初始化 p = Processor.NewProcessorV2(api, apiV2, &conf.Settings) + // 如果只启动了http api + if !conf.Settings.EnableWsServer { + if conf.Settings.HttpAddress != "" { + // 对全局生效 + conf.Settings.HttpOnlyBot = true + log.Println("提示,目前只启动了httpapi,正反向ws均未配置.") + } else { + log.Println("提示,目前你配置了个寂寞,httpapi没设置,正反ws都没配置.") + } + } else { + if conf.Settings.HttpAddress != "" { + log.Println("提示,目前启动了正向ws和httpapi,未连接反向ws") + } else { + log.Println("提示,目前启动了正向ws,未连接反向ws,httpapi未开启") + } + } } } else { // 设置颜色为红色 @@ -327,7 +343,6 @@ func main() { // 输出红色文本 red.Println("请设置正确的appid、token、clientsecret再试") } - } //图片上传 调用次数限制 diff --git a/server/wsserver.go b/server/wsserver.go index ae5aed28..d14f5a8e 100644 --- a/server/wsserver.go +++ b/server/wsserver.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "strings" + "sync" "time" "github.com/gin-gonic/gin" @@ -20,6 +21,7 @@ type WebSocketServerClient struct { Conn *websocket.Conn API openapi.OpenAPI APIv2 openapi.OpenAPI + mu sync.Mutex // 互斥锁保护 conn } var upgrader = websocket.Upgrader{ @@ -155,6 +157,9 @@ func processWSMessage(client *WebSocketServerClient, msg []byte) { // 发信息给client func (c *WebSocketServerClient) SendMessage(message map[string]interface{}) error { + c.mu.Lock() + defer c.mu.Unlock() + msgBytes, err := json.Marshal(message) if err != nil { mylog.Println("Error marshalling message:", err) diff --git a/structs/structs.go b/structs/structs.go index 6940c952..29c7c725 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -141,7 +141,8 @@ type Settings struct { EnableChangeWord bool `yaml:"enableChangeWord"` DefaultChangeWord string `yaml:"defaultChangeWord"` //错误临时修复类 - Fix11300 bool `yaml:"fix_11300"` + Fix11300 bool `yaml:"fix_11300"` + HttpOnlyBot bool `yaml:"http_only_bot"` //内置指令 BindPrefix string `yaml:"bind_prefix"` MePrefix string `yaml:"me_prefix"` diff --git a/template/config_template.go b/template/config_template.go index 3006a586..48863c48 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -183,7 +183,7 @@ settings: #错误临时修复类 fix_11300: false #修复11300报错,需要在develop_bot_id填入自己机器人的appid. 11300原因暂时未知,临时修复方案. - + http_only_bot : false #这个配置项会自动配置,请不要修改,保持false. #内置指令类 bind_prefix : "/bind" #需设置 #增强配置项 master_id 可触发