package commands import ( "fmt" "strings" "github.com/bwmarrin/discordgo" "gitea.teamortix.com/chanbakjsd/Milen/util" ) type command struct { Name string Usage string Processor func(*discordgo.Session, *discordgo.MessageCreate, []string) MinimumArgument int ExactArgument bool } var commands = []command{ command{"autorole", " ", handleAutorole, 3, true}, command{"recalclevel", "", handleRecalculateLevel, 0, false}, command{"level", "", handleLevel, 0, false}, } func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.Bot { return } defer func() { if r := recover(); r != nil { // Oh crap. We recovered from a panic. if val, ok := r.(error); ok { util.ReportError(dg, val) } else { util.ReportError(dg, fmt.Errorf("%T: %v", r, r)) } } }() split := strings.Split(m.Content, " ") prefix := strings.ToLower(split[0]) if prefix != "milen," && prefix != "milen" { return } if len(split) == 1 { util.SendFailEmbed(dg, m.ChannelID, ":(", "Did you just call me out for no reason? Please specify what you want me to do.") return } for _, v := range commands { if split[1] != v.Name { continue } if (v.ExactArgument && len(split) != v.MinimumArgument+2) || (!v.ExactArgument && len(split) < v.MinimumArgument+2) { util.SendFailEmbed(dg, m.ChannelID, "Incorrect Usage", "Usage: `milen "+v.Name+" "+v.Usage+"`") return } if len(split) == 2 { v.Processor(dg, m, []string{}) return } v.Processor(dg, m, split[2:]) return } handleMacro(dg, m, split[1:]) }