Generalized redirect function, some logging (fixed #3, #4)

Now supports embeds as an optional parameter. Already 80% less copy
paste code for the redirects. Can’t get it of it 100% because reasons.
Slight adjustments to logging so I can finally see embeds the bot is
sending.
This commit is contained in:
kageru 2019-01-27 17:09:29 +01:00
parent 9dd7775168
commit 3b672e2f30
3 changed files with 33 additions and 24 deletions

@ -67,7 +67,12 @@ func registerCommand(command Command) {
*/ */
func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) { func evaluateMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
log.Printf("<Self> %s", m.Content) // Properly log embeds
if len(m.Embeds) > 0 {
log.Printf("<Self> %s", m.Embeds[0].Description)
} else {
log.Printf("<Self> %s", m.Content)
}
return return
} }
for _, command := range commands { for _, command := range commands {
@ -161,34 +166,38 @@ func generateReply(message *discordgo.MessageCreate, command *Command) string {
} }
/* /*
Any message passed to this method will be redirected to config.ModChannel. * Im beginning to doubt my own self-imposed limitations of
This is useful for anonymous complaints or similar messages. * only allowing func(session, message) to be attached to commands,
*/ * but refactoring that might be more effort than its worth.
// TODO: refactor this and the next into a more general redirect function * Hence, small wrappers around the redirect function.
func redirectComplaint(s *discordgo.Session, m *discordgo.MessageCreate) { */
embed := &discordgo.MessageEmbed{ func modComplain(s *discordgo.Session, m *discordgo.MessageCreate) {
Author: &discordgo.MessageEmbedAuthor{}, redirectMessage(s, m, config.ModChannel, true)
Color: 0xbb0000,
Description: m.Content,
}
s.ChannelMessageSendEmbed(config.ModChannel, embed)
dm, _ := s.UserChannelCreate(m.Author.ID) dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage) s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} }
// copy paste programming btw :haHAA: func selphyComplain(s *discordgo.Session, m *discordgo.MessageCreate) {
func redirectComplaintToDM(s *discordgo.Session, m *discordgo.MessageCreate) {
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
dm_target, _ := s.UserChannelCreate("190958368301645824") dm_target, _ := s.UserChannelCreate("190958368301645824")
s.ChannelMessageSendEmbed(dm_target.ID, embed) redirectMessage(s, m, dm_target.ID, true)
dm, _ := s.UserChannelCreate(m.Author.ID) dm, _ := s.UserChannelCreate(m.Author.ID)
s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage) s.ChannelMessageSend(dm.ID, config.ComplaintReceivedMessage)
} }
func redirectMessage(s *discordgo.Session, m *discordgo.MessageCreate, target string, isEmbed bool) {
if isEmbed {
embed := &discordgo.MessageEmbed{
// Embed are anonymized by default for now. Fits the use case.
Author: &discordgo.MessageEmbedAuthor{},
Color: 0xbb0000,
Description: m.Content,
}
s.ChannelMessageSendEmbed(target, embed)
return
}
s.ChannelMessageSend(target, messageToString(m.Message))
}
func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) { func echoMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, m.Content) s.ChannelMessageSend(m.ChannelID, m.Content)
} }

@ -23,7 +23,7 @@ func onJoin(s *discordgo.Session, member *discordgo.GuildMemberAdd) {
} }
if err != nil { if err != nil {
// if any of the preceding operations produced an error // if any of the preceding operations produced an error
log.Printf("Sending welcome @mention at %s", userToString(member.User)) log.Printf("Sending welcome @mention @%s", userToString(member.User))
s.ChannelMessageSend(config.GeneralChannel, fmt.Sprintf("Wilkommen <@%s>. Bitte aktiviere vorübergehend DMs für diesen Server und sende eine Nachricht mit !welcome an mich.", member.User.ID)) s.ChannelMessageSend(config.GeneralChannel, fmt.Sprintf("Wilkommen <@%s>. Bitte aktiviere vorübergehend DMs für diesen Server und sende eine Nachricht mit !welcome an mich.", member.User.ID))
} }
} }

@ -53,9 +53,9 @@ func addCommands() {
registerCommand(Command{Trigger: "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex}) registerCommand(Command{Trigger: "^[^`]*([()|DoOvVcC][-=^']?;|;[-=^']?[()|DoOpPvVcC3]|:wink:|😉)[^`]*$", Output: "<@%s> Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
registerCommand(Command{Trigger: "(\\s|\n|^)[nN][hH]([ ?.,\n]|$)", Output: "<@%s> „nh“ ist kein Wort, du Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex}) registerCommand(Command{Trigger: "(\\s|\n|^)[nN][hH]([ ?.,\n]|$)", Output: "<@%s> „nh“ ist kein Wort, du Oboe!", DeleteInput: true, OutputIsReply: true, Type: CommandTypeRegex})
registerCommand(Command{Trigger: "einzigste", Output: "<@%s> Es heißt „einzige“, du Tuba.", DeleteInput: true, OutputIsReply: true, Type: CommandTypeContains}) registerCommand(Command{Trigger: "einzigste", Output: "<@%s> Es heißt „einzige“, du Tuba.", DeleteInput: true, OutputIsReply: true, Type: CommandTypeContains})
registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint}) registerCommand(Command{Trigger: "!complain", Type: CommandTypePrefix, DMOnly: true, Function: modComplain})
registerCommand(Command{Trigger: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaintToDM}) registerCommand(Command{Trigger: "!scomplain", Type: CommandTypePrefix, DMOnly: true, Function: selphyComplain})
registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: redirectComplaint}) registerCommand(Command{Trigger: "!beschwerde", Type: CommandTypePrefix, DMOnly: true, Function: modComplain})
for comm, _ := range config.RoleCommands { for comm, _ := range config.RoleCommands {
registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole}) registerCommand(Command{Trigger: comm, Type: CommandTypeFullMatch, DMOnly: true, Function: giveAgeRole})