Skip to content

Commit

Permalink
Merge pull request #5 from EsefexBot/dev-api
Browse files Browse the repository at this point in the history
Support for regular Emoji
  • Loading branch information
jokil123 authored Dec 26, 2023
2 parents d9e25db + 5be628e commit 67c705e
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 20 deletions.
4 changes: 3 additions & 1 deletion EsefexApi/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/sounds
/testsounds
/data
/database
/database

.env
2 changes: 1 addition & 1 deletion EsefexApi/bot/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func fmtMetaList(metas []sounddb.SoundMeta) string {
// log.Printf("fmtMetaList: %v", metas)
var str string
for _, meta := range metas {
str += fmt.Sprintf("- %s `%s`\n", meta.Name, meta.SoundID)
str += fmt.Sprintf("- %s %s `%s`\n", meta.Icon.String(), meta.Name, meta.SoundID)
}

// log.Println(str)
Expand Down
17 changes: 12 additions & 5 deletions EsefexApi/bot/commands/upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"esefexapi/sounddb"
"esefexapi/util"
"fmt"
"log"
Expand Down Expand Up @@ -38,8 +39,11 @@ var (
func (c *CommandHandlers) Upload(s *discordgo.Session, i *discordgo.InteractionCreate) (*discordgo.InteractionResponse, error) {
options := OptionsMap(i)

icon := options["icon"]
iconURL := util.ExtractIconUrl(icon)
iconOption := options["icon"]
icon, err := sounddb.ExtractIcon(fmt.Sprint(iconOption.Value))
if err != nil {
return nil, err
}

soundFile := options["sound-file"]
soundFileUrl := i.ApplicationCommandData().Resolved.Attachments[fmt.Sprint(soundFile.Value)].URL
Expand All @@ -49,13 +53,16 @@ func (c *CommandHandlers) Upload(s *discordgo.Session, i *discordgo.InteractionC
return nil, err
}

c.db.AddSound(i.GuildID, fmt.Sprint(options["name"].Value), iconURL, pcm)
uid, err := c.db.AddSound(i.GuildID, fmt.Sprint(options["name"].Value), icon, pcm)
if err != nil {
return nil, err
}

log.Printf("Uploaded sound effect %v to server %v", options["name"].Value, i.GuildID)
log.Printf("Uploaded sound effect %v to server %v", uid.SoundID, i.GuildID)
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Uploaded sound effect",
Content: fmt.Sprintf("Uploaded sound effect %s %s", uid.SoundID, icon.Name),
},
}, nil
}
17 changes: 17 additions & 0 deletions EsefexApi/cmd/emoji_testing/emoji_testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"esefexapi/sounddb"
"fmt"
)

func main() {
text := "<:emoji:630819109726191617>πŸ€„πŸ†˜πŸ§ŒπŸ€‘πŸ†˜"

icon, err := sounddb.ExtractIcon(text)
if err != nil {
panic(err)
}

fmt.Println(icon)
}
2 changes: 2 additions & 0 deletions EsefexApi/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
services:
api:
image: jokil/esefexapi:latest
env_file:
- .env.github
ports:
- "8080:8080"
volumes:
Expand Down
2 changes: 2 additions & 0 deletions EsefexApi/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ require github.com/pelletier/go-toml v1.9.5
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/samber/lo v1.39.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
Expand Down
4 changes: 4 additions & 0 deletions EsefexApi/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
Expand Down
26 changes: 21 additions & 5 deletions EsefexApi/sounddb/apimockdb/apimockdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,43 @@ var mockData = map[string]map[string]sounddb.SoundMeta{
SoundID: "sound1",
ServerID: "server1",
Name: "sound1Name",
Icon: "icon1",
Icon: sounddb.Icon{
Name: "icon1",
ID: "icon1ID",
Url: "https://icon1Url.webp",
},
},
"sound2": {
SoundID: "sound2",
ServerID: "server1",
Name: "sound2Name",
Icon: "icon2",
Icon: sounddb.Icon{
Name: "icon2",
ID: "icon2ID",
Url: "https://icon2Url.webp",
},
},
},
"server2": {
"sound3": {
SoundID: "sound3",
ServerID: "server2",
Name: "sound3Name",
Icon: "icon3",
Icon: sounddb.Icon{
Name: "icon3",
ID: "icon3ID",
Url: "https://icon3Url.webp",
},
},
"sound4": {
SoundID: "sound4",
ServerID: "server2",
Name: "sound4Name",
Icon: "icon4",
Icon: sounddb.Icon{
Name: "icon4",
ID: "icon4ID",
Url: "https://icon4Url.webp",
},
},
},
}
Expand All @@ -43,7 +59,7 @@ func NewApiMockDB() *ApiMockDB {
}

// AddSound implements sounddb.ISoundDB.
func (*ApiMockDB) AddSound(serverID string, name string, icon string, pcm []int16) (sounddb.SoundUID, error) {
func (*ApiMockDB) AddSound(serverID string, name string, icon sounddb.Icon, pcm []int16) (sounddb.SoundUID, error) {
panic("unimplemented")
}

Expand Down
4 changes: 2 additions & 2 deletions EsefexApi/sounddb/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sounddb

type ISoundDB interface {
AddSound(serverID string, name string, icon string, pcm []int16) (SoundUID, error)
AddSound(serverID string, name string, icon Icon, pcm []int16) (SoundUID, error)
DeleteSound(uid SoundUID) error
GetSoundMeta(uid SoundUID) (SoundMeta, error)
GetSoundPcm(uid SoundUID) ([]int16, error)
Expand All @@ -19,5 +19,5 @@ type SoundMeta struct {
SoundID string `json:"id"`
ServerID string `json:"serverId"`
Name string `json:"name"`
Icon string `json:"icon"`
Icon Icon `json:"icon"`
}
2 changes: 1 addition & 1 deletion EsefexApi/sounddb/dbcache/dbcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewDBCache(db sounddb.ISoundDB) *DBCache {
}

// AddSound implements db.SoundDB.
func (c *DBCache) AddSound(serverID string, name string, icon string, pcm []int16) (sounddb.SoundUID, error) {
func (c *DBCache) AddSound(serverID string, name string, icon sounddb.Icon, pcm []int16) (sounddb.SoundUID, error) {
c.rw.Lock()
defer c.rw.Unlock()

Expand Down
4 changes: 2 additions & 2 deletions EsefexApi/sounddb/filedb/addsound.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// AddSound implements sounddb.SoundDB.
func (f *FileDB) AddSound(serverID string, name string, iconUrl string, pcm []int16) (sounddb.SoundUID, error) {
func (f *FileDB) AddSound(serverID string, name string, icon sounddb.Icon, pcm []int16) (sounddb.SoundUID, error) {
sid, err := f.generateSoundID(serverID)
if err != nil {
return sounddb.SoundUID{}, err
Expand All @@ -20,7 +20,7 @@ func (f *FileDB) AddSound(serverID string, name string, iconUrl string, pcm []in
SoundID: sid,
ServerID: serverID,
Name: name,
Icon: iconUrl,
Icon: icon,
}

// Make sure the db folder exists
Expand Down
11 changes: 8 additions & 3 deletions EsefexApi/sounddb/filedb/filedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
func TestFileDB(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)

iconUrl := "https://github.com/Cinnazeyy/Esefex/raw/main/EsefexApi/test/staticfiles/icon.webp"
icon := sounddb.Icon{
Name: "icon1",
ID: "icon1",
Url: "https://raw.githubusercontent.com/EsefexBot/Esefex/main/EsefexApi/test/staticfiles/icon.webp",
}

serverID := "server1"
soundName := "sound1"
soundPcm := []int16{115, 117, 115}
Expand All @@ -24,7 +29,7 @@ func TestFileDB(t *testing.T) {
assert.Nil(t, err)

// Test that we can add a sound
uid, err := db.AddSound(serverID, soundName, iconUrl, soundPcm)
uid, err := db.AddSound(serverID, soundName, icon, soundPcm)
assert.Nil(t, err)

_, err = os.Stat(fmt.Sprintf("%s/%s/%s_meta.json", location, serverID, uid.SoundID))
Expand All @@ -44,7 +49,7 @@ func TestFileDB(t *testing.T) {
SoundID: uid.SoundID,
ServerID: serverID,
Name: soundName,
Icon: iconUrl,
Icon: icon,
})

// Test that we can get the sound pcm
Expand Down
61 changes: 61 additions & 0 deletions EsefexApi/sounddb/icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sounddb

import (
"esefexapi/util"
"fmt"
"regexp"
)

type Icon struct {
RegularEmoji bool `json:"regularEmoji"`
Name string `json:"name"`
ID string `json:"id"`
Url string `json:"url"`
}

func NewCustomIcon(name string, id string) Icon {
return Icon{
RegularEmoji: false,
Name: name,
ID: id,
Url: fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.webp?quality=lossless", id),
}
}

func NewEmojiIcon(emoji string) Icon {
return Icon{
RegularEmoji: true,
Name: emoji,
Url: util.GetEmojiURL(emoji),
}
}

func (i *Icon) String() string {
if i.RegularEmoji {
return i.Name
}
return fmt.Sprintf("<:%s:%s>", i.Name, i.ID)
}

var iconRegex string = `<:([^:]+):(\d+)>`
var r *regexp.Regexp = regexp.MustCompile(fmt.Sprintf(`%s|(%s)`, iconRegex, util.EmojiRegex))

func ExtractIcon(str string) (Icon, error) {
m := r.FindStringSubmatch(str)
if m == nil {
return Icon{}, fmt.Errorf("invalid icon, no match")
}
if len(m) != 4 {
return Icon{}, fmt.Errorf("invalid icon, len(m) != 4")
}

if m[1] != "" && m[2] != "" {
return NewCustomIcon(m[1], m[2]), nil
}

if m[3] != "" {
return NewEmojiIcon(m[3]), nil
}

return Icon{}, fmt.Errorf("invalid icon")
}
66 changes: 66 additions & 0 deletions EsefexApi/sounddb/sounddb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sounddb

import (
"github.com/stretchr/testify/assert"
"testing"
)

func ExtractIconTest(t *testing.T) {
text := "<:emoji:630819109726191617>πŸ€„πŸ†˜πŸ§ŒπŸ€‘πŸ†˜"
icon, err := ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: false,
Name: "emoji",
ID: "630819109726191617",
Url: "https://cdn.discordapp.com/emojis/630819109726191617.webp?quality=lossless",
}, icon)

text = "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘¦πŸ€„πŸ†˜πŸ§ŒπŸ€‘πŸ†˜"
icon, err = ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: true,
Name: "πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘¦",
Url: "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f468-200d-1f468-200d-1f467-200d-1f466.svg",
}, icon)

text = "<:emoji:630819109726191617>"
icon, err = ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: false,
Name: "emoji",
ID: "630819109726191617",
Url: "https://cdn.discordapp.com/emojis/630819109726191617.webp?quality=lossless",
}, icon)

text = "asdasc<:emoji:630819109726191617>asdasc"
icon, err = ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: false,
Name: "emoji",
ID: "630819109726191617",
Url: "https://cdn.discordapp.com/emojis/630819109726191617.webp?quality=lossless",
}, icon)

text = "asdasc<:emoji1:111>asdasc<:emoji2:222>asdasc"
icon, err = ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: false,
Name: "emoji1",
ID: "111",
Url: "https://cdn.discordapp.com/emojis/111.webp?quality=lossless",
}, icon)

text = "asdas<><<s;;::c🀑"
icon, err = ExtractIcon(text)
assert.Nil(t, err)
assert.Equal(t, Icon{
RegularEmoji: true,
Name: "🀑",
Url: "https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f921.svg",
}, icon)
}
3 changes: 3 additions & 0 deletions EsefexApi/util/emojiregex.go

Large diffs are not rendered by default.

Loading

0 comments on commit 67c705e

Please sign in to comment.