Skip to content

Commit

Permalink
Merge pull request #47 from Esfands/45-utilize-stream-status-in-db
Browse files Browse the repository at this point in the history
Utilize stream status in DB
  • Loading branch information
Mahcks authored Aug 5, 2024
2 parents 0096cd2 + 3a38bea commit 7920739
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
19 changes: 8 additions & 11 deletions internal/bot/commands/game/game.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package game

import (
"errors"
"fmt"
"strings"

"github.com/esfands/retpaladinbot/internal/global"
"github.com/esfands/retpaladinbot/pkg/domain"
"github.com/esfands/retpaladinbot/pkg/utils"
"github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix/v2"
)

type GameCommand struct {
Expand Down Expand Up @@ -67,21 +67,18 @@ func (c *GameCommand) GlobalCooldown() int {
func (c *GameCommand) Code(user twitch.User, context []string) (string, error) {
target := utils.GetTarget(user, context)

res, err := c.gctx.Crate().Helix.Client().GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterIDs: []string{c.gctx.Config().Twitch.Bot.ChannelID},
})
stream, err := c.gctx.Crate().Turso.Queries().GetMostRecentStreamStatus(c.gctx)
if err != nil {
return "", err
return "", errors.New("error getting the stream status")
}

// Check if the response responded with an unauthorized error or some other error
if res.Error != "" {
return fmt.Sprintf("@%v, sorry, the Twitch API threw an error... Susge", user.Name), nil
if stream.GameName.String == "" || !stream.GameID.Valid {
return fmt.Sprintf("@%v, Esfand isn't under a specific category", target), nil
}

if strings.ToLower(res.Data.Channels[0].GameName) == "just chatting" {
return fmt.Sprintf("@%v, Esfand is under the category: %v", target, res.Data.Channels[0].Title), nil
if strings.ToLower(stream.GameName.String) == "just chatting" {
return fmt.Sprintf("@%v, Esfand is under the category: %v", target, stream.GameName.String), nil
}

return fmt.Sprintf("@%v, Esfand is playing %v", target, res.Data.Channels[0].GameName), nil
return fmt.Sprintf("@%v, Esfand is playing %v", target, stream.GameName.String), nil
}
15 changes: 6 additions & 9 deletions internal/bot/commands/title/title.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package title

import (
"errors"
"fmt"

"github.com/esfands/retpaladinbot/internal/global"
"github.com/esfands/retpaladinbot/pkg/domain"
"github.com/esfands/retpaladinbot/pkg/utils"
"github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix/v2"
)

type TitleCommand struct {
Expand Down Expand Up @@ -64,17 +64,14 @@ func (c *TitleCommand) GlobalCooldown() int {
func (c *TitleCommand) Code(user twitch.User, context []string) (string, error) {
target := utils.GetTarget(user, context)

res, err := c.gctx.Crate().Helix.Client().GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterIDs: []string{c.gctx.Config().Twitch.Bot.ChannelID},
})
stream, err := c.gctx.Crate().Turso.Queries().GetMostRecentStreamStatus(c.gctx)
if err != nil {
return "", err
return "", errors.New("error getting the stream status")
}

// Check if the response responded with an unauthorized error or some other error
if res.Error != "" {
return fmt.Sprintf("@%v, sorry, the Twitch API threw an error... Susge", user.Name), nil
if stream.Title.String == "" || !stream.Title.Valid {
return fmt.Sprintf("@%v the title is not set to anything", target), nil
}

return fmt.Sprintf("@%v current title: %v", target, res.Data.Channels[0].Title), nil
return fmt.Sprintf("@%v current title: %v", target, stream.Title.String), nil
}
37 changes: 24 additions & 13 deletions internal/bot/commands/uptime/uptime.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package uptime

import (
"errors"
"fmt"
"time"

"github.com/esfands/retpaladinbot/internal/global"
"github.com/esfands/retpaladinbot/pkg/domain"
"github.com/esfands/retpaladinbot/pkg/utils"
"github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix/v2"
"golang.org/x/exp/slog"
)

type UptimeCommand struct {
Expand Down Expand Up @@ -65,21 +66,31 @@ func (c *UptimeCommand) GlobalCooldown() int {
func (c *UptimeCommand) Code(user twitch.User, context []string) (string, error) {
target := utils.GetTarget(user, context)

res, err := c.gctx.Crate().Helix.Client().GetStreams(&helix.StreamsParams{
UserIDs: []string{c.gctx.Config().Twitch.Bot.ChannelID},
})
stream, err := c.gctx.Crate().Turso.Queries().GetMostRecentStreamStatus(c.gctx)
if err != nil {
slog.Error("[uptime-cmd] error getting most recent stream status", "error", err.Error())
return "", err
}

// Check if the response responded with an unauthorized error or some other error
if res.Error != "" {
return fmt.Sprintf("@%v, sorry, the Twitch API threw an error... Susge", user.Name), nil
// Get the uptime since there's no end time
if !stream.EndedAt.Valid {
// Parse the start time
parsedStartTime, err := time.Parse(time.RFC3339, stream.StartedAt)
if err != nil {
slog.Error("[uptime-cmd] error parsing stream start time", "error", err.Error())
return "", errors.New("error parsing the stream start time")
}

uptime := utils.TimeDifference(parsedStartTime, time.Now(), true)
return fmt.Sprintf("@%v, the stream has been live for %v", target, uptime), nil
} else {
parsedEndTime, err := time.Parse(time.RFC3339, stream.EndedAt.String)
if err != nil {
slog.Error("[uptime-cmd] error parsing stream end time", "error", err.Error())
return "", errors.New("error parsing the stream end time")
}

downtime := utils.TimeDifference(parsedEndTime, time.Now(), true)
return fmt.Sprintf("@%v, the stream has been offline for %v", target, downtime), nil
}

if len(res.Data.Streams) == 0 {
return fmt.Sprintf("@%v, the stream is offline Sadge", target), nil
}

return fmt.Sprintf("@%v, the stream has been live for %v", target, utils.TimeDifference(res.Data.Streams[0].StartedAt, time.Now(), true)), nil
}
10 changes: 4 additions & 6 deletions internal/bot/modules/go-live-right-now/go_live_right_now.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package goliverightnow

import (
"fmt"
"log/slog"
"time"

"github.com/esfands/retpaladinbot/internal/global"
"github.com/gempir/go-twitch-irc/v4"
"github.com/go-co-op/gocron"
"github.com/nicklaw5/helix/v2"
)

type GoLiveRightNowModule struct {
Expand All @@ -28,16 +28,14 @@ func NewGoLiveRightNowModule(gctx global.Context, client *twitch.Client) *GoLive

// Define the job
job := func() {
channelInfo, err := gctx.Crate().Helix.Client().GetStreams(&helix.StreamsParams{
UserIDs: []string{gctx.Config().Twitch.Bot.ChannelID},
})
streamStatus, err := gctx.Crate().Turso.Queries().GetMostRecentStreamStatus(gctx)
if err != nil {
fmt.Println("Error getting channel information in go live right now module:", err)
slog.Error("[go-live-right-now] Error getting most recent stream status", "error", err)
return
}

// Say GOLIVERIGHTNOWMADGE if the stream isn't live
if len(channelInfo.Data.Streams) == 0 {
if !streamStatus.Live {
client.Say(gctx.Config().Twitch.Bot.Channel, "GOLIVERIGHTNOWMADGE")
}
}
Expand Down
Binary file modified local.db
Binary file not shown.

0 comments on commit 7920739

Please sign in to comment.