adds back reaction listener ....

This commit is contained in:
Seraphim Strub 2023-01-28 17:14:54 +01:00
parent 5983b00e6e
commit 2c3e52ab6d

View file

@ -49,6 +49,7 @@ func main() {
), ),
bot.WithEventListenerFunc(joinListener), bot.WithEventListenerFunc(joinListener),
bot.WithEventListenerFunc(leaveListener), bot.WithEventListenerFunc(leaveListener),
bot.WithEventListenerFunc(reactionListener),
) )
if err != nil { if err != nil {
log.Fatal("error while building disgo instance: ", err) log.Fatal("error while building disgo instance: ", err)
@ -154,3 +155,89 @@ func leaveListener(event *events.GuildMemberLeave) {
return 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
}
}