@ -14,6 +14,7 @@ const (
CommandTypePrefix CommandType = 0
CommandTypeFullMatch CommandType = 1
CommandTypeRegex CommandType = 2
CommandTypeContains CommandType = 3
)
type Command struct {
@ -22,10 +23,10 @@ type Command struct {
OutputEmbed * discordgo . MessageEmbed // no embed output if unspecified
Type CommandType // defaults to Prefix
OutputIsReply bool // defaults to false
RequiresMention bool // defaults to false
DeleteInput bool // defaults to false
DMOnly bool // defaults to false
AdminOnly bool // defaults to false
IsEmbed bool // defaults to false
// for custom commands that go beyond prints and deletions
Function func ( * discordgo . Session , * discordgo . MessageCreate )
}
@ -44,7 +45,11 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
log . Printf ( "<Self> %s" , m . Content )
return
}
fmt . Println ( m . Content )
for _ , command := range commands {
if command . RequiresMention {
command . Trigger = fmt . Sprintf ( command . Trigger , s . State . User . ID )
}
switch command . Type {
case CommandTypePrefix :
if strings . HasPrefix ( m . Content , command . Trigger ) {
@ -62,6 +67,11 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
executeCommand ( s , m , command )
return
}
case CommandTypeContains :
if strings . Contains ( m . Content , command . Trigger ) {
executeCommand ( s , m , command )
return
}
}
}
}
@ -69,6 +79,8 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
func executeCommand ( session * discordgo . Session , message * discordgo . MessageCreate , command Command ) {
if ( ! command . DMOnly || ( getChannel ( session . State , message . ChannelID ) . Type == discordgo . ChannelTypeDM ) ) &&
( ! command . AdminOnly || ( message . Author . ID == config . AdminID ) ) {
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 . Function == nil {
// simple reply
if command . OutputEmbed == nil {
@ -80,6 +92,9 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate
if command . DeleteInput {
session . ChannelMessageDelete ( message . ChannelID , message . ID )
}
} else {
// execute custom function
command . Function ( session , message )
}
} else {
log . Printf ( "Denied command %s to user %s." , command . Trigger , userToString ( message . Author ) )
@ -94,3 +109,12 @@ func generateReply(message *discordgo.MessageCreate, command Command) string {
return output
}
func redirectComplaint ( s * discordgo . Session , m * discordgo . MessageCreate ) {
embed := & discordgo . MessageEmbed {
Author : & discordgo . MessageEmbedAuthor { } ,
Color : 0xbb0000 ,
Description : m . Content ,
}
s . ChannelMessageSendEmbed ( config . ModChannel , embed )
}