From 2c3e52ab6d0c9005c6df742602d75590d6b412b5 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Sat, 28 Jan 2023 17:14:54 +0100 Subject: [PATCH] adds back reaction listener .... --- cmd/welcomebot/main.go | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/cmd/welcomebot/main.go b/cmd/welcomebot/main.go index 6dfc6a2..d6f4374 100644 --- a/cmd/welcomebot/main.go +++ b/cmd/welcomebot/main.go @@ -49,6 +49,7 @@ func main() { ), bot.WithEventListenerFunc(joinListener), bot.WithEventListenerFunc(leaveListener), + bot.WithEventListenerFunc(reactionListener), ) if err != nil { log.Fatal("error while building disgo instance: ", err) @@ -154,3 +155,89 @@ func leaveListener(event *events.GuildMemberLeave) { return } } + +func reactionListener(event *events.ComponentInteractionCreate) { + + if event.ChannelID() != channelWelcomeID { + // reaction is not in welcome chat + return + } + + approverID := event.User().ID + if approverID.String() == event.Client().ID().String() { + // reaction user is bot itself + return + } + if event.ButtonInteractionData().CustomID() != "approve" { + // not approve button + return + } + approver, err := event.Client().Rest().GetMember(registerGuildID, approverID) + if err != nil { + event.Client().Logger().Error("could not find user: ", err) + return + } + hasPermission := func() bool { + for _, roleID := range approver.RoleIDs { + if roleID.String() == roleAppleCoreID.String() { + return true + } + } + return false + }() + + if hasPermission == false { + err = event.CreateMessage(discord.NewMessageCreateBuilder(). + SetEphemeral(true). + SetContent("you don't have permissions to do this"). + Build()) + if err != nil { + event.Client().Logger().Error("could not respond to reaction: ", err) + return + } + return + } + + messageID := event.Message.ID + message, err := event.Client().Rest().GetMessage(channelWelcomeID, messageID) + if err != nil { + event.Client().Logger().Error("could not find message: ", err) + return + } + if len(message.Mentions) != 1 { + // there should be only one mention in the message + return + } + user := message.Mentions[0] + + // add role to user (bot role needs to be above roleAppleID) + err = event.Client().Rest().AddMemberRole(registerGuildID, user.ID, roleAppleID) + if err != nil { + event.Client().Logger().Error("not able to add role: ", err) + return + } + + // update embed (and time) + if len(message.Embeds) != 1 { + // there should be only one embed in the message + return + } + embed := message.Embeds[0] + updatedMessage := discord.NewEmbedBuilder(). + SetColor(16068661). + SetTimestamp(time.Now()). + SetThumbnail(embed.Thumbnail.URL). + AddFields(embed.Fields...). + AddField("Approval", fmt.Sprintf("%v approved by %v (`%v`)", user.Mention(), approver.Mention(), approver.User.Username), false). + Build() + + err = event.UpdateMessage(discord.NewMessageUpdateBuilder(). + SetContent(message.Content). + SetEmbeds(updatedMessage). + ClearContainerComponents(). + Build()) + if err != nil { + event.Client().Logger().Error("could not update message: ", err) + return + } +}