finished plugin system base
This commit is contained in:
115
plugin_src/addemoji/main.go
Normal file
115
plugin_src/addemoji/main.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
"github.com/disgoorg/disgo/events"
|
||||
"github.com/disgoorg/json"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/vaporvee/acecore/shared"
|
||||
)
|
||||
|
||||
var Plugin = &shared.Plugin{
|
||||
Name: "Add Emoji",
|
||||
Commands: []shared.Command{
|
||||
{
|
||||
Definition: discord.SlashCommandCreate{
|
||||
Name: "add-emoji",
|
||||
Description: "Add an external emoji directly to the server.",
|
||||
DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuildExpressions),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func isGIFImage(imageData []byte) bool {
|
||||
if len(imageData) < 6 {
|
||||
return false
|
||||
}
|
||||
// Check for the GIF89a header at the beginning of the byte array
|
||||
if string(imageData[0:6]) == "GIF89a" {
|
||||
return true
|
||||
}
|
||||
// Check for the GIF87a header at the beginning of the byte array
|
||||
if string(imageData[0:6]) == "GIF87a" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user