package stats import ( "log" "slices" "time" "github.com/gempir/go-twitch-irc" ) type Channel struct { id string name string emotes map[string][]Emote db *DatabaseConnection } func NewChannel(id string, name string, db *DatabaseConnection) Channel { c := Channel{ 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) 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) { for { cc, _ := db.Query("SELECT alias_id, alias_name FROM channels WHERE opted_out_at IS NULL") for _, c := range cc { if slices.ContainsFunc(*channels, func(c2 Channel) bool { return c2.name == c["alias_name"] }) { continue } log.Printf("Joining #%s...\n", c["alias_name"]) client.Join(c["alias_name"]) *channels = append(*channels, NewChannel(c["alias_id"], c["alias_name"], db)) } for _, c := range *channels { if !slices.ContainsFunc(cc, func(x map[string]string) bool { return x["alias_name"] == c.name }) { log.Printf("Parting #%s...\n", c.name) client.Depart(c.name) *channels = slices.DeleteFunc(*channels, func(c2 Channel) bool { return c2.name == c.name }) } } time.Sleep(5 * time.Second) } }