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

Emotes - add create/list/delete slash commands #22

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ messageIDs.json
Porygon
porygon
templates/*.override.json
emojis/override/*
.idea/
6 changes: 3 additions & 3 deletions config/parser.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package config

import (
"fmt"
"github.com/BurntSushi/toml"
"log"
)

func (config *Config) ParseConfig() error {
if _, err := toml.DecodeFile("default.toml", &config); err != nil {
fmt.Println("error decoding default config file,", err)
log.Println("error decoding default config file,", err)
return err
}

if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("error decoding user config file,", err)
log.Println("error decoding user config file,", err)
return err
}

Expand Down
8 changes: 4 additions & 4 deletions database/db.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package database

import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"log"

"porygon/config"
)
Expand Down Expand Up @@ -74,7 +74,7 @@ func GetRaidStats(db *sqlx.DB) ([]RaidStats, error) {
`
err := db.Select(&raidStatsList, query)
if err != nil {
fmt.Println(err)
log.Println(err)
return nil, err
}

Expand All @@ -97,7 +97,7 @@ func GetGymStats(db *sqlx.DB) (GymStats, error) {
`)

if err != nil {
fmt.Println(err)
log.Println(err)
return GymStats{}, err
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func GetRewardStats(db *sqlx.DB) ([]TypeCountStats, error) {
GROUP BY reward_type
`)
if err != nil {
fmt.Println(err)
log.Println(err)
return rewardStatsList, err
}

Expand Down
9 changes: 5 additions & 4 deletions discord/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
"log"
"os"
"porygon/config"
"porygon/database"
Expand Down Expand Up @@ -90,7 +91,7 @@ func GenerateFields(gathered GatheredStats, config config.Config) []*discordgo.M
if err != nil {
currentTemplateFile, err = os.ReadFile("templates/current.json")
if err != nil {
panic(err)
log.Panicln(err)
}
}

Expand All @@ -104,17 +105,17 @@ func GenerateFields(gathered GatheredStats, config config.Config) []*discordgo.M
"EventEmoji": func(level int) string { return convertToEmoji(level, config.EventEmoji) },
}).Parse(string(currentTemplateFile))
if err != nil {
panic(err)
log.Panicln(err)
}

var resultJSON bytes.Buffer
if err := tmpl.Execute(&resultJSON, gathered); err != nil {
panic(err)
log.Panicln(err)
}

var fields []*discordgo.MessageEmbedField
if err := json.Unmarshal(resultJSON.Bytes(), &fields); err != nil {
panic(err)
log.Panicln(err)
}

return fields
Expand Down
169 changes: 169 additions & 0 deletions discord/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package discord

import (
"encoding/base64"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"path/filepath"
"strings"
)

var (
dmPermission = false
defaultMemberPermissions int64 = discordgo.PermissionAdministrator

Commands = []*discordgo.ApplicationCommand{
{
Name: "list-emotes",
Description: "List all guild emotes",
DefaultMemberPermissions: &defaultMemberPermissions,
DMPermission: &dmPermission,
},
{
Name: "create-emotes",
Description: "Create Porygon emotes",
DefaultMemberPermissions: &defaultMemberPermissions,
DMPermission: &dmPermission,
},
{
Name: "delete-emotes",
Description: "Delete all emotes (created by Porygon)",
DefaultMemberPermissions: &defaultMemberPermissions,
DMPermission: &dmPermission,
},
}

CommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"list-emotes": listEmotes,
"create-emotes": createEmotes,
"delete-emotes": deleteEmotes,
}
)

func listEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) {
var emotesList strings.Builder

guildEmotes, _ := s.GuildEmojis(i.GuildID)

if len(guildEmotes) > 0 {
emotesList.WriteString("```")
for _, emote := range guildEmotes {
emotesList.WriteString(fmt.Sprintf("<:%s:%s>\n", emote.Name, emote.ID))
}
emotesList.WriteString("```")
} else {
emotesList.WriteString("No guild emotes.")
}

_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: emotesList.String(),
Flags: discordgo.MessageFlagsEphemeral,
},
})
}

func deleteEmotes(s *discordgo.Session, i *discordgo.InteractionCreate) {
var output strings.Builder

_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
},
})

guildEmotes, _ := s.GuildEmojis(i.GuildID)

if len(guildEmotes) > 0 {
output.WriteString("```")
for _, emote := range guildEmotes {
if emote.User.ID == s.State.User.ID {
err := s.GuildEmojiDelete(i.GuildID, emote.ID)
if err != nil {
output.WriteString(fmt.Sprintf("%s - failed to remove: %s\n", emote.Name, err))
} else {
output.WriteString(fmt.Sprintf("%s - removed\n", emote.Name))
}
} else {
output.WriteString(fmt.Sprintf("%s - skipping, other owner %s\n", emote.Name, emote.User.String()))
}
}
output.WriteString("```")
} else {
output.WriteString("No guild emotes to delete.")
}

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

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

_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
},
})

emotesDir := "emojis/override"
files, err := filepath.Glob(filepath.Join(emotesDir, "*.png"))

if len(files) > 0 {
output.WriteString("Using `emojis/override` directory as emotes source.\n")
} else {
output.WriteString("Using `emojis` directory as emotes source.\n")
emotesDir = "emojis"
files, err = filepath.Glob(filepath.Join(emotesDir, "*.png"))
}

if err != nil {
output.WriteString(fmt.Sprintf("Error reading emotes directory: %s\n", err))
} else {
output.WriteString("```")

// fetch existing emotes
guildEmotes, _ := s.GuildEmojis(i.GuildID)
existingEmotes := make(map[string]bool)
for _, emote := range guildEmotes {
existingEmotes[emote.Name] = true
}

if len(files) == 0 {
output.WriteString("no files to upload")
}

// 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("```")
}

_, _ = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: output.String(),
})
}
Empty file added emojis/override/.gitkeep
Empty file.
Loading
Loading