mirror of
https://github.com/woodchen-ink/Q58Bot.git
synced 2025-07-18 05:42:06 +08:00
- 数据库: - 从Database实例中移除AddKeyword和RemoveKeyword中的错误返回值。现在只在发生错误时返回错误。 - 为RemoveKeyword添加受影响行数的返回值,以判断关键词是否被成功删除。 - 优化AddPromptReply和DeletePromptReply,使用事务确保数据的一致性和完整性。 - 调整GetAllPromptReplies以强制刷新缓存并更新缓存时间。 - 核心: - 重构init.go中的全局变量初始化,移除多余注释。 - 在main.go中添加数据库关闭操作,确保资源在程序结束时被正确释放。 - 链接过滤器: - 重构LinkFilter服务,移除数据库实例字段。 - 更新LinkFilter中的数据加载和关键词操作,使用core包中的数据库方法。 - 添加LinkFilter的Close方法以关闭数据库连接。 - 消息处理器: - 移除message_handler.go中handleUpdate和handleAdminCommand中的数据库参数。 - 更新RunMessageHandler以初始化数据库并确保在结束时关闭连接。 - 调整handleListKeywords、handleAddKeyword、handleDeleteKeyword、handleDeleteContainingKeyword、handleListWhitelist、handleAddWhitelist和handleDeleteWhitelist,移除数据库参数。 - 提示回复: - 在prompt_reply.go中移除全局数据库变量。 - 更新SetPromptReply和DeletePromptReply,使用core.DB代替db。 - 调整GetPromptReply和ListPromptReplies使用更新后的数据库访问方法。 这些更改优化了代码结构,减少了全局状态,并提高了数据库操作的可靠性。
139 lines
4.2 KiB
Go
139 lines
4.2 KiB
Go
package prompt_reply
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/woodchen-ink/Q58Bot/core"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
func SetPromptReply(prompt, reply string) error {
|
|
err := core.DB.AddPromptReply(prompt, reply)
|
|
if err != nil {
|
|
log.Printf("提示回复: %s 设置提示回复失败: %v", time.Now().Format("2006/01/02 15:04:05"), err)
|
|
return err
|
|
}
|
|
|
|
// 获取当前所有的 prompt replies 来确认添加成功
|
|
promptReplies, err := core.DB.GetAllPromptReplies()
|
|
if err != nil {
|
|
log.Printf("提示回复: %s 添加后获取提示回复列表出错: %v", time.Now().Format("2006/01/02 15:04:05"), err)
|
|
} else {
|
|
log.Printf("提示回复: %s 设置提示回复成功。当前提示回复数量: %d", time.Now().Format("2006/01/02 15:04:05"), len(promptReplies))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DeletePromptReply(prompt string) error {
|
|
err := core.DB.DeletePromptReply(prompt)
|
|
if err != nil {
|
|
log.Printf("提示回复: %s 删除提示回复失败: %v", time.Now().Format("2006/01/02 15:04:05"), err)
|
|
return err
|
|
}
|
|
|
|
// 获取当前所有的 prompt replies 来确认删除成功
|
|
promptReplies, err := core.DB.GetAllPromptReplies()
|
|
if err != nil {
|
|
log.Printf("提示回复: %s 删除后获取提示回复列表出错: %v", time.Now().Format("2006/01/02 15:04:05"), err)
|
|
} else {
|
|
log.Printf("提示回复: %s 删除提示回复成功。当前提示回复数量: %d", time.Now().Format("2006/01/02 15:04:05"), len(promptReplies))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetPromptReply(message string) (string, bool) {
|
|
promptReplies, err := core.DB.GetAllPromptReplies()
|
|
if err != nil {
|
|
log.Printf("Error getting prompt replies: %v", err)
|
|
return "", false
|
|
}
|
|
|
|
message = strings.ToLower(message)
|
|
for prompt, reply := range promptReplies {
|
|
if strings.Contains(message, strings.ToLower(prompt)) {
|
|
return reply, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func ListPromptReplies() string {
|
|
replies, err := core.DB.GetAllPromptReplies()
|
|
if err != nil {
|
|
log.Printf("Error getting prompt replies: %v", err)
|
|
return "Error retrieving prompt replies"
|
|
}
|
|
|
|
if len(replies) == 0 {
|
|
return "No prompt replies found"
|
|
}
|
|
|
|
var result strings.Builder
|
|
for prompt, reply := range replies {
|
|
result.WriteString(fmt.Sprintf("Prompt: %s\nReply: %s\n\n", prompt, reply))
|
|
}
|
|
|
|
return result.String()
|
|
}
|
|
|
|
func HandlePromptCommand(bot *tgbotapi.BotAPI, message *tgbotapi.Message) {
|
|
if !core.IsAdmin(message.From.ID) {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "只有管理员才能使用此命令。"))
|
|
return
|
|
}
|
|
|
|
args := strings.SplitN(message.Text, " ", 3)
|
|
if len(args) < 2 {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "使用方法: /prompt set <提示词> <回复>\n/prompt delete <提示词>\n/prompt list"))
|
|
return
|
|
}
|
|
|
|
switch args[1] {
|
|
case "set":
|
|
if len(args) < 3 {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "使用方法: /prompt set <提示词> <回复>"))
|
|
return
|
|
}
|
|
promptAndReply := strings.SplitN(args[2], " ", 2)
|
|
if len(promptAndReply) < 2 {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "请同时提供提示词和回复。"))
|
|
return
|
|
}
|
|
err := SetPromptReply(promptAndReply[0], promptAndReply[1])
|
|
if err != nil {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("设置提示词失败:%v", err)))
|
|
return
|
|
}
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("已设置提示词 '%s' 的回复。", promptAndReply[0])))
|
|
case "delete":
|
|
if len(args) < 3 {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "使用方法: /prompt delete <提示词>"))
|
|
return
|
|
}
|
|
err := DeletePromptReply(args[2])
|
|
if err != nil {
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("删除提示词失败:%v", err)))
|
|
return
|
|
}
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("已删除提示词 '%s' 的回复。", args[2])))
|
|
case "list":
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, ListPromptReplies()))
|
|
default:
|
|
bot.Send(tgbotapi.NewMessage(message.Chat.ID, "未知的子命令。使用方法: /prompt set|delete|list"))
|
|
}
|
|
}
|
|
|
|
func CheckAndReplyPrompt(bot *tgbotapi.BotAPI, message *tgbotapi.Message) {
|
|
if reply, found := GetPromptReply(message.Text); found {
|
|
replyMsg := tgbotapi.NewMessage(message.Chat.ID, reply)
|
|
replyMsg.ReplyToMessageID = message.MessageID
|
|
bot.Send(replyMsg)
|
|
}
|
|
}
|