Skip to content

Commit

Permalink
Test4 (#21)
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
  • Loading branch information
Hoshinonyaruko authored Oct 27, 2023
1 parent 5a3ad19 commit abfc600
Show file tree
Hide file tree
Showing 20 changed files with 604 additions and 164 deletions.
40 changes: 31 additions & 9 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand All @@ -16,7 +17,7 @@ import (
)

// ProcessC2CMessage 处理C2C消息 群私聊
func (p *Processor) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
// 打印data结构体
PrintStructWithFieldNames(data)

Expand Down Expand Up @@ -89,15 +90,26 @@ func (p *Processor) ProcessC2CMessage(data *dto.WSC2CMessageData) error {

// Convert OnebotGroupMessage to map and send
privateMsgMap := structToMap(privateMsg)
err = p.Wsclient.SendMessage(privateMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}
} else {
//将私聊信息转化为群信息(特殊需求情况下)

//转换at
messageText := handlers.RevertTransformedText(data.Content)
messageText := handlers.RevertTransformedText(data)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
Expand Down Expand Up @@ -165,11 +177,21 @@ func (p *Processor) ProcessC2CMessage(data *dto.WSC2CMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
err = p.Wsclient.SendMessage(groupMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}
}
return nil
}
58 changes: 45 additions & 13 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand All @@ -17,7 +18,7 @@ import (
)

// ProcessChannelDirectMessage 处理频道私信消息 这里我们是被动收到
func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) error {
func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) error {
// 打印data结构体
//PrintStructWithFieldNames(data)

Expand Down Expand Up @@ -93,9 +94,20 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e

// Convert OnebotGroupMessage to map and send
privateMsgMap := structToMap(privateMsg)
err = p.Wsclient.SendMessage(privateMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}
} else {
if !p.Settings.GlobalChannelToGroup {
Expand All @@ -109,7 +121,7 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
//获取s
s := client.GetGlobalS()
//转换at
messageText := handlers.RevertTransformedText(data.Content)
messageText := handlers.RevertTransformedText(data)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
Expand Down Expand Up @@ -175,18 +187,27 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e

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

// 使用 wsclient 发送消息
err = p.Wsclient.SendMessage(msgMap)
if err != nil {
return fmt.Errorf("error sending message via wsclient: %v", err)
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, "; "))
}
} else {
//将频道信息转化为群信息(特殊需求情况下)
//将channelid写入ini,可取出guild_id
idmap.WriteConfigv2(data.ChannelID, "guild_id", data.GuildID)
//转换at
messageText := handlers.RevertTransformedText(data.Content)
messageText := handlers.RevertTransformedText(data)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
Expand Down Expand Up @@ -266,9 +287,20 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
err = p.Wsclient.SendMessage(groupMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}
}

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

"github.com/hoshinonyaruko/gensokyo/config"
Expand All @@ -17,14 +18,14 @@ import (
)

// ProcessGroupMessage 处理群组消息
func (p *Processor) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
// 获取s
s := client.GetGlobalS()

idmap.WriteConfigv2(data.ChannelID, "guild_id", data.GuildID)

// 转换at
messageText := handlers.RevertTransformedText(data.Content)
messageText := handlers.RevertTransformedText(data)

// 转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
Expand Down Expand Up @@ -107,10 +108,20 @@ func (p *Processor) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
err = p.Wsclient.SendMessage(groupMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}
return nil
}
43 changes: 32 additions & 11 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
Expand All @@ -17,7 +18,7 @@ import (
)

// ProcessGuildATMessage 处理消息,执行逻辑并可能使用 api 发送响应
func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
if !p.Settings.GlobalChannelToGroup {
// 将时间字符串转换为时间戳
t, err := time.Parse(time.RFC3339, string(data.Timestamp))
Expand All @@ -27,7 +28,7 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
//获取s
s := client.GetGlobalS()
//转换at
messageText := handlers.RevertTransformedText(data.Content)
messageText := handlers.RevertTransformedText(data)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
Expand Down Expand Up @@ -98,21 +99,30 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
// 将 onebotMsg 结构体转换为 map[string]interface{}
msgMap := structToMap(onebotMsg)

// 使用 wsclient 发送消息
err = p.Wsclient.SendMessage(msgMap)
if err != nil {
return fmt.Errorf("error sending message via wsclient: %v", err)
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, "; "))
}
} else {
// GlobalChannelToGroup为true时的处理逻辑
//将频道转化为一个群
//获取s
s := client.GetGlobalS()
//将channelid写入ini,可取出guild_id todo 比ini更好的储存方式
idmap.WriteConfigv2(data.ChannelID, "guild_id", data.GuildID)
//转换at
messageText := handlers.RevertTransformedText(data.Content)
//转换at和图片
messageText := handlers.RevertTransformedText(data)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
Expand Down Expand Up @@ -192,9 +202,20 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
err = p.Wsclient.SendMessage(groupMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
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, "; "))
}

}
Expand Down
Loading

0 comments on commit abfc600

Please sign in to comment.