From 0537ce948d4f7c13e3748f1d318d25e137e60f7e Mon Sep 17 00:00:00 2001 From: karitham Date: Fri, 9 Apr 2021 20:21:18 +0200 Subject: [PATCH] feat: have the bot self react with and X and delete its own message on reaction add --- bot/discordPages.go | 8 ++++++++ bot/handlers.go | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bot/discordPages.go b/bot/discordPages.go index 71ef4ef..1d61052 100644 --- a/bot/discordPages.go +++ b/bot/discordPages.go @@ -2,6 +2,7 @@ package bot import ( "fmt" + "log" "time" "github.com/bwmarrin/discordgo" @@ -69,6 +70,13 @@ func formatForMessage(page *ReactionListener) string { // ReactionListen listens for the reactions for a previously sent embed. func ReactionListen(session *discordgo.Session, reaction *discordgo.MessageReactionAdd) { + // Shortcut to delete self embed + if reaction.UserID != session.State.Ready.User.ID && reaction.Emoji.Name == destroyEmoji { + if err := session.ChannelMessageDelete(reaction.ChannelID, reaction.MessageID); err != nil { + log.Printf("could not delete message: %s", err) + } + } + // if the message being reacted to is in the reaction map if _, ok := pageListeners[reaction.MessageID]; ok { // validating that the user reacting is indeed the user that owns the listener diff --git a/bot/handlers.go b/bot/handlers.go index b02e1f6..4f37c55 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -2,6 +2,7 @@ package bot import ( "fmt" + "log" "strings" "time" @@ -42,7 +43,15 @@ func HandleDoc(s *discordgo.Session, m *discordgo.MessageCreate, prefix string) if msg == nil { msg = errResponse("No results found, possibly an internal error.") } - s.ChannelMessageSendEmbed(m.ChannelID, msg) + new_m, err := s.ChannelMessageSendEmbed(m.ChannelID, msg) + if err != nil { + log.Printf("could not send message: %s", err) + } + + err = s.MessageReactionAdd(new_m.ChannelID, new_m.ID, destroyEmoji) + if err != nil { + log.Printf("could not send message: %s", err) + } } // queryResponse generates the response for a query.