Add command aliases, cleanup types and order

This commit is contained in:
Michał Gdula 2025-04-02 19:23:17 +01:00
parent a70ca23e41
commit 10acb07216
4 changed files with 58 additions and 83 deletions

View file

@ -21,14 +21,16 @@ type Config struct {
}
type App struct {
Config Config
Commands map[string]Callback
Config Config
Commands map[string]Callback
CommandAliases map[string]string
}
func NewApp(config Config) *App {
return &App{
Config: config,
Commands: make(map[string]Callback),
Config: config,
Commands: make(map[string]Callback),
CommandAliases: make(map[string]string),
}
}
@ -36,6 +38,10 @@ func (a *App) RegisterCommand(cmd string, f Callback) {
a.Commands[cmd] = f
}
func (a *App) RegisterCommandAlias(alias, cmd string) {
a.CommandAliases[alias] = cmd
}
func (a *App) Run() {
dg, err := discordgo.New("Bot " + a.Config.Token)
if err != nil {
@ -92,6 +98,11 @@ func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCrea
cmd = strings.TrimPrefix(cmd, a.Config.Prefix)
cmd, args, _ = strings.Cut(cmd, " ")
alias, ok := a.CommandAliases[cmd]
if ok {
cmd = alias
}
callback, ok := a.Commands[cmd]
if !ok {
// Falling back to default help command