feat: refactor command registration to use a centralized handler list

This commit is contained in:
krau
2025-09-13 10:37:43 +08:00
parent eef051de3b
commit 697e419643
2 changed files with 28 additions and 25 deletions

View File

@@ -19,7 +19,7 @@ import (
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
) )
func Init(ctx context.Context) (<-chan struct{}) { func Init(ctx context.Context) <-chan struct{} {
log.FromContext(ctx).Info("初始化 Bot...") log.FromContext(ctx).Info("初始化 Bot...")
resultChan := make(chan struct { resultChan := make(chan struct {
client *gotgproto.Client client *gotgproto.Client
@@ -75,18 +75,9 @@ func Init(ctx context.Context) (<-chan struct{}) {
client.API().BotsSetBotCommands(ctx, &tg.BotsSetBotCommandsRequest{ client.API().BotsSetBotCommands(ctx, &tg.BotsSetBotCommandsRequest{
Scope: &tg.BotCommandScopeDefault{}, Scope: &tg.BotCommandScopeDefault{},
}) })
commands := []tg.BotCommand{ commands := make([]tg.BotCommand, 0, len(handlers.CommandHandlers))
{Command: "start", Description: "开始使用"}, for _, info := range handlers.CommandHandlers {
{Command: "help", Description: "显示帮助"}, commands = append(commands, tg.BotCommand{Command: info.Cmd, Description: info.Desc})
{Command: "silent", Description: "开启/关闭静默模式"},
{Command: "storage", Description: "设置默认存储端"},
{Command: "save", Description: "保存文件"},
{Command: "dir", Description: "管理存储文件夹"},
{Command: "rule", Description: "管理规则"},
}
if config.C().Telegram.Userbot.Enable {
commands = append(commands, tg.BotCommand{Command: "watch", Description: "监听聊天"})
commands = append(commands, tg.BotCommand{Command: "unwatch", Description: "取消监听聊天"})
} }
_, err = client.API().BotsSetBotCommands(ctx, &tg.BotsSetBotCommandsRequest{ _, err = client.API().BotsSetBotCommands(ctx, &tg.BotsSetBotCommandsRequest{
Scope: &tg.BotCommandScopeDefault{}, Scope: &tg.BotCommandScopeDefault{},

View File

@@ -24,6 +24,27 @@ import (
"github.com/rs/xid" "github.com/rs/xid"
) )
type DescCommandHandler struct {
Cmd string
Desc string
handler func(ctx *ext.Context, u *ext.Update) error
}
var CommandHandlers = []DescCommandHandler{
{"start", "开始使用", handleHelpCmd},
{"silent", "切换静默模式", handleSilentCmd},
{"storage", "设置默认存储端", handleStorageCmd},
{"dir", "管理存储文件夹", handleDirCmd},
{"rule", "管理自动存储规则", handleRuleCmd},
{"watch", "监听聊天(UserBot)", handleWatchCmd},
{"unwatch", "取消监听聊天(UserBot)", handleUnwatchCmd},
{"save", "保存文件", handleSilentMode(handleSaveCmd, handleSilentSaveReplied)},
{"config", "修改配置", handleConfigCmd},
{"fnametmpl", "设置文件命名模板", handleConfigFnameTmpl},
{"update", "检查更新", handleUpdateCmd},
{"help", "显示帮助", handleHelpCmd},
}
func Register(disp dispatcher.Dispatcher) { func Register(disp dispatcher.Dispatcher) {
disp.AddHandler(handlers.NewMessage(filters.Message.ChatType(filters.ChatTypeChannel), func(ctx *ext.Context, u *ext.Update) error { disp.AddHandler(handlers.NewMessage(filters.Message.ChatType(filters.ChatTypeChannel), func(ctx *ext.Context, u *ext.Update) error {
return dispatcher.EndGroups return dispatcher.EndGroups
@@ -32,18 +53,9 @@ func Register(disp dispatcher.Dispatcher) {
return dispatcher.EndGroups return dispatcher.EndGroups
})) }))
disp.AddHandler(handlers.NewMessage(filters.Message.All, checkPermission)) disp.AddHandler(handlers.NewMessage(filters.Message.All, checkPermission))
disp.AddHandler(handlers.NewCommand("start", handleHelpCmd)) for _, info := range CommandHandlers {
disp.AddHandler(handlers.NewCommand("help", handleHelpCmd)) disp.AddHandler(handlers.NewCommand(info.Cmd, info.handler))
disp.AddHandler(handlers.NewCommand("silent", handleSilentCmd)) }
disp.AddHandler(handlers.NewCommand("storage", handleStorageCmd))
disp.AddHandler(handlers.NewCommand("dir", handleDirCmd))
disp.AddHandler(handlers.NewCommand("rule", handleRuleCmd))
disp.AddHandler(handlers.NewCommand("watch", handleWatchCmd))
disp.AddHandler(handlers.NewCommand("unwatch", handleUnwatchCmd))
disp.AddHandler(handlers.NewCommand("save", handleSilentMode(handleSaveCmd, handleSilentSaveReplied)))
disp.AddHandler(handlers.NewCommand("config", handleConfigCmd))
disp.AddHandler(handlers.NewCommand("fnametmpl", handleConfigFnameTmpl))
disp.AddHandler(handlers.NewCommand("update", handleUpdateCmd))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("update"), handleUpdateCallback)) disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("update"), handleUpdateCallback))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeAdd), handleAddCallback)) disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeAdd), handleAddCallback))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeSetDefault), handleSetDefaultCallback)) disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeSetDefault), handleSetDefaultCallback))