goMuffin/commands/discommand.go

176 lines
3.5 KiB
Go

package commands
import (
"sync"
"git.wh64.net/muffin/goMuffin/utils"
"github.com/bwmarrin/discordgo"
)
type modalRun func(ctx *ModalContext)
type messageRun func(ctx *MsgContext)
type chatInputRun func(ctx *ChatInputContext)
type componentRun func(ctx *ComponentContext)
type modalParse func(ctx *ModalContext) bool
type componentParse func(ctx *ComponentContext) bool
type Category string
type DetailedDescription struct {
Usage string
Examples []string
}
type Command struct {
*discordgo.ApplicationCommand
Aliases []string
DetailedDescription *DetailedDescription
Category Category
MessageRun messageRun
ChatInputRun chatInputRun
}
type DiscommandStruct struct {
Commands map[string]*Command
Components []*Component
Aliases map[string]string
Modals []*Modal
}
type MsgContext struct {
Session *discordgo.Session
Msg *discordgo.MessageCreate
Args *[]string
Command *Command
}
type ChatInputContext struct {
Session *discordgo.Session
Inter *utils.InteractionCreate
Command *Command
}
type ComponentContext struct {
Session *discordgo.Session
Inter *utils.InteractionCreate
Component *Component
}
type ModalContext struct {
Inter *utils.InteractionCreate
Modal *Modal
}
type Component struct {
Parse componentParse
Run componentRun
}
type Modal struct {
Parse modalParse
Run modalRun
}
const (
Chatting Category = "채팅"
General Category = "일반"
)
var (
commandMutex sync.Mutex
componentMutex sync.Mutex
modalMutex sync.Mutex
)
func new() *DiscommandStruct {
discommand := DiscommandStruct{
Commands: map[string]*Command{},
Aliases: map[string]string{},
Components: []*Component{},
Modals: []*Modal{},
}
return &discommand
}
func (d *DiscommandStruct) LoadCommand(c *Command) {
defer commandMutex.Unlock()
commandMutex.Lock()
d.Commands[c.Name] = c
d.Aliases[c.Name] = c.Name
for _, alias := range c.Aliases {
d.Aliases[alias] = c.Name
}
}
func (d *DiscommandStruct) LoadComponent(c *Component) {
defer componentMutex.Unlock()
componentMutex.Lock()
d.Components = append(d.Components, c)
}
func (d *DiscommandStruct) LoadModal(m *Modal) {
defer modalMutex.Unlock()
modalMutex.Lock()
d.Modals = append(d.Modals, m)
}
func (d *DiscommandStruct) MessageRun(name string, s *discordgo.Session, m *discordgo.MessageCreate, args []string) {
if command, ok := d.Commands[name]; ok {
command.MessageRun(&MsgContext{s, m, &args, command})
}
}
func (d *DiscommandStruct) ChatInputRun(name string, s *discordgo.Session, i *discordgo.InteractionCreate) {
if command, ok := d.Commands[name]; ok {
command.ChatInputRun(&ChatInputContext{s, &utils.InteractionCreate{
InteractionCreate: i,
Session: s,
Options: utils.GetInteractionOptions(i),
}, command})
}
}
func (d *DiscommandStruct) ComponentRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
data := &ComponentContext{
Session: s,
Inter: &utils.InteractionCreate{
InteractionCreate: i,
Session: s,
},
}
for _, c := range d.Components {
data.Component = c
if !c.Parse(data) {
continue
}
c.Run(data)
break
}
}
func (d *DiscommandStruct) ModalRun(s *discordgo.Session, i *discordgo.InteractionCreate) {
data := &ModalContext{
Inter: &utils.InteractionCreate{
InteractionCreate: i,
Session: s,
},
}
for _, m := range d.Modals {
data.Modal = m
if !m.Parse(data) {
continue
}
m.Run(data)
break
}
}
var Discommand *DiscommandStruct = new()