Compare commits

..

2 Commits

Author SHA1 Message Date
ee1bb762fb Add improve webhook integration 2024-03-06 21:40:23 +01:00
355b95e544 Update Dockerfile 2024-03-04 00:59:39 +01:00
4 changed files with 71 additions and 2 deletions

View File

@ -1,4 +1,4 @@
FROM golang:1.20-bullseye
FROM golang:1.21-bullseye
RUN apt update -y

View File

@ -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)

View File

@ -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

View File

@ -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)