mirror of
https://github.com/awfufu/go-hurobot.git
synced 2026-03-01 05:29:43 +08:00
fix: silence GORM 'record not found' warnings for unconfigured RCON groups
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"go-hurobot/qbot"
|
||||
)
|
||||
|
||||
func customReply(c *qbot.Client, msg *qbot.Message) {
|
||||
// 2025-03-08 晚上,让 bot 在某 mc 群发电加的
|
||||
if msg.GroupID == 738943282 {
|
||||
if strings.Contains(msg.Raw, "厉厉厉害害") || strings.Contains(msg.Raw, "厉厉害害害") {
|
||||
return
|
||||
} else if strings.Contains(msg.Raw, "厉厉害害") {
|
||||
c.SendGroupMsg(msg.GroupID, strings.Replace(msg.Raw, "厉厉害害", "可可爱爱", -1), false)
|
||||
} else if strings.Contains(msg.Raw, "厉害害") {
|
||||
c.SendGroupMsg(msg.GroupID, strings.Replace(msg.Raw, "厉害害", "可爱爱", -1), false)
|
||||
} else if strings.Contains(msg.Raw, "厉害") {
|
||||
c.SendGroupMsg(msg.GroupID, strings.Replace(msg.Raw, "厉害", "可爱", -1), false)
|
||||
}
|
||||
}
|
||||
// 2025-03-08 ↑
|
||||
}
|
||||
16
handlers.go
16
handlers.go
@@ -13,20 +13,16 @@ func messageHandler(c *qbot.Client, msg *qbot.Message) {
|
||||
isCommand := cmds.HandleCommand(c, msg)
|
||||
defer qbot.SaveDatabase(msg, isCommand)
|
||||
|
||||
// Forward non-command messages to Minecraft if RCON is enabled
|
||||
if !isCommand {
|
||||
if isCommand {
|
||||
return
|
||||
} else {
|
||||
mc.ForwardMessageToMC(c, msg)
|
||||
}
|
||||
|
||||
if isCommand {
|
||||
if llm.NeedLLMResponse(msg) {
|
||||
llm.LLMMsgHandle(c, msg)
|
||||
return
|
||||
}
|
||||
if llm.LLMMsgHandle(c, msg) {
|
||||
return
|
||||
}
|
||||
if cmds.CheckUserEvents(c, msg) {
|
||||
return
|
||||
}
|
||||
customReply(c, msg)
|
||||
cmds.CheckUserEvents(c, msg)
|
||||
}
|
||||
}
|
||||
|
||||
42
llm/llm.go
42
llm/llm.go
@@ -14,6 +14,19 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NeedLLMResponse(msg *qbot.Message) bool {
|
||||
if strings.Contains(msg.Raw, "狐萝卜") {
|
||||
return true
|
||||
} else {
|
||||
for _, item := range msg.Array {
|
||||
if item.Type == qbot.At && item.Content == strconv.FormatUint(config.BotID, 10) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SendLLMRequest(supplier string, messages []openai.ChatCompletionMessageParamUnion, model string, temperature float64) (*openai.ChatCompletion, error) {
|
||||
var client *openai.Client
|
||||
|
||||
@@ -59,18 +72,7 @@ func SendLLMRequest(supplier string, messages []openai.ChatCompletionMessagePara
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func LLMMsgHandle(c *qbot.Client, msg *qbot.Message) bool {
|
||||
reply := false
|
||||
if reply = strings.Contains(msg.Raw, "狐萝卜"); !reply {
|
||||
for _, item := range msg.Array {
|
||||
if item.Type == qbot.At && item.Content == strconv.FormatUint(config.BotID, 10) {
|
||||
reply = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !reply {
|
||||
return false
|
||||
}
|
||||
func LLMMsgHandle(c *qbot.Client, msg *qbot.Message) {
|
||||
const prePrompt = `你是一个群聊聊天机器人,请你陪伴群友们聊天。
|
||||
1. 你的名字叫狐萝卜或狐萝bot,这个名字取自"狐robot",人设是一只萝莉狐娘,但请不要强调这个信息。
|
||||
2. 群聊不支持 Markdown 语法,不要使用。
|
||||
@@ -129,7 +131,7 @@ msg 有什么技术问题可以一起讨论哦
|
||||
|
||||
if err != nil || !llmCustomConfig.Enabled {
|
||||
c.SendMsg(msg, err.Error())
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
if llmCustomConfig.Supplier == "" || llmCustomConfig.Model == "" {
|
||||
@@ -178,7 +180,7 @@ msg 有什么技术问题可以一起讨论哦
|
||||
Find(&histories).Error
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
var userMap = make(map[uint64]UserInfo)
|
||||
@@ -247,7 +249,7 @@ msg 有什么技术问题可以一起讨论哦
|
||||
resp, err := SendLLMRequest(llmCustomConfig.Supplier, messages, llmCustomConfig.Model, 0.6)
|
||||
if err != nil {
|
||||
c.SendGroupMsg(msg.GroupID, err.Error(), false)
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
responseContent := resp.Choices[0].Message.Content
|
||||
@@ -261,7 +263,7 @@ msg 有什么技术问题可以一起讨论哦
|
||||
c.SendPrivateMsg(config.MasterID, "命令解析错误:\n"+err.Error(), false)
|
||||
c.SendPrivateMsg(config.MasterID, responseContent, false)
|
||||
c.SendPrivateMsg(config.MasterID, "消息来源:\ngroup_id="+strconv.FormatUint(msg.GroupID, 10)+"\nuser_id="+strconv.FormatUint(msg.UserID, 10)+"\nmsg="+msg.Content, false)
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
if resp != nil && resp.Usage.TotalTokens > 0 {
|
||||
@@ -269,14 +271,6 @@ msg 有什么技术问题可以一起讨论哦
|
||||
Where("user_id = ?", msg.UserID).
|
||||
Update("token_usage", gorm.Expr("token_usage + ?", resp.Usage.TotalTokens))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func formatMsg(t time.Time, name string, id uint64, msg string) string {
|
||||
return fmt.Sprintf("[%s] %s(id:%d)说: %q\n",
|
||||
t.In(time.FixedZone("UTC+8", 8*60*60)).Format("2006-01-02 15:04:05"),
|
||||
name, id, msg)
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
|
||||
21
mc/mc.go
21
mc/mc.go
@@ -1,7 +1,6 @@
|
||||
package mc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -9,7 +8,6 @@ import (
|
||||
"go-hurobot/qbot"
|
||||
|
||||
"github.com/gorcon/rcon"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ForwardMessageToMC forwards a group message to Minecraft server if RCON is enabled
|
||||
@@ -21,15 +19,14 @@ func ForwardMessageToMC(c *qbot.Client, msg *qbot.Message) {
|
||||
|
||||
// Get RCON configuration for this group
|
||||
var rconConfig qbot.GroupRconConfigs
|
||||
result := qbot.PsqlDB.Where("group_id = ?", msg.GroupID).First(&rconConfig)
|
||||
result := qbot.PsqlDB.Where("group_id = ?", msg.GroupID).Limit(1).Find(&rconConfig)
|
||||
|
||||
// Skip if RCON not configured or disabled
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
// Silently return if RCON not configured for this group
|
||||
return
|
||||
}
|
||||
// Log other database errors if needed
|
||||
return
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
// Silently return if RCON not configured for this group
|
||||
return
|
||||
}
|
||||
|
||||
@@ -41,8 +38,8 @@ func ForwardMessageToMC(c *qbot.Client, msg *qbot.Message) {
|
||||
var user qbot.Users
|
||||
nickname := msg.Card // Default to group card name
|
||||
|
||||
userResult := qbot.PsqlDB.Where("user_id = ?", msg.UserID).First(&user)
|
||||
if userResult.Error == nil && user.Nickname != "" {
|
||||
userResult := qbot.PsqlDB.Where("user_id = ?", msg.UserID).Limit(1).Find(&user)
|
||||
if userResult.Error == nil && userResult.RowsAffected > 0 && user.Nickname != "" {
|
||||
nickname = user.Nickname
|
||||
}
|
||||
|
||||
@@ -124,10 +121,10 @@ func getAtUserNickname(atContent string) string {
|
||||
|
||||
// Look up user in database
|
||||
var user qbot.Users
|
||||
result := qbot.PsqlDB.Where("user_id = ?", userID).First(&user)
|
||||
result := qbot.PsqlDB.Where("user_id = ?", userID).Limit(1).Find(&user)
|
||||
|
||||
// Use nick_name if available, otherwise try to get group member info
|
||||
if result.Error == nil && user.Nickname != "" {
|
||||
if result.Error == nil && result.RowsAffected > 0 && user.Nickname != "" {
|
||||
return user.Nickname
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user