use command pointers to improve performance and usability

This commit is contained in:
kageru 2018-06-29 16:50:28 +02:00
parent 0717c68b53
commit 8b170ddc5a
3 changed files with 18 additions and 17 deletions

View File

@ -6,6 +6,8 @@ Updates are listed in reverse chronological order.
- finally wrote a readme
- updated wink regex
- added more misc commands
- added feedback to !complain (used to work but was forgotten when refactoring)
- use global array of pointers to commands to allow easier modification and avoid unnecessary memcpy
### 1.1
- cooldowns are now stored per-user, not globally, and no longer apply in DMs

View File

@ -56,7 +56,7 @@ func registerCommand(command Command) {
command.Trigger = strings.ToLower(command.Trigger)
}
command.UsersOnCooldown = mapset.NewSet()
commands = append(commands, command)
commands = append(commands, &command)
}
/*
@ -71,7 +71,7 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("<Self> %s", m.Content)
return
}
for i, command := range commands {
for _, command := range commands {
content := m.Content
if command.IgnoreCase {
content = strings.ToLower(content)
@ -82,23 +82,23 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
switch command.Type {
case CommandTypePrefix:
if strings.HasPrefix(content, command.Trigger) {
executeCommand(s, m, command, i)
executeCommand(s, m, command)
return
}
case CommandTypeFullMatch:
if content == command.Trigger {
executeCommand(s, m, command, i)
executeCommand(s, m, command)
return
}
case CommandTypeRegex:
match, _ := regexp.MatchString(command.Trigger, content)
if match {
executeCommand(s, m, command, i)
executeCommand(s, m, command)
return
}
case CommandTypeContains:
if strings.Contains(content, command.Trigger) {
executeCommand(s, m, command, i)
executeCommand(s, m, command)
return
}
}
@ -109,15 +109,15 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
Executes the given command on the given message and session.
Sets command cooldowns if necessary and also clears them again.
*/
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command, commandIndex int) {
func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command *Command) {
if isAdmin(message.Author) || // no restrictions for admins
(!command.AdminOnly && (isDM(session, message) || !commands[commandIndex].UsersOnCooldown.Contains(message.Author.ID)) &&
(!command.AdminOnly && (isDM(session, message) || !command.UsersOnCooldown.Contains(message.Author.ID)) &&
(!command.DMOnly || isDM(session, message))) {
log.Printf("Executed command %s triggered by user %s", command.Trigger, userToString(message.Author))
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
commands[commandIndex].UsersOnCooldown.Add(message.Author.ID)
go removeCooldown(commandIndex, message.Author.ID)
command.UsersOnCooldown.Add(message.Author.ID)
go removeCooldown(command, message.Author.ID)
}
if command.Function == nil {
// simple reply
@ -139,14 +139,14 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate
}
}
func removeCooldown(commandIndex int, uid string) {
time.Sleep(time.Duration(commands[commandIndex].Cooldown) * time.Second)
if commands[commandIndex].UsersOnCooldown.Contains(uid) {
commands[commandIndex].UsersOnCooldown.Remove(uid)
func removeCooldown(command *Command, uid string) {
time.Sleep(time.Duration(command.Cooldown) * time.Second)
if command.UsersOnCooldown.Contains(uid) {
command.UsersOnCooldown.Remove(uid)
}
}
func generateReply(message *discordgo.MessageCreate, command Command) string {
func generateReply(message *discordgo.MessageCreate, command *Command) string {
output := command.Output
if command.OutputIsReply {
output = fmt.Sprintf(output, message.Author.ID)

View File

@ -18,6 +18,7 @@ func main() {
fmt.Println("error: ", err)
return
}
defer dg.Close()
dg.AddHandler(evaluateMessage)
dg.AddHandler(onJoin)
@ -43,8 +44,6 @@ func main() {
fmt.Println("Exiting...")
log.Println("Exiting...")
dg.Close()
}
// I’ll just put all of the commands here for now.