Files
mygoTgChanBot/internal/telegram/handlers_cat.go
2026-02-05 21:29:48 +08:00

81 lines
1.9 KiB
Go

package telegram
import (
"fmt"
"strconv"
"strings"
tele "gopkg.in/telebot.v3"
)
func (b *Bot) handleCatAdd(c tele.Context) error {
args := strings.Fields(c.Message().Payload)
if len(args) == 0 {
return c.Reply("用法: /cat_add <名称> [排序]\n例如: /cat_add iOS 1")
}
name := args[0]
order := 0
if len(args) > 1 {
if o, err := strconv.Atoi(args[1]); err == nil {
order = o
}
}
if err := b.storage.CreateCategory(name, order); err != nil {
return c.Reply(fmt.Sprintf("❌ 创建失败: %v", err))
}
return c.Reply(fmt.Sprintf("✅ 分类 [%s] 已创建 (排序: %d)", name, order))
}
func (b *Bot) handleCatDel(c tele.Context) error {
name := strings.TrimSpace(c.Message().Payload)
if name == "" {
return c.Reply("用法: /cat_del <名称>")
}
if err := b.storage.DeleteCategory(name); err != nil {
return c.Reply(fmt.Sprintf("❌ 删除失败: %v", err))
}
return c.Reply(fmt.Sprintf("✅ 分类 [%s] 已删除", name))
}
func (b *Bot) handleCatRename(c tele.Context) error {
args := strings.Fields(c.Message().Payload)
if len(args) < 2 {
return c.Reply("用法: /cat_rename <旧名称> <新名称>\n例如: /cat_rename iOS Apple")
}
oldName := args[0]
newName := args[1]
if err := b.storage.RenameCategory(oldName, newName); err != nil {
return c.Reply(fmt.Sprintf("❌ 重命名失败: %v", err))
}
b.toc.TriggerUpdate()
return c.Reply(fmt.Sprintf("✅ 分类 [%s] 已重命名为 [%s]", oldName, newName))
}
func (b *Bot) handleCatList(c tele.Context) error {
categories, err := b.storage.ListCategories()
if err != nil {
return c.Reply(fmt.Sprintf("❌ 获取失败: %v", err))
}
if len(categories) == 0 {
return c.Reply("📂 暂无分类\n使用 /cat_add <名称> 创建")
}
var sb strings.Builder
sb.WriteString("📂 分类列表:\n\n")
for i, cat := range categories {
sb.WriteString(fmt.Sprintf("%d. %s (排序: %d)\n", i+1, cat.Name, cat.Order))
}
return c.Reply(sb.String())
}