From 1ec391d4785d8202b22e1a892e956dd7c0c6c673 Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Wed, 19 Mar 2025 21:21:39 +0900 Subject: [PATCH] feat: Add command handler(message command) --- commands/discommand.go | 49 ++++++++++++++++++++++++++++++++++++++++ commands/help.go | 17 ++++++++++++++ handler/messageCreate.go | 9 ++++++-- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 commands/discommand.go create mode 100644 commands/help.go diff --git a/commands/discommand.go b/commands/discommand.go new file mode 100644 index 0000000..74a10aa --- /dev/null +++ b/commands/discommand.go @@ -0,0 +1,49 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" +) + +type DetailedDescription struct { + Usage string + Examples []string +} + +type Command struct { + Name string + Aliases []string + Description string + DetailedDescription DetailedDescription +} + +type DiscommandStruct struct { + Commands map[string]Command + Aliases map[string]string +} + +func new() *DiscommandStruct { + discommand := DiscommandStruct{} + discommand.Commands = make(map[string]Command) + discommand.Aliases = make(map[string]string) + discommand.loadCommands(HelpCommand) + return &discommand +} + +func (d *DiscommandStruct) loadCommands(command Command) { + d.Commands[command.Name] = command + d.Aliases[command.Name] = command.Name + + for _, alias := range command.Aliases { + d.Aliases[alias] = command.Name + } +} + +func (d *DiscommandStruct) MessageRun(command string, s *discordgo.Session, m *discordgo.MessageCreate) { + // 극한의 하드코딩 으아악 + switch command { + case "도움말": + HelpCommand.MessageRun(s, m) + } +} + +var Discommand *DiscommandStruct = new() diff --git a/commands/help.go b/commands/help.go new file mode 100644 index 0000000..adc3f10 --- /dev/null +++ b/commands/help.go @@ -0,0 +1,17 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +var HelpCommand Command = Command{ + Name: "도움말", + Aliases: []string{"도움", "명령어", "help"}, + Description: "기본적인 사용ㅂ법이에요.", + DetailedDescription: DetailedDescription{ + Usage: "머핀아 도움말 [명령어]", + Examples: []string{"머핀아 도움말", "머핀아 도움말 배워"}, + }, +} + +func (c *Command) MessageRun(s *discordgo.Session, m *discordgo.MessageCreate) { + s.ChannelMessageSend(m.ChannelID, "asdf") +} diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 5e09e83..d3e313f 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/bwmarrin/discordgo" + "gitlab.wh64.net/muffin/goMuffin/commands" "gitlab.wh64.net/muffin/goMuffin/configs" ) @@ -16,9 +17,13 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if strings.HasPrefix(m.Content, config.Prefix) { content := strings.TrimPrefix(m.Content, config.Prefix) - if content == "안녕" { - s.ChannelMessageSend(m.ChannelID, "안녕") + command := commands.Discommand.Aliases[content] + + if command == "" { + return } + + commands.Discommand.MessageRun(command, s, m) } else { return }