Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test4 #34

Merged
merged 53 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
fad0d50
Compiled main.go and pushed changes
Hoshinonyaruko Oct 23, 2023
fda2572
test
Hoshinonyaruko Oct 23, 2023
34cfac1
适配了频道私聊,用bolt数据库取代ini
Hoshinonyaruko Oct 23, 2023
7fc1f10
适配了nonebot2
Hoshinonyaruko Oct 24, 2023
28f9a73
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo
Hoshinonyaruko Oct 24, 2023
9fadda4
适配私域
Hoshinonyaruko Oct 24, 2023
1336c6a
add license
Hoshinonyaruko Oct 24, 2023
b094949
add a lot
Hoshinonyaruko Oct 25, 2023
f4787f5
Resolved merge conflicts
Hoshinonyaruko Oct 25, 2023
86698c6
trss support
Hoshinonyaruko Oct 26, 2023
045f3e9
fix
Hoshinonyaruko Oct 26, 2023
618561c
add action
Hoshinonyaruko Oct 26, 2023
7e803aa
add action
Hoshinonyaruko Oct 26, 2023
ece4cef
add action
Hoshinonyaruko Oct 26, 2023
f864a50
fixbug
Hoshinonyaruko Oct 26, 2023
4564731
add wss
Hoshinonyaruko Oct 26, 2023
070e4df
merge
Hoshinonyaruko Oct 26, 2023
84e8fe9
bugfix
Hoshinonyaruko Oct 26, 2023
0e4fd50
fix action
Hoshinonyaruko Oct 26, 2023
fe2234a
fix action again
Hoshinonyaruko Oct 26, 2023
cf67a11
fix action againnn
Hoshinonyaruko Oct 26, 2023
0875837
fa
Hoshinonyaruko Oct 26, 2023
3a3c33f
fix
Hoshinonyaruko Oct 26, 2023
8789fa8
add a lot
Hoshinonyaruko Oct 27, 2023
7943466
add ws server token
Hoshinonyaruko Oct 28, 2023
4660983
merge
Hoshinonyaruko Oct 28, 2023
2a1026a
bugifx
Hoshinonyaruko Oct 28, 2023
4186970
fix
Hoshinonyaruko Oct 28, 2023
2f26828
fixat
Hoshinonyaruko Oct 28, 2023
007e1af
bugfix
Hoshinonyaruko Oct 28, 2023
c41fd77
bugfix
Hoshinonyaruko Oct 28, 2023
d8354b7
test
Hoshinonyaruko Oct 28, 2023
6e3c63c
test
Hoshinonyaruko Oct 28, 2023
f08d9d7
test2
Hoshinonyaruko Oct 28, 2023
4dd6e15
add url service
Hoshinonyaruko Oct 31, 2023
1f8c174
add url service
Hoshinonyaruko Oct 31, 2023
240f569
add url service
Hoshinonyaruko Oct 31, 2023
b191cb7
bugfix
Hoshinonyaruko Oct 31, 2023
42daab3
merge
Hoshinonyaruko Oct 31, 2023
8fec03f
fix
Hoshinonyaruko Oct 31, 2023
cca528b
fix
Hoshinonyaruko Oct 31, 2023
463a182
fix
Hoshinonyaruko Oct 31, 2023
e9187a1
bug fix
Hoshinonyaruko Nov 1, 2023
5ba2815
fix
Hoshinonyaruko Nov 1, 2023
c16e1af
fix
Hoshinonyaruko Nov 1, 2023
ec7863d
test
Hoshinonyaruko Nov 2, 2023
e8a7e73
ok
Hoshinonyaruko Nov 2, 2023
122d17d
add webui
Hoshinonyaruko Nov 2, 2023
4e9c209
merge
Hoshinonyaruko Nov 2, 2023
b502d61
add image_compress
Hoshinonyaruko Nov 3, 2023
3519e3e
merge
Hoshinonyaruko Nov 3, 2023
530ff96
bug fix
Hoshinonyaruko Nov 3, 2023
155a701
fix cq code
Hoshinonyaruko Nov 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
mylog.Printf("Error storing ID: %v", err)
return nil
}

//映射str的messageID到int
messageID64, err := idmap.StoreIDv2(data.ID)
if err != nil {
Expand Down Expand Up @@ -100,7 +99,6 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
//储存当前群或频道号的类型
idmap.WriteConfigv2(fmt.Sprint(GroupID64), "type", "group")
echo.AddMsgType(AppIDString, GroupID64, "group")

// 调试
PrintStructWithFieldNames(groupMsg)

Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Settings struct {
DeveloperLog bool `yaml:"developer_log"`
Username string `yaml:"server_user_name"`
Password string `yaml:"server_user_password"`
ImageLimit int `yaml:"image_sizelimit"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -367,3 +368,16 @@ func GetServerUserPassword() string {
}
return instance.Settings.Password
}

// GetImageLimit 返回 ImageLimit 的值
func GetImageLimit() int {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get image limit value.")
return 0 // 或者返回一个默认的 ImageLimit 值
}

return instance.Settings.ImageLimit
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mvdan/xurls v1.1.0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
)

replace github.com/tencent-connect/botgo => ./botgo
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mvdan/xurls v1.1.0 h1:OpuDelGQ1R1ueQ6sSryzi6P+1RtBpfQHM8fJwlE45ww=
github.com/mvdan/xurls v1.1.0/go.mod h1:tQlNn3BED8bE/15hnSL2HLkDeLWpNPAwtw7wkEq44oU=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
98 changes: 98 additions & 0 deletions handlers/get_group_member_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package handlers

import (
"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/tencent-connect/botgo/openapi"
)

// 初始化handler,在程序启动时会被调用
func init() {
callapi.RegisterHandler("get_group_member_info", getGroupMemberInfo)
}

// 成员信息的结构定义
type MemberInfo struct {
UserID int64 `json:"user_id"`
GroupID int64 `json:"group_id"`
Nickname string `json:"nickname"`
Card string `json:"card"`
Sex string `json:"sex"`
Age int32 `json:"age"`
Area string `json:"area"`
JoinTime int32 `json:"join_time"`
LastSentTime int32 `json:"last_sent_time"`
Level string `json:"level"`
Role string `json:"role"`
Unfriendly bool `json:"unfriendly"`
Title string `json:"title"`
TitleExpireTime int64 `json:"title_expire_time"`
CardChangeable bool `json:"card_changeable"`
ShutUpTimestamp int64 `json:"shut_up_timestamp"`
}

// 构建单个成员的响应数据
func buildResponseForSingleMember(memberInfo *MemberInfo, echoValue interface{}) map[string]interface{} {
// 构建成员数据的映射
memberMap := map[string]interface{}{
"group_id": memberInfo.GroupID,
"user_id": memberInfo.UserID,
"nickname": memberInfo.Nickname,
"card": memberInfo.Card,
"sex": memberInfo.Sex,
"age": memberInfo.Age,
"area": memberInfo.Area,
"join_time": memberInfo.JoinTime,
"last_sent_time": memberInfo.LastSentTime,
"level": memberInfo.Level,
"role": memberInfo.Role,
"unfriendly": memberInfo.Unfriendly,
"title": memberInfo.Title,
"title_expire_time": memberInfo.TitleExpireTime,
"card_changeable": memberInfo.CardChangeable,
"shut_up_timestamp": memberInfo.ShutUpTimestamp,
}

// 构建完整的响应映射
response := map[string]interface{}{
"retcode": 0,
"status": "ok",
"data": memberMap,
"echo": echoValue,
}

return response
}

// getGroupMemberInfo是处理获取群成员信息的函数
func getGroupMemberInfo(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) {
// 使用虚拟数据构造 MemberInfo
memberInfo := &MemberInfo{
UserID: 123456789, // 虚拟的 QQ 号
GroupID: 987654321, // 虚拟的群号
Nickname: "主人", // 虚拟昵称
Card: "主人",
Sex: "unknown", // 性别未知
Age: 20, // 虚拟年龄
Area: "虚拟地区",
JoinTime: 1630416000, // 虚拟加群时间戳
LastSentTime: 1630502400, // 虚拟最后发言时间戳
Level: "1", // 虚拟成员等级
Role: "member", // 角色为普通成员
Unfriendly: false, // 没有不良记录
Title: "虚拟头衔",
TitleExpireTime: 1630598800, // 虚拟头衔过期时间
CardChangeable: true, // 允许修改群名片
ShutUpTimestamp: 0, // 不在禁言中
}

// 构建响应JSON
responseJSON := buildResponseForSingleMember(memberInfo, message.Echo)
mylog.Printf("get_group_member_info: %s\n", responseJSON)

// 发送响应回去
err := client.SendMessage(responseJSON)
if err != nil {
mylog.Printf("发送消息时出错: %v", err)
}
}
21 changes: 18 additions & 3 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/hoshinonyaruko/gensokyo/callapi"
Expand Down Expand Up @@ -160,15 +161,19 @@ func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[strin

// at处理和链接处理
func transformMessageText(messageText string) string {
// 首先,将AppID替换为BotID
messageText = strings.ReplaceAll(messageText, AppID, BotID)

// 使用正则表达式来查找所有[CQ:at,qq=数字]的模式
re := regexp.MustCompile(`\[CQ:at,qq=(\d+)\]`)
messageText = re.ReplaceAllStringFunc(messageText, func(m string) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
realUserID, err := idmap.RetrieveRowByIDv2(submatches[1])
if err != nil {
// 如果出错,也替换成相应的格式,但使用原始QQ号
mylog.Printf("Error retrieving user ID: %v", err)
return m // 如果出错,返回原始匹配
return "<@!" + submatches[1] + ">"
}
return "<@!" + realUserID + ">"
}
Expand Down Expand Up @@ -208,11 +213,21 @@ func RevertTransformedText(data interface{}) string {

// 使用正则表达式来查找所有<@!数字>的模式
re := regexp.MustCompile(`<@!(\d+)>`)
// 使用正则表达式来替换找到的模式为[CQ:at,qq=数字]
// 使用正则表达式来替换找到的模式为[CQ:at,qq=用户ID]
messageText = re.ReplaceAllStringFunc(messageText, func(m string) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
return "[CQ:at,qq=" + submatches[1] + "]"
//映射用户id
userID64, err := idmap.StoreIDv2(submatches[1])
if err != nil {
//如果储存失败(数据库损坏)返回原始值
mylog.Printf("Error storing ID: %v", err)
return "[CQ:at,qq=" + submatches[1] + "]"
}
//类型转换
userIDStr := strconv.FormatInt(userID64, 10)
//经过转换的cq码
return "[CQ:at,qq=" + userIDStr + "]"
}
return m
})
Expand Down
24 changes: 22 additions & 2 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,19 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
MsgType: 0, // 默认文本类型
}
}
// 首先压缩图片 默认不压缩
compressedData, err := images.CompressSingleImage(imageData)
if err != nil {
mylog.Printf("Error compressing image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 压缩图片失败",
MsgID: id,
MsgType: 0, // 默认文本类型
}
}

// base64编码
base64Encoded := base64.StdEncoding.EncodeToString(imageData)
base64Encoded := base64.StdEncoding.EncodeToString(compressedData)

// 上传base64编码的图片并获取其URL
imageURL, err := images.UploadBase64ImageToServer(base64Encoded)
Expand Down Expand Up @@ -204,8 +214,18 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
mylog.Printf("failed to decode base64 image: %v", err)
return nil
}
// 首先压缩图片 默认不压缩
compressedData, err := images.CompressSingleImage(fileImageData)
if err != nil {
mylog.Printf("Error compressing image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 压缩图片失败",
MsgID: id,
MsgType: 0, // 默认文本类型
}
}
// 将解码的图片数据转换回base64格式并上传
imageURL, err := images.UploadBase64ImageToServer(base64.StdEncoding.EncodeToString(fileImageData))
imageURL, err := images.UploadBase64ImageToServer(base64.StdEncoding.EncodeToString(compressedData))
if err != nil {
mylog.Printf("failed to upload base64 image: %v", err)
return nil
Expand Down
14 changes: 12 additions & 2 deletions handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/hoshinonyaruko/gensokyo/images"
"github.com/hoshinonyaruko/gensokyo/mylog"

"github.com/hoshinonyaruko/gensokyo/echo"
Expand Down Expand Up @@ -143,9 +144,18 @@ func generateReplyMessage(id string, foundItems map[string][]string, messageText
}
return &reply, false
}

// 首先压缩图片
compressedData, err := images.CompressSingleImage(imageData)
if err != nil {
mylog.Printf("Error compressing image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 压缩图片失败",
MsgID: id,
MsgType: 0, // 默认文本类型
}, false
}
//base64编码
base64Encoded := base64.StdEncoding.EncodeToString(imageData)
base64Encoded := base64.StdEncoding.EncodeToString(compressedData)

// 当作base64图来处理
reply = dto.MessageToCreate{
Expand Down
6 changes: 4 additions & 2 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open

// 优先发送文本信息
if messageText != "" {
groupReply := generatePrivateMessage(messageID, nil, messageText)
groupReply := generateGroupMessage(messageID, nil, messageText)

// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
Expand All @@ -96,7 +96,8 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
var singleItem = make(map[string][]string)
singleItem[key] = urls

groupReply := generatePrivateMessage(messageID, singleItem, "")
//先试试用群里一样的处理逻辑,看看能跑不
groupReply := generateGroupMessage(messageID, singleItem, "")

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
Expand All @@ -119,6 +120,7 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
}
}

// 这里是只有群私聊会用到
func generatePrivateMessage(id string, foundItems map[string][]string, messageText string) interface{} {
if imageURLs, ok := foundItems["local_image"]; ok && len(imageURLs) > 0 {
// 本地发图逻辑 todo 适配base64图片
Expand Down
Loading
Loading