Milen/commands/commands.go

78 lines
2.1 KiB
Go

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", "<message link> <emoji> <role to assign>", handleAutorole, 3, true},
command{"ignorechannel", "", handleIgnoreChannel, 0, true},
command{"unignorechannel", "", handleUnignoreChannel, 0, true},
command{"leaderboard", "", handleLeaderboard, 0, true},
command{"level", "", handleLevel, 0, false},
command{"recalclevel", "", handleRecalculateLevel, 0, false},
command{"roll", "[[[number of dice]d]<number of sides>]", handleRoll, 0, false},
}
func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot || m.GuildID == "" {
return
}
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
util.SendFailEmbed(
dg, m.ChannelID, "Error",
"A fatal error has occurred while processing this command. This event has been reported.",
)
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
}
commandName := strings.ToLower(split[1])
for _, v := range commands {
if commandName != 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:])
}