Compare commits
2 Commits
6d0c98f62b
...
ee1bb762fb
Author | SHA1 | Date | |
---|---|---|---|
ee1bb762fb | |||
355b95e544 |
@ -1,4 +1,4 @@
|
||||
FROM golang:1.20-bullseye
|
||||
FROM golang:1.21-bullseye
|
||||
|
||||
RUN apt update -y
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user