added switch to make commands case-insensitive

This commit is contained in:
kageru 2018-06-05 11:50:40 +02:00
parent 9043a9dbac
commit ab5ce7cc5f

View File

@ -27,6 +27,7 @@ type Command struct {
DeleteInput bool // defaults to false DeleteInput bool // defaults to false
DMOnly bool // defaults to false DMOnly bool // defaults to false
AdminOnly bool // defaults to false AdminOnly bool // defaults to false
IgnoreCase bool // defaults to false
// for custom commands that go beyond prints and deletions // for custom commands that go beyond prints and deletions
Function func(*discordgo.Session, *discordgo.MessageCreate) Function func(*discordgo.Session, *discordgo.MessageCreate)
} }
@ -37,6 +38,9 @@ func registerCommand(command Command) {
fmt.Println("Cannot register a command with no trigger. Skipping.") fmt.Println("Cannot register a command with no trigger. Skipping.")
return return
} }
if command.IgnoreCase {
command.Trigger = strings.ToLower(command.Trigger)
}
commands = append(commands, command) commands = append(commands, command)
} }
@ -47,28 +51,32 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
fmt.Println(m.Content) fmt.Println(m.Content)
for _, command := range commands { for _, command := range commands {
content := m.Content
if command.IgnoreCase {
content = strings.ToLower(content)
}
if command.RequiresMention { if command.RequiresMention {
command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID) command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID)
} }
switch command.Type { switch command.Type {
case CommandTypePrefix: case CommandTypePrefix:
if strings.HasPrefix(m.Content, command.Trigger) { if strings.HasPrefix(content, command.Trigger) {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeFullMatch: case CommandTypeFullMatch:
if m.Content == command.Trigger { if content == command.Trigger {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeRegex: case CommandTypeRegex:
match, _ := regexp.MatchString(command.Trigger, m.Content) match, _ := regexp.MatchString(command.Trigger, content)
if match { if match {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
case CommandTypeContains: case CommandTypeContains:
if strings.Contains(m.Content, command.Trigger) { if strings.Contains(content, command.Trigger) {
executeCommand(s, m, command) executeCommand(s, m, command)
return return
} }
@ -118,3 +126,6 @@ func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSendEmbed(config.ModChannel, embed) s.ChannelMessageSendEmbed(config.ModChannel, embed)
} }
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content)
}