use command pointers to improve performance and usability
This commit is contained in:
parent
0717c68b53
commit
8b170ddc5a
|
@ -6,6 +6,8 @@ Updates are listed in reverse chronological order.
|
||||||
- finally wrote a readme
|
- finally wrote a readme
|
||||||
- updated wink regex
|
- updated wink regex
|
||||||
- added more misc commands
|
- 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
|
### 1.1
|
||||||
- cooldowns are now stored per-user, not globally, and no longer apply in DMs
|
- cooldowns are now stored per-user, not globally, and no longer apply in DMs
|
||||||
|
|
30
command.go
30
command.go
|
@ -56,7 +56,7 @@ func registerCommand(command Command) {
|
||||||
command.Trigger = strings.ToLower(command.Trigger)
|
command.Trigger = strings.ToLower(command.Trigger)
|
||||||
}
|
}
|
||||||
command.UsersOnCooldown = mapset.NewSet()
|
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)
|
log.Printf("<Self> %s", m.Content)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i, command := range commands {
|
for _, command := range commands {
|
||||||
content := m.Content
|
content := m.Content
|
||||||
if command.IgnoreCase {
|
if command.IgnoreCase {
|
||||||
content = strings.ToLower(content)
|
content = strings.ToLower(content)
|
||||||
|
@ -82,23 +82,23 @@ 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, i)
|
executeCommand(s, m, command)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case CommandTypeFullMatch:
|
case CommandTypeFullMatch:
|
||||||
if content == command.Trigger {
|
if content == command.Trigger {
|
||||||
executeCommand(s, m, command, i)
|
executeCommand(s, m, command)
|
||||||
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, i)
|
executeCommand(s, m, command)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case CommandTypeContains:
|
case CommandTypeContains:
|
||||||
if strings.Contains(content, command.Trigger) {
|
if strings.Contains(content, command.Trigger) {
|
||||||
executeCommand(s, m, command, i)
|
executeCommand(s, m, command)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,15 +109,15 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
Executes the given command on the given message and session.
|
Executes the given command on the given message and session.
|
||||||
Sets command cooldowns if necessary and also clears them again.
|
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
|
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))) {
|
(!command.DMOnly || isDM(session, message))) {
|
||||||
|
|
||||||
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))
|
||||||
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
|
if command.Cooldown > 0 && !isDM(session, message) && !isAdmin(message.Author) {
|
||||||
commands[commandIndex].UsersOnCooldown.Add(message.Author.ID)
|
command.UsersOnCooldown.Add(message.Author.ID)
|
||||||
go removeCooldown(commandIndex, message.Author.ID)
|
go removeCooldown(command, message.Author.ID)
|
||||||
}
|
}
|
||||||
if command.Function == nil {
|
if command.Function == nil {
|
||||||
// simple reply
|
// simple reply
|
||||||
|
@ -139,14 +139,14 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeCooldown(commandIndex int, uid string) {
|
func removeCooldown(command *Command, uid string) {
|
||||||
time.Sleep(time.Duration(commands[commandIndex].Cooldown) * time.Second)
|
time.Sleep(time.Duration(command.Cooldown) * time.Second)
|
||||||
if commands[commandIndex].UsersOnCooldown.Contains(uid) {
|
if command.UsersOnCooldown.Contains(uid) {
|
||||||
commands[commandIndex].UsersOnCooldown.Remove(uid)
|
command.UsersOnCooldown.Remove(uid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
output = fmt.Sprintf(output, message.Author.ID)
|
output = fmt.Sprintf(output, message.Author.ID)
|
||||||
|
|
3
main.go
3
main.go
|
@ -18,6 +18,7 @@ func main() {
|
||||||
fmt.Println("error: ", err)
|
fmt.Println("error: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer dg.Close()
|
||||||
|
|
||||||
dg.AddHandler(evaluateMessage)
|
dg.AddHandler(evaluateMessage)
|
||||||
dg.AddHandler(onJoin)
|
dg.AddHandler(onJoin)
|
||||||
|
@ -43,8 +44,6 @@ func main() {
|
||||||
|
|
||||||
fmt.Println("Exiting...")
|
fmt.Println("Exiting...")
|
||||||
log.Println("Exiting...")
|
log.Println("Exiting...")
|
||||||
|
|
||||||
dg.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// I’ll just put all of the commands here for now.
|
// I’ll just put all of the commands here for now.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user