feat: Add command handler(message command)

This commit is contained in:
Siwoo Jeon 2025-03-19 21:21:39 +09:00
parent 2e12ff158d
commit 1ec391d478
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 73 additions and 2 deletions

49
commands/discommand.go Normal file
View file

@ -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()

17
commands/help.go Normal file
View file

@ -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")
}

View file

@ -4,6 +4,7 @@ import (
"strings" "strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"gitlab.wh64.net/muffin/goMuffin/commands"
"gitlab.wh64.net/muffin/goMuffin/configs" "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) { if strings.HasPrefix(m.Content, config.Prefix) {
content := strings.TrimPrefix(m.Content, config.Prefix) content := strings.TrimPrefix(m.Content, config.Prefix)
if content == "안녕" { command := commands.Discommand.Aliases[content]
s.ChannelMessageSend(m.ChannelID, "안녕")
if command == "" {
return
} }
commands.Discommand.MessageRun(command, s, m)
} else { } else {
return return
} }