Compare commits

..

No commits in common. "ee1bb762fbdb72874d8e42a349c8ffbb56908cfa" and "6d0c98f62ba6a4f6ea7628a969e36021d701c151" have entirely different histories.

4 changed files with 2 additions and 71 deletions

View File

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

View File

@ -67,9 +67,6 @@ 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,35 +8,6 @@ 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)
@ -46,9 +17,7 @@ func (discord *Discord) WebhookSendMessage(channel *core.Channel, displayName st
}
if webhookData == nil {
webhookAvatar := discord.GetAvatarBase64(NewUser(discord.session.State.User))
webhook, err := discord.session.WebhookCreate(channel.ID, WebhookName, webhookAvatar)
webhook, err := discord.session.WebhookCreate(channel.ID, "BirdBot", "")
if err != nil {
log.Printf("Error creating webhook: %s", err)
return

View File

@ -1,11 +1,6 @@
package discord
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/jpeg"
"log"
"github.com/bwmarrin/discordgo"
@ -28,36 +23,6 @@ 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)