-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from EsefexBot/dev-api
Support for regular Emoji
- Loading branch information
Showing
16 changed files
with
217 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
/sounds | ||
/testsounds | ||
/data | ||
/database | ||
/database | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.