From ee1bb762fbdb72874d8e42a349c8ffbb56908cfa Mon Sep 17 00:00:00 2001 From: Layla Manley Date: Wed, 6 Mar 2024 21:40:23 +0100 Subject: [PATCH] Add improve webhook integration --- discord/discord.go | 3 +++ discord/message.go | 33 ++++++++++++++++++++++++++++++++- discord/user.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/discord/discord.go b/discord/discord.go index 7e64924..4a9b37f 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -67,6 +67,9 @@ func (discord *Discord) Run() error { } }) + // Validate state + discord.RefreshWebhookState() + // Keep alive discord.stop = make(chan os.Signal, 1) signal.Notify(discord.stop, os.Interrupt) diff --git a/discord/message.go b/discord/message.go index 5f54f2a..8dc71aa 100644 --- a/discord/message.go +++ b/discord/message.go @@ -8,6 +8,35 @@ import ( "github.com/yeslayla/birdbot/persistence" ) +const WebhookName = "BirdBot" + +// RefreshWebhookState refreshes the state of all webhooks +func (discord *Discord) RefreshWebhookState() { + channels, err := discord.session.GuildChannels(discord.guildID) + if err != nil { + log.Printf("Error getting channels: %s", err) + return + } + + for _, channel := range channels { + webhookData, err := discord.db.GetDiscordWebhook(channel.ID) + if err != nil { + log.Printf("Error getting webhook from DB: %s", err) + return + } + + if webhookData == nil { + continue + } + + _, err = discord.session.WebhookEdit(webhookData.ID, WebhookName, discord.GetAvatarBase64(NewUser(discord.session.State.User)), channel.ID) + if err != nil { + log.Printf("Error updating webhook: %s", err) + } + } +} + +// WebhookSendMessage sends a message to a channel using a webhook func (discord *Discord) WebhookSendMessage(channel *core.Channel, displayName string, message string) { webhookData, err := discord.db.GetDiscordWebhook(channel.ID) @@ -17,7 +46,9 @@ func (discord *Discord) WebhookSendMessage(channel *core.Channel, displayName st } if webhookData == nil { - webhook, err := discord.session.WebhookCreate(channel.ID, "BirdBot", "") + webhookAvatar := discord.GetAvatarBase64(NewUser(discord.session.State.User)) + + webhook, err := discord.session.WebhookCreate(channel.ID, WebhookName, webhookAvatar) if err != nil { log.Printf("Error creating webhook: %s", err) return diff --git a/discord/user.go b/discord/user.go index 8520971..94de77f 100644 --- a/discord/user.go +++ b/discord/user.go @@ -1,6 +1,11 @@ package discord import ( + "bytes" + "encoding/base64" + "fmt" + "image" + "image/jpeg" "log" "github.com/bwmarrin/discordgo" @@ -23,6 +28,36 @@ func NewUser(user *discordgo.User) common.User { } } +// GetAvatar returns the users Avatar as a image.Image +func (discord *Discord) GetAvatar(user common.User) image.Image { + discordUser, err := discord.session.User(user.ID) + if err != nil { + log.Println("Error getting user: ", err) + return nil + } + + avatar, err := discord.session.UserAvatarDecode(discordUser) + if err != nil { + log.Println("Error decoding avatar: ", err) + return nil + } + + return avatar +} + +// GetAvatarBase64 returns the base64 encoded avatar of a user +func (discord *Discord) GetAvatarBase64(user common.User) string { + avatar := discord.GetAvatar(user) + + fmtAvatar := &bytes.Buffer{} + if err := jpeg.Encode(fmtAvatar, avatar, nil); err != nil { + log.Println("Error encoding avatar: ", err) + return "" + } + + return fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(fmtAvatar.Bytes())) +} + // AssignRole adds a role to a user func (discord *Discord) AssignRole(user common.User, role *Role) error { return discord.session.GuildMemberRoleAdd(discord.guildID, user.ID, role.ID)