From 965a976d44f19c3eac32733289aa5bb209c5b38f Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Tue, 25 Mar 2025 21:46:51 +0900 Subject: [PATCH] feat: Add DiscommandStruct.messageRuns --- commands/discommand.go | 25 ++++++++++++++++--------- commands/help.go | 9 +++++++-- configs/config.go | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/commands/discommand.go b/commands/discommand.go index 593abc9..aefcbff 100644 --- a/commands/discommand.go +++ b/commands/discommand.go @@ -4,6 +4,8 @@ import ( "github.com/bwmarrin/discordgo" ) +// type messageRun func(s *discordgo.Session, m *discordgo.MessageCreate) + type DetailedDescription struct { Usage string Examples []string @@ -17,17 +19,21 @@ type Command struct { } type DiscommandStruct struct { - Commands map[string]Command - Aliases map[string]string + Commands map[string]Command + Aliases map[string]string + messageRuns map[string]interface{} } func new() *DiscommandStruct { discommand := DiscommandStruct{ - Commands: map[string]Command{}, - Aliases: map[string]string{}, + Commands: map[string]Command{}, + Aliases: map[string]string{}, + messageRuns: map[string]interface{}{}, } discommand.loadCommands(HelpCommand) + + discommand.addMessageRun(HelpCommand.Name, HelpCommand.helpMessageRun) return &discommand } @@ -40,12 +46,13 @@ func (d *DiscommandStruct) loadCommands(command Command) { } } +func (d *DiscommandStruct) addMessageRun(name string, run func(s *discordgo.Session, m *discordgo.MessageCreate)) { + d.messageRuns[name] = run +} + func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) { - // 극한의 하드코딩 으아악 - switch command { - case "도움말": - HelpCommand.MessageRun(s, m) - } + // 더욱 나아진 + d.messageRuns[command].(func(s *discordgo.Session, m *discordgo.MessageCreate))(s, m) } var Discommand *DiscommandStruct = new() diff --git a/commands/help.go b/commands/help.go index adc3f10..26a21e0 100644 --- a/commands/help.go +++ b/commands/help.go @@ -1,6 +1,10 @@ package commands -import "github.com/bwmarrin/discordgo" +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) var HelpCommand Command = Command{ Name: "도움말", @@ -12,6 +16,7 @@ var HelpCommand Command = Command{ }, } -func (c *Command) MessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { +func (c *Command) helpMessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { + fmt.Println(c.Name) s.ChannelMessageSend(m.ChannelID, "asdf") } diff --git a/configs/config.go b/configs/config.go index 6d9f525..53ef630 100644 --- a/configs/config.go +++ b/configs/config.go @@ -8,7 +8,7 @@ import ( "github.com/joho/godotenv" ) -var MUFFIN_VERSION = "0.0.0-gopher_canary.250324a" +var MUFFIN_VERSION = "0.0.0-gopher_canary.250325a" type botConfig struct { Token string