From 9043a9dbac406efdcd3b9722a862b1852d1f87e3 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 4 Jun 2018 02:32:40 +0200 Subject: [PATCH] refactor: !complain --- command.go | 26 +++++++++++++++++++++++++- events.go | 9 --------- main.go | 12 ++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/command.go b/command.go index 47d03fd..0f7e9f3 100644 --- a/command.go +++ b/command.go @@ -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(" %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) +} + diff --git a/events.go b/events.go index 801c029..03960bf 100644 --- a/events.go +++ b/events.go @@ -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) -} - diff --git a/main.go b/main.go index bbeec11..9befad9 100644 --- a/main.go +++ b/main.go @@ -34,8 +34,20 @@ func main() { defer f.Close() 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: "<: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") sc := make(chan os.Signal, 1)