Skip to content

Commit

Permalink
Test4 (#22)
Browse files Browse the repository at this point in the history
* Compiled main.go and pushed changes

* test

* 适配了频道私聊,用bolt数据库取代ini

* 适配了nonebot2

* add license

* add a lot

* trss support

* add action

* add action

* add action

* fixbug

* add wss

* bugfix

* fix action

* fix action again

* fa

* fix

* add a lot

* add ws server token
  • Loading branch information
Hoshinonyaruko authored Oct 28, 2023
1 parent abfc600 commit 0404531
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 255 deletions.
36 changes: 4 additions & 32 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
package Processor

import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -90,21 +88,8 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {

// Convert OnebotGroupMessage to map and send
privateMsgMap := structToMap(privateMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(privateMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending private message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(privateMsgMap)
} else {
//将私聊信息转化为群信息(特殊需求情况下)

Expand Down Expand Up @@ -177,21 +162,8 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(groupMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending group message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
log.Println("Encountered errors while sending to wsclients:", strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(groupMsgMap)
}
return nil
}
52 changes: 6 additions & 46 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -94,21 +93,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)

// Convert OnebotGroupMessage to map and send
privateMsgMap := structToMap(privateMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(privateMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending private message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(privateMsgMap)
} else {
if !p.Settings.GlobalChannelToGroup {
//将频道私信作为普通频道信息
Expand Down Expand Up @@ -187,21 +173,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)

// 将 onebotMsg 结构体转换为 map[string]interface{}
msgMap := structToMap(onebotMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(msgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(msgMap)
} else {
//将频道信息转化为群信息(特殊需求情况下)
//将channelid写入ini,可取出guild_id
Expand Down Expand Up @@ -287,21 +260,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(groupMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending group message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(groupMsgMap)
}

}
Expand Down
18 changes: 2 additions & 16 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -108,20 +107,7 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(groupMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending group message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(groupMsgMap)
return nil
}
35 changes: 4 additions & 31 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -99,21 +98,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
// 将 onebotMsg 结构体转换为 map[string]interface{}
msgMap := structToMap(onebotMsg)

var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(msgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(msgMap)
} else {
// GlobalChannelToGroup为true时的处理逻辑
//将频道转化为一个群
Expand Down Expand Up @@ -202,21 +188,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
var errors []string

for _, client := range p.Wsclient {
err = client.SendMessage(groupMsgMap)
if err != nil {
// 记录错误信息,但不立即返回
errors = append(errors, fmt.Sprintf("error sending group message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(groupMsgMap)

}

Expand Down
37 changes: 4 additions & 33 deletions Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -98,22 +97,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
// 将 onebotMsg 结构体转换为 map[string]interface{}
msgMap := structToMap(onebotMsg)

var errors []string

// 遍历每一个 wsclient 并发送消息
for _, client := range p.Wsclient {
err = client.SendMessage(msgMap)
if err != nil {
// 记录错误但不立即返回
errors = append(errors, fmt.Sprintf("error sending message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join来合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(msgMap)
} else {
// GlobalChannelToGroup为true时的处理逻辑
//将频道转化为一个群
Expand Down Expand Up @@ -203,22 +188,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)

var errors []string

// 遍历每一个 wsclient 并发送消息
for _, client := range p.Wsclient {
err = client.SendMessage(groupMsgMap)
if err != nil {
// 记录错误但不立即返回
errors = append(errors, fmt.Sprintf("error sending group message via wsclient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
// 使用strings.Join来合并所有的错误信息
return fmt.Errorf(strings.Join(errors, "; "))
}
//上报信息到onebotv11应用端(正反ws)
p.BroadcastMessageToAll(groupMsgMap)
}

return nil
Expand Down
58 changes: 53 additions & 5 deletions Processor/Processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/wsclient"

"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/openapi"
)

// Processor 结构体用于处理消息
type Processors struct {
Api openapi.OpenAPI // API 类型
Apiv2 openapi.OpenAPI //群的API
Settings *config.Settings // 使用指针
Wsclient []*wsclient.WebSocketClient // 指针的切片
Api openapi.OpenAPI // API 类型
Apiv2 openapi.OpenAPI //群的API
Settings *config.Settings // 使用指针
Wsclient []*wsclient.WebSocketClient // 指针的切片
WsServerClients []callapi.WebSocketServerClienter //ws server被连接的客户端
}

type Sender struct {
Expand Down Expand Up @@ -183,3 +186,48 @@ func NewProcessor(api openapi.OpenAPI, apiv2 openapi.OpenAPI, settings *config.S
Wsclient: wsclient,
}
}

// 发信息给所有连接正向ws的客户端
func (p *Processors) SendMessageToAllClients(message map[string]interface{}) error {
var result *multierror.Error

for _, client := range p.WsServerClients {
// 使用接口的方法
err := client.SendMessage(message)
if err != nil {
// Append the error to our result
result = multierror.Append(result, fmt.Errorf("failed to send to client: %w", err))
}
}

// This will return nil if no errors were added
return result.ErrorOrNil()
}

// 方便快捷的发信息函数
func (p *Processors) BroadcastMessageToAll(message map[string]interface{}) error {
var errors []string

// 发送到我们作为客户端的Wsclient
for _, client := range p.Wsclient {
err := client.SendMessage(message)
if err != nil {
errors = append(errors, fmt.Sprintf("error sending private message via wsclient: %v", err))
}
}

// 发送到我们作为服务器连接到我们的WsServerClients
for _, serverClient := range p.WsServerClients {
err := serverClient.SendMessage(message)
if err != nil {
errors = append(errors, fmt.Sprintf("error sending private message via WsServerClient: %v", err))
}
}

// 在循环结束后处理记录的错误
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "; "))
}

return nil
}
5 changes: 5 additions & 0 deletions callapi/callapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ type Client interface {
SendMessage(message map[string]interface{}) error
}

// 为了解决processor和server循环依赖设计的接口
type WebSocketServerClienter interface {
SendMessage(message map[string]interface{}) error
}

// 根据action订阅handler处理api
type HandlerFunc func(client Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, messgae ActionMessage)

Expand Down
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Settings struct {
WsToken []string `yaml:"ws_token,omitempty"` // 连接wss时使用,不是wss可留空 一一对应
MasterID []string `yaml:"master_id,omitempty"` // 如果需要在群权限判断是管理员是,将user_id填入这里,master_id是一个文本数组
EnableWsServer bool `yaml:"enable_ws_server,omitempty"` //正向ws开关
WsServerToken string `yaml:"ws_server_token,omitempty"` //正向ws token
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -169,3 +170,15 @@ func GetEnableWsServer() bool {
}
return instance.Settings.EnableWsServer
}

// 获取WsServerToken的值
func GetWsServerToken() string {
mu.Lock()
defer mu.Unlock()

if instance == nil {
log.Println("Warning: instance is nil when trying to get WsServerToken value.")
return ""
}
return instance.Settings.WsServerToken
}
Loading

0 comments on commit 0404531

Please sign in to comment.