refactor: !complain

This commit is contained in:
kageru 2018-06-04 02:32:40 +02:00
parent a7d6298746
commit 9043a9dbac
Signed by untrusted user: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 37 additions and 10 deletions

View File

@ -14,6 +14,7 @@ const (
CommandTypePrefix CommandType = 0 CommandTypePrefix CommandType = 0
CommandTypeFullMatch CommandType = 1 CommandTypeFullMatch CommandType = 1
CommandTypeRegex CommandType = 2 CommandTypeRegex CommandType = 2
CommandTypeContains CommandType = 3
) )
type Command struct { type Command struct {
@ -22,10 +23,10 @@ type Command struct {
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
OutputIsReply bool // defaults to false OutputIsReply bool // defaults to false
RequiresMention bool // defaults to false
DeleteInput bool // defaults to false DeleteInput bool // defaults to false
DMOnly bool // defaults to false DMOnly bool // defaults to false
AdminOnly bool // defaults to false AdminOnly bool // defaults to false
IsEmbed 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)
} }
@ -44,7 +45,11 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("<Self> %s", m.Content) log.Printf("<Self> %s", m.Content)
return return
} }
fmt.Println(m.Content)
for _, command := range commands { for _, command := range commands {
if command.RequiresMention {
command.Trigger = fmt.Sprintf(command.Trigger, s.State.User.ID)
}
switch command.Type { switch command.Type {
case CommandTypePrefix: case CommandTypePrefix:
if strings.HasPrefix(m.Content, command.Trigger) { if strings.HasPrefix(m.Content, command.Trigger) {
@ -62,6 +67,11 @@ func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
executeCommand(s, m, command) executeCommand(s, m, command)
return 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) { func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate, command Command) {
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)) {
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 { if command.Function == nil {
// simple reply // simple reply
if command.OutputEmbed == nil { if command.OutputEmbed == nil {
@ -80,6 +92,9 @@ func executeCommand(session *discordgo.Session, message *discordgo.MessageCreate
if command.DeleteInput { if command.DeleteInput {
session.ChannelMessageDelete(message.ChannelID, message.ID) session.ChannelMessageDelete(message.ChannelID, message.ID)
} }
} else {
// execute custom function
command.Function(session, message)
} }
} else { } else {
log.Printf("Denied command %s to user %s.", command.Trigger, userToString(message.Author)) 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 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)
}

View File

@ -168,13 +168,4 @@ func adminshit (s *discordgo.Session, m *discordgo.MessageCreate) {
*/ */
} }
func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed {
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
s.ChannelMessageSendEmbed(config.ModChannel, embed)
}

12
main.go
View File

@ -34,8 +34,20 @@ func main() {
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
// Moderation
registerCommand(Command{Trigger: "^[^`]*([()|DoO];|;[()|DoOpP]|:wink:)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
// Misc commands
registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch}) registerCommand(Command{Trigger: "o/", Output: "\\o", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch}) registerCommand(Command{Trigger: "\\o", Output: "o/", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "<:selphyDango:441001954542616576>", Output: ":notes: Dango, Dango, Dango, Dango, Dango Daikazoku :notes:", Type: CommandTypeFullMatch})
registerCommand(Command{Trigger: "!welcome", OutputEmbed: getWelcomeEmbed(), Type: CommandTypeFullMatch, DMOnly: true})
registerCommand(Command{Trigger: "<@%s> <3", Output: "<@%s> <3", Type: CommandTypeFullMatch, AdminOnly: true, OutputIsReply: true, RequiresMention: true})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint})
fmt.Println("bot running. selphyWoo") fmt.Println("bot running. selphyWoo")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)