Skip to content

Commit

Permalink
Test4 (#11)
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
  • Loading branch information
Hoshinonyaruko authored Oct 26, 2023
1 parent 1acc96c commit 0400d3b
Show file tree
Hide file tree
Showing 32 changed files with 1,302 additions and 205 deletions.
157 changes: 157 additions & 0 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// 处理收到的信息事件
package Processor

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

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/handlers"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/websocket/client"
)

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

// 从私信中提取必要的信息 这是测试回复需要用到
//recipientID := data.Author.ID
//ChannelID := data.ChannelID
//sourece是源头频道
//GuildID := data.GuildID

//获取当前的s值 当前ws连接所收到的信息条数
s := client.GetGlobalS()
if !p.Settings.GlobalPrivateToChannel {
// 直接转换成ob11私信

//转换appidstring
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
echostr := AppIDString + "_" + strconv.FormatInt(s, 10)

//将真实id转为int userid64
userid64, err := idmap.StoreIDv2(data.Author.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}

//收到私聊信息调用的具体还原步骤
//1,idmap还原真实userid,
//发信息使用的是userid

messageID64, err := idmap.StoreIDv2(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
messageID := int(messageID64)
messageText := data.Content
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
privateMsg := OnebotPrivateMessage{
RawMessage: messageText,
Message: segmentedMessages,
MessageID: messageID,
MessageType: "private",
PostType: "message",
SelfID: int64(p.Settings.AppID),
UserID: userid64,
Sender: PrivateSender{
Nickname: "", //这个不支持,但加机器人好友,会收到一个事件,可以对应储存获取,用idmaps可以做到.
UserID: userid64,
},
SubType: "friend",
Time: time.Now().Unix(),
Avatar: "", //todo 同上
Echo: echostr,
}

// 将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
echo.AddMsgType(AppIDString, s, "group_private")
//其实不需要用AppIDString,因为gensokyo是单机器人框架
//可以试着开发一个,会很棒的
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "group_private")
//储存当前群或频道号的类型 私信不需要
//idmap.WriteConfigv2(data.ChannelID, "type", "group_private")

// 调试
PrintStructWithFieldNames(privateMsg)

// 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)
}
} else {
//将私聊信息转化为群信息(特殊需求情况下)

//转换at
messageText := handlers.RevertTransformedText(data.Content)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
echostr := AppIDString + "_" + strconv.FormatInt(s, 10)
//把userid作为群号
//映射str的userid到int
userid64, err := idmap.StoreIDv2(data.Author.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
//映射str的messageID到int
messageID64, err := idmap.StoreIDv2(data.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
messageID := int(messageID64)
//todo 判断array模式 然后对Message处理成array格式
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: messageText,
MessageID: messageID,
GroupID: userid64,
MessageType: "group",
PostType: "message",
SelfID: int64(p.Settings.AppID),
UserID: userid64,
Sender: Sender{
Nickname: "",
UserID: userid64,
},
SubType: "normal",
Time: time.Now().Unix(),
Avatar: "",
Echo: echostr,
}
//将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
echo.AddMsgType(AppIDString, s, "group_private")
//为不支持双向echo的ob服务端映射
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "group_private")

//调试
PrintStructWithFieldNames(groupMsg)

// 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)
}
}

return nil
}
24 changes: 19 additions & 5 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/handlers"
"github.com/hoshinonyaruko/gensokyo/idmap"
Expand Down Expand Up @@ -56,10 +57,15 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
log.Fatalf("Error storing ID: %v", err)
}
messageID := int(messageID64)

messageText := data.Content
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
privateMsg := OnebotPrivateMessage{
RawMessage: data.Content,
Message: data.Content,
RawMessage: messageText,
Message: segmentedMessages,
MessageID: messageID,
MessageType: "private",
PostType: "message",
Expand Down Expand Up @@ -144,6 +150,8 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
//为不支持双向echo的ob服务端映射
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "guild_private")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild_private")

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -186,10 +194,14 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
return nil
}
messageID := int(messageID64)
//todo 判断array模式 然后对Message处理成array格式
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: messageText,
Message: segmentedMessages,
MessageID: messageID,
GroupID: int64(channelIDInt),
MessageType: "group",
Expand All @@ -211,6 +223,8 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
//为不支持双向echo的ob服务端映射
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "guild_private")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild_private")

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
13 changes: 10 additions & 3 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/handlers"
"github.com/hoshinonyaruko/gensokyo/idmap"
Expand Down Expand Up @@ -43,18 +44,22 @@ func (p *Processor) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
log.Printf("Error storing ID: %v", err)
return nil
}
//userid := int(userid64)

//映射str的messageID到int
messageID64, err := idmap.StoreIDv2(data.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
messageID := int(messageID64)
// todo 判断array模式 然后对Message处理成array格式
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: messageText,
Message: segmentedMessages,
MessageID: messageID,
GroupID: GroupID64,
MessageType: "group",
Expand All @@ -77,6 +82,8 @@ func (p *Processor) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
//为不支持双向echo的ob服务端映射
echo.AddMsgID(AppIDString, GroupID64, data.ID)
echo.AddMsgType(AppIDString, GroupID64, "group")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.GroupID, "type", "group")

// 调试
PrintStructWithFieldNames(groupMsg)
Expand Down
28 changes: 17 additions & 11 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/handlers"
"github.com/hoshinonyaruko/gensokyo/idmap"
Expand Down Expand Up @@ -37,19 +38,16 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
log.Printf("Error storing ID: %v", err)
return nil
}
//映射str的messageID到int
//可以是string
// messageID64, err := idmap.StoreIDv2(data.ID)
// if err != nil {
// log.Printf("Error storing ID: %v", err)
// return nil
// }
// messageID := int(messageID64)
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
// 处理onebot_channel_message逻辑
onebotMsg := OnebotChannelMessage{
ChannelID: data.ChannelID,
GuildID: data.GuildID,
Message: messageText,
Message: segmentedMessages,
RawMessage: messageText,
MessageID: data.ID,
MessageType: "guild",
Expand All @@ -74,6 +72,8 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
//为不支持双向echo的ob11服务端映射
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -120,10 +120,14 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
return nil
}
messageID := int(messageID64)
//todo 判断array模式 然后对Message处理成array格式
// 如果在Array模式下, 则处理Message为Segment格式
var segmentedMessages interface{} = messageText
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: messageText,
Message: segmentedMessages,
MessageID: messageID,
GroupID: int64(channelIDInt),
MessageType: "group",
Expand All @@ -145,6 +149,8 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {
//为不支持双向echo的ob服务端映射
echo.AddMsgID(AppIDString, int64(channelIDInt), data.ID)
echo.AddMsgType(AppIDString, int64(channelIDInt), "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
Loading

0 comments on commit 0400d3b

Please sign in to comment.