Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving basic twitch stream endpoints to twitchgo library #12

Draft
wants to merge 2 commits into
base: twitch
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var log = *logger.New(logger.Writer(), "[Events] ", logger.LstdFlags|logger.Lmsgprefix)

// PostRegister registers all events, like commands after the bots are started.
func PostRegister(dc *discordgo.Session, t *twitchgo.Twitch, guildID string) error {
func PostRegister(dc *discordgo.Session, t *twitchgo.Session, guildID string) error {
err := command.Register(dc, guildID)
if err != nil {
return err
Expand All @@ -40,7 +40,7 @@ func PostRegister(dc *discordgo.Session, t *twitchgo.Twitch, guildID string) err
}

// AddListeners adds all event handlers to the given bots.
func AddListeners(dc *discordgo.Session, t *twitchgo.Twitch, webChan chan struct{}) {
func AddListeners(dc *discordgo.Session, t *twitchgo.Session, webChan chan struct{}) {
dc.AddHandler(handleInteractionCreate)
addVoiceStateListeners(dc)

Expand All @@ -50,6 +50,6 @@ func AddListeners(dc *discordgo.Session, t *twitchgo.Twitch, webChan chan struct
t.OnChannelMessage(twitch.MessageHandler)

addYouTubeListeners(dc)
addTwitchListeners(dc)
addTwitchListeners(dc, t)
addScheduledTriggers(dc, t, webChan)
}
6 changes: 3 additions & 3 deletions event/scheduledTriggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/spf13/viper"
)

func addScheduledTriggers(dc *discordgo.Session, t *twitchgo.Twitch, webChan chan struct{}) {
func addScheduledTriggers(dc *discordgo.Session, t *twitchgo.Session, webChan chan struct{}) {
go scheduleFunction(dc, t, 0, 0,
adventcalendar.Midnight,
)
Expand All @@ -40,7 +40,7 @@ func addScheduledTriggers(dc *discordgo.Session, t *twitchgo.Twitch, webChan cha
go refreshYoutube(webChan)
}

func scheduleFunction(dc *discordgo.Session, t *twitchgo.Twitch, hour, min int, callbacks ...interface{}) {
func scheduleFunction(dc *discordgo.Session, t *twitchgo.Session, hour, min int, callbacks ...interface{}) {
if len(callbacks) == 0 {
return
}
Expand All @@ -59,7 +59,7 @@ func scheduleFunction(dc *discordgo.Session, t *twitchgo.Twitch, hour, min int,
switch f := c.(type) {
case func(*discordgo.Session):
f(dc)
case func(*twitchgo.Twitch):
case func(*twitchgo.Session):
f(t)
}
}
Expand Down
34 changes: 17 additions & 17 deletions event/twitch/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package twitch
import (
"cake4everybot/data/lang"
"cake4everybot/database"
"cake4everybot/twitch"
"cake4everybot/util"
webTwitch "cake4everybot/webserver/twitch"
"database/sql"
Expand All @@ -13,26 +12,27 @@ import (
"strings"

"github.com/bwmarrin/discordgo"
"github.com/kesuaheli/twitchgo"
)

// HandleChannelUpdate is the event handler for the "channel.update" event from twitch.
func HandleChannelUpdate(s *discordgo.Session, e *webTwitch.ChannelUpdateEvent) {
HandleStreamAnnouncementChange(s, e.BroadcasterUserID, "")
func HandleChannelUpdate(s *discordgo.Session, t *twitchgo.Session, e *webTwitch.ChannelUpdateEvent) {
HandleStreamAnnouncementChange(s, t, e.BroadcasterUserID, "")
}

// HandleStreamOnline is the event handler for the "stream.online" event from twitch.
func HandleStreamOnline(s *discordgo.Session, e *webTwitch.StreamOnlineEvent) {
HandleStreamAnnouncementChange(s, e.BroadcasterUserID, lang.GetDefault("module.twitch.msg.nofification"))
func HandleStreamOnline(s *discordgo.Session, t *twitchgo.Session, e *webTwitch.StreamOnlineEvent) {
HandleStreamAnnouncementChange(s, t, e.BroadcasterUserID, lang.GetDefault("module.twitch.msg.nofification"))
}

// HandleStreamOffline is the event handler for the "stream.offline" event from twitch.
func HandleStreamOffline(s *discordgo.Session, e *webTwitch.StreamOfflineEvent) {
HandleStreamAnnouncementChange(s, e.BroadcasterUserID, "")
func HandleStreamOffline(s *discordgo.Session, t *twitchgo.Session, e *webTwitch.StreamOfflineEvent) {
HandleStreamAnnouncementChange(s, t, e.BroadcasterUserID, "")
}

// HandleStreamAnnouncementChange is a general event handler for twitch events, that should update
// the discord announcement embed.
func HandleStreamAnnouncementChange(s *discordgo.Session, platformID string, notification string) {
func HandleStreamAnnouncementChange(s *discordgo.Session, t *twitchgo.Session, platformID string, notification string) {
announcements, err := database.GetAnnouncement(database.AnnouncementPlatformTwitch, platformID)
if err == sql.ErrNoRows {
return
Expand All @@ -42,7 +42,7 @@ func HandleStreamAnnouncementChange(s *discordgo.Session, platformID string, not
}

for _, announcement := range announcements {
err = updateAnnouncementMessage(s, announcement, notification)
err = updateAnnouncementMessage(s, t, announcement, notification)
if err != nil {
log.Printf("Error: %v", err)
}
Expand Down Expand Up @@ -72,16 +72,16 @@ func newAnnouncementMessage(s *discordgo.Session, announcement *database.Announc
return msg, announcement.UpdateAnnouncementMessage(msg.ID)
}

func updateAnnouncementMessage(s *discordgo.Session, announcement *database.Announcement, notification string) error {
func updateAnnouncementMessage(s *discordgo.Session, t *twitchgo.Session, announcement *database.Announcement, notification string) error {
msg, err := getAnnouncementMessage(s, announcement)
if err != nil {
return fmt.Errorf("get announcement in channel '%s': %v", announcement, err)
}

var (
embed *discordgo.MessageEmbed
user *twitch.User
stream *twitch.Stream
user *twitchgo.User
stream *twitchgo.Stream
)

if msg == nil || len(msg.Embeds) == 0 {
Expand All @@ -90,15 +90,15 @@ func updateAnnouncementMessage(s *discordgo.Session, announcement *database.Anno
} else {
embed = msg.Embeds[0]
}
users, err := twitch.GetUsersByID(announcement.PlatformID)
users, err := t.GetUsersByID(announcement.PlatformID)
if err != nil {
return err
}
if len(users) == 0 {
return fmt.Errorf("get users: found no user with ID '%s'", announcement.PlatformID)
}
user = users[0]
streams, err := twitch.GetStreamsByID(announcement.PlatformID)
streams, err := t.GetStreamsByID(announcement.PlatformID)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func updateAnnouncementMessage(s *discordgo.Session, announcement *database.Anno
return nil
}

func setDefaultEmbed(embed *discordgo.MessageEmbed, user *twitch.User) {
func setDefaultEmbed(embed *discordgo.MessageEmbed, user *twitchgo.User) {
embed.Author = &discordgo.MessageEmbedAuthor{
URL: fmt.Sprintf("https://twitch.tv/%s/about", user.Login),
Name: user.DisplayName,
Expand All @@ -152,7 +152,7 @@ func setDefaultEmbed(embed *discordgo.MessageEmbed, user *twitch.User) {
embed.Image.Height = 1080
}

func setOnlineEmbed(embed *discordgo.MessageEmbed, user *twitch.User, stream *twitch.Stream) {
func setOnlineEmbed(embed *discordgo.MessageEmbed, user *twitchgo.User, stream *twitchgo.Stream) {
setDefaultEmbed(embed, user)

embed.Title = stream.Title
Expand All @@ -167,7 +167,7 @@ func setOnlineEmbed(embed *discordgo.MessageEmbed, user *twitch.User, stream *tw
embed.Image.URL = strings.ReplaceAll(stream.ThumbnailURL, "{width}x{height}", "1920x1080")
}

func setOfflineEmbed(embed *discordgo.MessageEmbed, user *twitch.User) {
func setOfflineEmbed(embed *discordgo.MessageEmbed, user *twitchgo.User) {
setDefaultEmbed(embed, user)

embed.Title = ""
Expand Down
8 changes: 4 additions & 4 deletions event/twitch/messageHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ var se *streamelements.Streamelements

// MessageHandler handles new messages from the twitch chat(s). It will be called on every new
// message.
func MessageHandler(t *twitchgo.Twitch, channel string, user *twitchgo.User, message string) {
func MessageHandler(t *twitchgo.Session, channel string, user *twitchgo.IRCUser, message string) {
log.Printf("<%s@%s> %s", user.Nickname, channel, message)
}

// HandleCmdJoin is the handler for a command in a twitch chat. This handler buys a giveaway ticket
// and removes the configured cost amount for a ticket.
func HandleCmdJoin(t *twitchgo.Twitch, channel string, user *twitchgo.User, args []string) {
func HandleCmdJoin(t *twitchgo.Session, channel string, user *twitchgo.IRCUser, args []string) {
channel, _ = strings.CutPrefix(channel, "#")
const tp = tp + "join."

Expand Down Expand Up @@ -155,7 +155,7 @@ func HandleCmdJoin(t *twitchgo.Twitch, channel string, user *twitchgo.User, args

// HandleCmdTickets is the handler for the tickets command in a twitch chat. This handler simply
// prints the users amount of tickets
func HandleCmdTickets(t *twitchgo.Twitch, channel string, source *twitchgo.User, args []string) {
func HandleCmdTickets(t *twitchgo.Session, channel string, source *twitchgo.IRCUser, args []string) {
channel, _ = strings.CutPrefix(channel, "#")
const tp = tp + "tickets."

Expand Down Expand Up @@ -254,7 +254,7 @@ skipPoints:

// HandleCmdDraw is the handler for the draw command in a twitch chat. This handler selects a random
// winner and removes their tickets.
func HandleCmdDraw(t *twitchgo.Twitch, channel string, user *twitchgo.User, args []string) {
func HandleCmdDraw(t *twitchgo.Session, channel string, user *twitchgo.IRCUser, args []string) {
channel, _ = strings.CutPrefix(channel, "#")
const tp = tp + "draw."

Expand Down
4 changes: 2 additions & 2 deletions event/twitch/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (

// Register is setting up the twitch bot. Like joining channels and other stuff that is available
// after the bot is connected
func Register(bot *twitchgo.Twitch) {
func Register(t *twitchgo.Session) {
channels := viper.GetStringSlice("twitch.channels")
for _, channel := range channels {
bot.SendCommandf("JOIN #%s", channel)
t.SendCommandf("JOIN #%s", channel)
}
log.Printf("Channel list set to %v\n", channels)

Expand Down
4 changes: 3 additions & 1 deletion event/twitchUpdates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
webTwitch "cake4everybot/webserver/twitch"

"github.com/bwmarrin/discordgo"
"github.com/kesuaheli/twitchgo"
"github.com/spf13/viper"
)

func addTwitchListeners(s *discordgo.Session) {
func addTwitchListeners(s *discordgo.Session, t *twitchgo.Session) {
webTwitch.SetDiscordSession(s)
webTwitch.SetTwitchSession(t)
webTwitch.SetDiscordChannelUpdateHandler(twitch.HandleChannelUpdate)
webTwitch.SetDiscordStreamOnlineHandler(twitch.HandleStreamOnline)
webTwitch.SetDiscordStreamOfflineHandler(twitch.HandleStreamOffline)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bwmarrin/discordgo v0.27.1
github.com/go-sql-driver/mysql v1.7.1
github.com/gorilla/mux v1.8.0
github.com/kesuaheli/twitchgo v0.2.7
github.com/kesuaheli/twitchgo v0.2.8-0.20240413191324-200d5973ea61
github.com/spf13/viper v1.15.0
)

Expand All @@ -28,3 +28,4 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kesuaheli/twitchgo v0.2.7 h1:/Dv+RFNThaMADzHiRb6UrSBAPubq04YT0X9MbWJgiAo=
github.com/kesuaheli/twitchgo v0.2.7/go.mod h1:swIW1jJcFa4bWi/9JfUYH4Sf1kAvj+QUcaTPkce+6Ds=
github.com/kesuaheli/twitchgo v0.2.8-0.20240413191324-200d5973ea61 h1:8A1ZAyjfVQf6ZwJEXNe+sg/3ZJNqRnOGtGeNMS1cnws=
github.com/kesuaheli/twitchgo v0.2.8-0.20240413191324-200d5973ea61/go.mod h1:swIW1jJcFa4bWi/9JfUYH4Sf1kAvj+QUcaTPkce+6Ds=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"cake4everybot/config"
"cake4everybot/database"
"cake4everybot/event"
"cake4everybot/twitch"
"cake4everybot/webserver"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -74,8 +73,7 @@ func main() {
log.Printf("Logged in to Discord as %s#%s\n", s.State.User.Username, s.State.User.Discriminator)
})

twitch.Connect()
twitchBot := twitchgo.New(viper.GetString("twitch.name"), viper.GetString("twitch.token"))
twitchBot := twitchgo.New(viper.GetString("twitch.clientID"), viper.GetString("twitch.clientSecret"), viper.GetString("twitch.token"))

// adding listeners for events
event.AddListeners(discordBot, twitchBot, webChan)
Expand Down
Loading