From d0246a6c4edae354bb2a8ecc133a7ae62c4e9cff Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 5 Jun 2018 12:31:26 +0200 Subject: [PATCH] added option for command cooldowns --- command.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/command.go b/command.go index 9a97f24..2424610 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "time" "github.com/bwmarrin/discordgo" "strings" "log" @@ -22,6 +23,7 @@ type Command struct { Output string // no output if unspecified OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified Type CommandType // defaults to Prefix + Cooldown int // defaults to 0 (no cooldown) OutputIsReply bool // defaults to false RequiresMention bool // defaults to false DeleteInput bool // defaults to false @@ -30,6 +32,8 @@ type Command struct { IgnoreCase bool // defaults to false // for custom commands that go beyond prints and deletions Function func(*discordgo.Session, *discordgo.MessageCreate) + + IsOnCooldown bool // don’t set this manually (it’s overwritten anyway) } @@ -50,7 +54,7 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { return } fmt.Println(m.Content) - for _, command := range commands { + for i, command := range commands { content := m.Content if command.IgnoreCase { content = strings.ToLower(content) @@ -61,34 +65,38 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { switch command.Type { case CommandTypePrefix: if strings.HasPrefix(content, command.Trigger) { - executeCommand(s, m, command) + executeCommand(s, m, command, i) return } case CommandTypeFullMatch: if content == command.Trigger { - executeCommand(s, m, command) + executeCommand(s, m, command, i) return } case CommandTypeRegex: match, _ := regexp.MatchString(command.Trigger, content) if match { - executeCommand(s, m, command) + executeCommand(s, m, command, i) return } case CommandTypeContains: if strings.Contains(content, command.Trigger) { - executeCommand(s, m, command) + executeCommand(s, m, command, i) return } } } } -func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command) { +func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command, commandIndex int) { if (!command.DMOnly || (getChannel(session.State, message.ChannelID).Type == discordgo.ChannelTypeDM)) && - (!command.AdminOnly || (message.Author.ID == config.AdminID)) { + (!command.AdminOnly || (message.Author.ID == config.AdminID)) && !command.IsOnCooldown { log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author)) fmt.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author)) + if command.Cooldown > 0 { + commands[commandIndex].IsOnCooldown = true + go removeCooldown(commandIndex) + } if command.Function == nil { // simple reply if command.OutputEmbed == nil { @@ -109,6 +117,11 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate } } +func removeCooldown(commandIndex int) { + time.Sleep(time.Duration(commands[commandIndex].Cooldown) * time.Second) + commands[commandIndex].IsOnCooldown = false +} + func generateReply(message *discordgo.MessageCreate, command Command) string { output := command.Output if command.OutputIsReply {