summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-10-14 22:34:59 +0500
committerilotterytea <iltsu@alright.party>2025-10-14 22:34:59 +0500
commit8b9188b2c0ac192150201c511944d68862275f69 (patch)
tree67dae6db7a2f54aa2afd45e92be802b35e2b236a
parente4c92920e627b7ad17f51637f52ca36e8468341f (diff)
feat: get bttv emotes
-rw-r--r--database.sql10
-rw-r--r--internal/channel.go36
-rw-r--r--internal/emotes.go47
3 files changed, 85 insertions, 8 deletions
diff --git a/database.sql b/database.sql
index e453239..023723a 100644
--- a/database.sql
+++ b/database.sql
@@ -29,4 +29,12 @@ CREATE TABLE IF NOT EXISTS channel_words (
last_used_at TIMESTAMP NOT NULL DEFAULT UTC_TIMESTAMP,
UNIQUE (channel_id, user_id, word_id)
-); \ No newline at end of file
+);
+
+CREATE TABLE IF NOT EXISTS emotes (
+ id BIGINT PRIMARY KEY AUTO_INCREMENT,
+ platform VARCHAR(16) NOT NULL,
+ platform_id VARCHAR(64) NOT NULL,
+ UNIQUE (platform, platform_id)
+);
+
diff --git a/internal/channel.go b/internal/channel.go
index c6100c5..da051ac 100644
--- a/internal/channel.go
+++ b/internal/channel.go
@@ -9,20 +9,42 @@ import (
)
type Channel struct {
- id string
- name string
+ id string
+ name string
+ emotes map[string][]Emote
+ db *DatabaseConnection
}
-func NewChannel(id string, name string) Channel {
+func NewChannel(id string, name string, db *DatabaseConnection) Channel {
c := Channel{
- id: id,
- name: name,
+ id: id,
+ name: name,
+ emotes: make(map[string][]Emote),
+ db: db,
}
+ go func() {
+ if err := c.updateEmotes(); err != nil {
+ log.Fatalf("Failed to update emotes: %v\n", err)
+ }
+ }()
+
return c
}
-func (c *Channel) HandleMessageEvent(sender twitch.User, message twitch.Message, db *DatabaseConnection) {
+func (c *Channel) updateEmotes() (err error) {
+ log.Printf("Fetching BetterTTV emotes for ID %s...\n", c.id)
+ emotes, err := GetBetterTTVEmotes(c.id)
+ c.emotes["betterttv"] = emotes
+
+ for _, e := range emotes {
+ c.db.Exec("INSERT IGNORE INTO emotes(platform, platform_id) VALUES (?, ?)", e.Id, "betterttv")
+ }
+
+ return
+}
+
+func (c *Channel) HandleMessageEvent(sender twitch.User, message twitch.Message) {
}
func JoinChannels(channels *[]Channel, client *twitch.Client, db *DatabaseConnection) {
@@ -38,7 +60,7 @@ func JoinChannels(channels *[]Channel, client *twitch.Client, db *DatabaseConnec
log.Printf("Joining #%s...\n", c["alias_name"])
client.Join(c["alias_name"])
- *channels = append(*channels, NewChannel(c["alias_id"], c["alias_name"]))
+ *channels = append(*channels, NewChannel(c["alias_id"], c["alias_name"], db))
}
for _, c := range *channels {
diff --git a/internal/emotes.go b/internal/emotes.go
new file mode 100644
index 0000000..d85eca2
--- /dev/null
+++ b/internal/emotes.go
@@ -0,0 +1,47 @@
+package stats
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+)
+
+type Emote struct {
+ Id string
+ Name string
+}
+
+func GetBetterTTVEmotes(channelId string) (emotes []Emote, err error) {
+ resp, err := http.Get(fmt.Sprintf("https://api.betterttv.net/3/cached/users/twitch/%s", channelId))
+ if err != nil {
+ return
+ }
+ defer resp.Body.Close()
+
+ var data map[string]any
+ if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
+ return
+ }
+
+ if msg, ok := data["message"]; ok {
+ err = fmt.Errorf("error while searching user: %s", msg)
+ return
+ }
+
+ ejson := []map[string]any{}
+ for _, v := range data["channelEmotes"].([]any) {
+ ejson = append(ejson, v.(map[string]any))
+ }
+ for _, v := range data["sharedEmotes"].([]any) {
+ ejson = append(ejson, v.(map[string]any))
+ }
+
+ for _, e := range ejson {
+ emotes = append(emotes, Emote{
+ Id: e["id"].(string),
+ Name: e["code"].(string),
+ })
+ }
+
+ return
+}