@ -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 ) || ! command s[ 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 ) {
command s[ commandIndex ] . UsersOnCooldown . Add ( message . Author . ID )
go removeCooldown ( command Index , 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 ( command Index int , uid string ) {
time . Sleep ( time . Duration ( command s[ commandIndex ] . Cooldown ) * time . Second )
if command s[ commandIndex ] . UsersOnCooldown . Contains ( uid ) {
command s[ 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 )