Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
fix: log to message, more bulletproof
Browse files Browse the repository at this point in the history
  • Loading branch information
lenisko committed Mar 1, 2024
1 parent f901df2 commit deeaab0
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions discord/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -57,7 +56,6 @@ func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) {

func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) {
var output strings.Builder
output.WriteString("```")

_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Expand All @@ -78,40 +76,45 @@ func createEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) {
}

if err != nil {
log.Println("Error reading emotes directory:", err)
return
}

// fetch existing emotes
guildEmotes, _ := s.GuildEmojis(i.GuildID)
existingEmotes := make(map[string]bool)
for _, emote := range guildEmotes {
existingEmotes[emote.Name] = true
}
output.WriteString(fmt.Sprintf("Error reading emotes directory: %s\n", err))
} else {
output.WriteString("```")

// check and upload every emote we have under emotesDir
for _, file := range files {
emoteName := strings.TrimSuffix(filepath.Base(file), ".png")
// fetch existing emotes
guildEmotes, _ := s.GuildEmojis(i.GuildID)
existingEmotes := make(map[string]bool)
for _, emote := range guildEmotes {
existingEmotes[emote.Name] = true
}

if _, exists := existingEmotes[emoteName]; exists {
output.WriteString(fmt.Sprintf("%s - already there\n", emoteName))
continue
if len(files) == 0 {
output.WriteString("no files to upload")
}
emoteFile, err := os.ReadFile(filepath.Join(emotesDir, filepath.Base(file)))
encodedImage := base64.StdEncoding.EncodeToString(emoteFile)
dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage)

_, err = s.GuildEmojiCreate(i.GuildID, &discordgo.EmojiParams{
Name: emoteName,
Image: dataURI,
})
if err != nil {
output.WriteString(fmt.Sprintf("%s - upload error: %s\n", emoteName, err))
continue

// check and upload every emote we have under emotesDir
for _, file := range files {
emoteName := strings.TrimSuffix(filepath.Base(file), ".png")

if _, exists := existingEmotes[emoteName]; exists {
output.WriteString(fmt.Sprintf("%s - already there\n", emoteName))
continue
}
emoteFile, err := os.ReadFile(file)
encodedImage := base64.StdEncoding.EncodeToString(emoteFile)
dataURI := fmt.Sprintf("data:image/png;base64,%s", encodedImage)

_, err = s.GuildEmojiCreate(i.GuildID, &discordgo.EmojiParams{
Name: emoteName,
Image: dataURI,
})
if err != nil {
output.WriteString(fmt.Sprintf("%s - upload error: %s\n", emoteName, err))
continue
}
output.WriteString(fmt.Sprintf("%s - success\n", emoteName))
}
output.WriteString(fmt.Sprintf("%s - success\n", emoteName))
output.WriteString("```")
}
output.WriteString("```")

_, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: output.String(),
Expand Down

0 comments on commit deeaab0

Please sign in to comment.