added addemoji

This commit is contained in:
2024-04-13 21:27:09 +02:00
parent 0099e89294
commit e6da5dedd6
4 changed files with 22 additions and 15 deletions

92
cmd_addemoji.go Normal file
View File

@@ -0,0 +1,92 @@
package main
import (
"bytes"
"io"
"net/http"
"regexp"
"strings"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
)
var cmd_addemoji Command = Command{
Definition: discord.SlashCommandCreate{
Name: "add-emoji",
Description: "Add an external emoji directly to the server.",
Options: []discord.ApplicationCommandOption{
&discord.ApplicationCommandOptionString{
Name: "emoji",
Description: "The emoji you want to add",
Required: true,
},
},
},
Interact: func(e *events.ApplicationCommandInteractionCreate) {
emojiRegex := regexp.MustCompile(`<(.+):(\d+)>`)
emojistring := emojiRegex.FindString(e.SlashCommandInteractionData().String("emoji"))
emojiArray := strings.Split(emojistring, ":")
var emojiName string
var emojiID string
var emojiFileName string
if len(emojiArray) > 1 {
emojiName = strings.TrimSuffix(emojiArray[1], ">")
emojiID = strings.TrimSuffix(emojiArray[2], ">")
}
imageType, emojiReadBit64 := getEmoji(emojiID)
emojiData, err := discord.NewIcon(imageType, emojiReadBit64)
if err != nil {
logrus.Error(err)
}
_, err = e.Client().Rest().CreateEmoji(*e.GuildID(), discord.EmojiCreate{
Name: emojiName,
Image: *emojiData,
})
if err != nil {
if strings.HasPrefix(err.Error(), "50035") {
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("Failed adding emoji. Did you provide a correct one?").SetEphemeral(true).Build())
return
}
if strings.HasPrefix(err.Error(), "50138") {
e.CreateMessage(discord.NewMessageCreateBuilder().SetContent("Failed adding emoji. Unable to resize the emoji image.").SetEphemeral(true).Build())
return
}
logrus.Error(err)
return
}
if imageType == discord.IconTypeGIF {
emojiFileName = emojiName + ".gif"
} else {
emojiFileName = emojiName + ".png"
}
_, emojiRead := getEmoji(emojiID) // for some reason any []bit variable thats used with NewIcon gets corrupted even when its redeclared in a new variable
err = e.CreateMessage(discord.NewMessageCreateBuilder().
SetContentf("Emoji %s sucessfully added to this server!", emojiName).SetFiles(discord.NewFile(emojiFileName, "", emojiRead)).SetEphemeral(true).
Build())
if err != nil {
logrus.Error(err)
}
},
}
func getEmoji(emojiID string) (discord.IconType, io.Reader) {
resp, err := http.Get("https://cdn.discordapp.com/emojis/" + emojiID)
if err != nil {
logrus.Error(err)
return discord.IconTypePNG, nil
}
defer resp.Body.Close()
imageData, err := io.ReadAll(resp.Body)
if err != nil {
logrus.Error(err)
return discord.IconTypePNG, nil
}
isAnimated := isGIFImage(imageData)
if isAnimated {
return discord.IconTypeGIF, bytes.NewReader(imageData)
} else {
return discord.IconTypePNG, bytes.NewReader(imageData)
}
}