Skip to content

Commit

Permalink
beta89
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko committed Dec 13, 2023
1 parent 063bbca commit 09d4479
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 48 deletions.
21 changes: 15 additions & 6 deletions Processor/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
var IsBindedUserId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
}
privateMsg := OnebotPrivateMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -98,11 +103,15 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
Nickname: "", //这个不支持,但加机器人好友,会收到一个事件,可以对应储存获取,用idmaps可以做到.
UserID: userid64,
},
SubType: "friend",
Time: time.Now().Unix(),
Avatar: "", //todo 同上
RealMessageType: "group_private",
IsBindedUserId: IsBindedUserId,
SubType: "friend",
Time: time.Now().Unix(),
}
if !config.GetNativeOb11() {
privateMsg.RealMessageType = "group_private"
privateMsg.IsBindedUserId = IsBindedUserId
if IsBindedUserId {
privateMsg.Avatar, _ = GenerateAvatarURL(userid64)
}
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
Expand Down
41 changes: 29 additions & 12 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
var IsBindedUserId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
}

privateMsg := OnebotPrivateMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -119,11 +125,14 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
Nickname: data.Member.Nick,
UserID: userid64,
},
SubType: "friend",
Time: time.Now().Unix(),
Avatar: data.Author.Avatar,
RealMessageType: "guild_private",
IsBindedUserId: IsBindedUserId,
SubType: "friend",
Time: time.Now().Unix(),
}
//增强字段
if !config.GetNativeOb11() {
privateMsg.RealMessageType = "guild_private"
privateMsg.IsBindedUserId = IsBindedUserId
privateMsg.Avatar = data.Author.Avatar
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
Expand Down Expand Up @@ -318,7 +327,12 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
var IsBindedUserId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -338,11 +352,14 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
Area: "",
Level: "0",
},
SubType: "normal",
Time: time.Now().Unix(),
Avatar: data.Author.Avatar,
RealMessageType: "guild_private",
IsBindedUserId: IsBindedUserId,
SubType: "normal",
Time: time.Now().Unix(),
}
//增强字段
if !config.GetNativeOb11() {
groupMsg.RealMessageType = "guild_private"
groupMsg.IsBindedUserId = IsBindedUserId
groupMsg.Avatar = data.Author.Avatar
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
Expand Down
27 changes: 19 additions & 8 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,14 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId := idmap.CheckValue(data.GroupID, GroupID64)
var IsBindedUserId, IsBindedGroupId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId = idmap.CheckValue(data.GroupID, GroupID64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
IsBindedGroupId = idmap.CheckValuev2(GroupID64)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -106,12 +112,17 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
Area: "0",
Level: "0",
},
SubType: "normal",
Time: time.Now().Unix(),
Avatar: "",
RealMessageType: "group",
IsBindedUserId: IsBindedUserId,
IsBindedGroupId: IsBindedGroupId,
SubType: "normal",
Time: time.Now().Unix(),
}
//增强配置
if !config.GetNativeOb11() {
groupMsg.RealMessageType = "group"
groupMsg.IsBindedUserId = IsBindedUserId
groupMsg.IsBindedGroupId = IsBindedGroupId
if IsBindedUserId {
groupMsg.Avatar, _ = GenerateAvatarURL(userid64)
}
}
//根据条件判断是否增加nick和card
var CaN = config.GetCardAndNick()
Expand Down
17 changes: 15 additions & 2 deletions Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId := idmap.CheckValue(data.GroupID, ChannelID64)
var IsBindedUserId, IsBindedGroupId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId = idmap.CheckValue(data.ChannelID, ChannelID64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
IsBindedGroupId = idmap.CheckValuev2(ChannelID64)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -205,6 +211,13 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
IsBindedUserId: IsBindedUserId,
IsBindedGroupId: IsBindedGroupId,
}
//增强配置
if !config.GetNativeOb11() {
groupMsg.RealMessageType = "guild"
groupMsg.IsBindedUserId = IsBindedUserId
groupMsg.IsBindedGroupId = IsBindedGroupId
groupMsg.Avatar = data.Author.Avatar
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
Expand Down
26 changes: 17 additions & 9 deletions Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
return nil
}
}

//转成int再互转
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "guild_id", data.GuildID)
//转换at
Expand All @@ -182,8 +181,14 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
if config.GetArrayValue() {
segmentedMessages = handlers.ConvertToSegmentedMessage(data)
}
IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId := idmap.CheckValue(data.GroupID, ChannelID64)
var IsBindedUserId, IsBindedGroupId bool
if config.GetHashIDValue() {
IsBindedUserId = idmap.CheckValue(data.Author.ID, userid64)
IsBindedGroupId = idmap.CheckValue(data.ChannelID, ChannelID64)
} else {
IsBindedUserId = idmap.CheckValuev2(userid64)
IsBindedGroupId = idmap.CheckValuev2(ChannelID64)
}
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: segmentedMessages,
Expand All @@ -202,12 +207,15 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
Area: "",
Level: "0",
},
SubType: "normal",
Time: time.Now().Unix(),
Avatar: data.Author.Avatar,
RealMessageType: "guild",
IsBindedUserId: IsBindedUserId,
IsBindedGroupId: IsBindedGroupId,
SubType: "normal",
Time: time.Now().Unix(),
}
//增强配置
if !config.GetNativeOb11() {
groupMsg.RealMessageType = "guild"
groupMsg.IsBindedUserId = IsBindedUserId
groupMsg.IsBindedGroupId = IsBindedGroupId
groupMsg.Avatar = data.Author.Avatar
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
Expand Down
49 changes: 38 additions & 11 deletions Processor/Processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"reflect"
"regexp"
Expand Down Expand Up @@ -86,9 +87,9 @@ type OnebotGroupMessage struct {
MessageSeq int `json:"message_seq"`
Font int `json:"font"`
UserID int64 `json:"user_id"`
RealMessageType string `json:"real_message_type"` //当前信息的真实类型 group group_private guild guild_private
IsBindedGroupId bool `json:"is_binded_group_id"` //当前群号是否是binded后的
IsBindedUserId bool `json:"is_binded_user_id"` //当前用户号号是否是binded后的
RealMessageType string `json:"real_message_type,omitempty"` //当前信息的真实类型 group group_private guild guild_private
IsBindedGroupId bool `json:"is_binded_group_id,omitempty"` //当前群号是否是binded后的
IsBindedUserId bool `json:"is_binded_user_id,omitempty"` //当前用户号号是否是binded后的
}

// 私聊信息事件
Expand All @@ -103,12 +104,12 @@ type OnebotPrivateMessage struct {
Time int64 `json:"time"`
Avatar string `json:"avatar,omitempty"`
Echo string `json:"echo,omitempty"`
Message interface{} `json:"message"` // For array format
MessageSeq int `json:"message_seq"` // Optional field
Font int `json:"font"` // Optional field
UserID int64 `json:"user_id"` // Can be either string or int depending on logic
RealMessageType string `json:"real_message_type"` //当前信息的真实类型 group group_private guild guild_private
IsBindedUserId bool `json:"is_binded_user_id"` //当前用户号号是否是binded后的
Message interface{} `json:"message"` // For array format
MessageSeq int `json:"message_seq"` // Optional field
Font int `json:"font"` // Optional field
UserID int64 `json:"user_id"` // Can be either string or int depending on logic
RealMessageType string `json:"real_message_type,omitempty"` //当前信息的真实类型 group group_private guild guild_private
IsBindedUserId bool `json:"is_binded_user_id,omitempty"` //当前用户号号是否是binded后的
}

type PrivateSender struct {
Expand Down Expand Up @@ -261,10 +262,23 @@ func (p *Processors) BroadcastMessageToAll(message map[string]interface{}) error
return fmt.Errorf(strings.Join(errors, "; "))
}

PostMessageToUrls(message)
//判断是否填写了反向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 列表
Expand Down Expand Up @@ -536,7 +550,7 @@ func performBindOperation(cleanedMessage string, data interface{}, Type string,
if err != nil {
SendMessage(err.Error(), data, Type, p, p2)
} else {
SendMessage("绑定成功,目前状态:\n当前真实值 "+now+"\n当前虚拟值 "+new, data, Type, p, p2)
SendMessage("绑定成功,目前状态:\n当前真实值 "+new+"\n当前虚拟值 "+now, data, Type, p, p2)
}

return nil
Expand Down Expand Up @@ -825,3 +839,16 @@ func updateMappings(userid64, vuinValue, GroupID64, idValue int64) error {
}
return nil
}

// GenerateAvatarURL 生成根据给定 userID 和随机 q 值组合的 QQ 头像 URL
func GenerateAvatarURL(userID int64) (string, error) {
// 使用 crypto/rand 生成更安全的随机数
n, err := rand.Int(rand.Reader, big.NewInt(5))
if err != nil {
return "", err
}
qNumber := n.Int64() + 1 // 产生 1 到 5 的随机数

// 构建并返回 URL
return fmt.Sprintf("http://q%d.qlogo.cn/g?b=qq&nk=%d&s=640", qNumber, userID), nil
}
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type Settings struct {
PostSecret []string `yaml:"post_secret"`
PostMaxRetries []int `yaml:"post_max_retries"`
PostRetriesInterval []int `yaml:"post_retries_interval"`
NativeOb11 bool `yaml:"native_ob11"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -1221,3 +1222,15 @@ func GetPostRetriesInterval() []int {
}
return instance.Settings.PostRetriesInterval
}

// 获取GetTransferUrl的值
func GetNativeOb11() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to NativeOb11 value.")
return false
}
return instance.Settings.NativeOb11
}
10 changes: 10 additions & 0 deletions idmap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func CheckValue(id string, value int64) bool {
return generatedValue != value
}

func CheckValuev2(value int64) bool {
var isbinded bool
if value < 100000 {
isbinded = false
} else {
isbinded = true
}
return isbinded
}

// 根据a储存b
func StoreID(id string) (int64, error) {
var newRow int64
Expand Down
1 change: 1 addition & 0 deletions template/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ settings:
reconnect_times : 100 #反向ws连接失败后的重试次数,希望一直重试,可设置9999
heart_beat_interval : 10 #反向ws心跳间隔 单位秒 推荐5-10
launch_reconnect_times : 1 #启动时尝试反向ws连接次数,建议先打开应用端再开启gensokyo,因为启动时连接会阻塞webui启动,默认只连接一次,可自行增大
native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性
#正向ws设置
ws_server_path : "ws" #默认监听0.0.0.0:port/ws_server_path 若有安全需求,可不放通port到公网,或设置ws_server_token 若想监听/ 可改为"",若想监听到不带/地址请写nil
Expand Down
1 change: 1 addition & 0 deletions template/config_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ settings:
launch_reconnect_times : 1 #启动时尝试反向ws连接次数,建议先打开应用端再开启gensokyo,因为启动时连接会阻塞webui启动,默认只连接一次,可自行增大
white_bypass : [] #格式[1,2,3],白名单不生效的群,用于设置自己的灰度沙箱,避免测试时候反复开关白名单的不便.
transfer_url : true #默认开启,关闭后自理url发送,配置server_dir为你的域名,配置crt和key后,将域名/url和/image在q.qq.com后台通过校验,自动使用302跳转处理机器人发出的所有域名.
native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性

0 comments on commit 09d4479

Please sign in to comment.