feat: Add command handler(message command)
This commit is contained in:
parent
2e12ff158d
commit
1ec391d478
3 changed files with 73 additions and 2 deletions
49
commands/discommand.go
Normal file
49
commands/discommand.go
Normal 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
17
commands/help.go
Normal 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")
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue