forked from kageru/discord-selphybot
added option for command cooldowns
This commit is contained in:
parent
ab5ce7cc5f
commit
d0246a6c4e
27
command.go
27
command.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"strings"
|
"strings"
|
||||||
"log"
|
"log"
|
||||||
|
@ -22,6 +23,7 @@ type Command struct {
|
||||||
Output string // no output if unspecified
|
Output string // no output if unspecified
|
||||||
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
|
OutputEmbed *discordgo.MessageEmbed // no embed output if unspecified
|
||||||
Type CommandType // defaults to Prefix
|
Type CommandType // defaults to Prefix
|
||||||
|
Cooldown int // defaults to 0 (no cooldown)
|
||||||
OutputIsReply bool // defaults to false
|
OutputIsReply bool // defaults to false
|
||||||
RequiresMention bool // defaults to false
|
RequiresMention bool // defaults to false
|
||||||
DeleteInput bool // defaults to false
|
DeleteInput bool // defaults to false
|
||||||
|
@ -30,6 +32,8 @@ type Command struct {
|
||||||
IgnoreCase 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)
|
||||||
|
|
||||||
|
IsOnCooldown bool // don’t set this manually (it’s overwritten anyway)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,7 +54,7 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(m.Content)
|
fmt.Println(m.Content)
|
||||||
for _, command := range commands {
|
for i, command := range commands {
|
||||||
content := m.Content
|
content := m.Content
|
||||||
if command.IgnoreCase {
|
if command.IgnoreCase {
|
||||||
content = strings.ToLower(content)
|
content = strings.ToLower(content)
|
||||||
|
@ -61,34 +65,38 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
switch command.Type {
|
switch command.Type {
|
||||||
case CommandTypePrefix:
|
case CommandTypePrefix:
|
||||||
if strings.HasPrefix(content, command.Trigger) {
|
if strings.HasPrefix(content, command.Trigger) {
|
||||||
executeCommand(s, m, command)
|
executeCommand(s, m, command, i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case CommandTypeFullMatch:
|
case CommandTypeFullMatch:
|
||||||
if content == command.Trigger {
|
if content == command.Trigger {
|
||||||
executeCommand(s, m, command)
|
executeCommand(s, m, command, i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case CommandTypeRegex:
|
case CommandTypeRegex:
|
||||||
match, _ := regexp.MatchString(command.Trigger, content)
|
match, _ := regexp.MatchString(command.Trigger, content)
|
||||||
if match {
|
if match {
|
||||||
executeCommand(s, m, command)
|
executeCommand(s, m, command, i)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case CommandTypeContains:
|
case CommandTypeContains:
|
||||||
if strings.Contains(content, command.Trigger) {
|
if strings.Contains(content, command.Trigger) {
|
||||||
executeCommand(s, m, command)
|
executeCommand(s, m, command, i)
|
||||||
return
|
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)) &&
|
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))
|
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))
|
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 {
|
if command.Function == nil {
|
||||||
// simple reply
|
// simple reply
|
||||||
if command.OutputEmbed == nil {
|
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 {
|
func generateReply(message *discordgo.MessageCreate, command Command) string {
|
||||||
output := command.Output
|
output := command.Output
|
||||||
if command.OutputIsReply {
|
if command.OutputIsReply {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user