internal/discord.go

157 lines · 4081 bytes · executable

  1package internal
  2
  3import (
  4	"fmt"
  5	"github.com/tr1xem/go-discordrpc/client"
  6	"time"
  7)
  8
  9var discordClient *client.Client
 10var isLoggedIn bool
 11var lastPausedState bool
 12var lastEpisodeNumber int
 13var lastAnimeTitle string
 14var lastUpdateTime time.Time
 15var lastForceUpdateTime time.Time
 16
 17func LoginClient(clientId string) error {
 18	if discordClient != nil && isLoggedIn {
 19		return nil // Already logged in
 20	}
 21
 22	discordClient = client.NewClient(clientId)
 23
 24	if err := discordClient.Login(); err != nil {
 25		return fmt.Errorf("login failed: %w", err)
 26	}
 27
 28	isLoggedIn = true
 29	return nil
 30}
 31
 32func DiscordPresence(anime Anime, IsPaused bool, currentPosition int, totalDuration int, clientId string) error {
 33	return DiscordPresenceWithForce(anime, IsPaused, currentPosition, totalDuration, clientId, false)
 34}
 35
 36func DiscordPresenceWithForce(anime Anime, IsPaused bool, currentPosition int, totalDuration int, clientId string, forceUpdate bool) error {
 37	// Ensure client is logged in
 38	if discordClient == nil || !isLoggedIn {
 39		if err := LoginClient(clientId); err != nil {
 40			return err
 41		}
 42	}
 43
 44	currentAnimeTitle := GetAnimeName(anime)
 45	now := time.Now()
 46
 47	shouldUpdate := false
 48
 49	if lastForceUpdateTime.IsZero() || time.Since(lastForceUpdateTime) >= 2*time.Minute {
 50		shouldUpdate = true
 51		lastForceUpdateTime = now
 52	}
 53
 54	if lastUpdateTime.IsZero() ||
 55		lastPausedState != IsPaused ||
 56		lastEpisodeNumber != anime.Ep.Number ||
 57		lastAnimeTitle != currentAnimeTitle ||
 58		forceUpdate {
 59		shouldUpdate = true
 60	}
 61
 62	if !shouldUpdate {
 63		return nil // Skip update
 64	}
 65
 66	var timestamps *client.Timestamps
 67	var SmallImage = "pause-button"
 68	var SmallText = "pause-button"
 69
 70	startTime := now.Add(-time.Duration(currentPosition) * time.Second)
 71
 72	if IsPaused {
 73		timestamps = &client.Timestamps{
 74			Start: &startTime,
 75			End:   nil, // No end time when paused
 76		}
 77		SmallImage = "pause-button"
 78		SmallText = "Paused"
 79	} else {
 80		if totalDuration > 60 && totalDuration > currentPosition {
 81			remainingSeconds := totalDuration - currentPosition
 82			endTime := now.Add(time.Duration(remainingSeconds) * time.Second)
 83			timestamps = &client.Timestamps{
 84				Start: &startTime,
 85				End:   &endTime,
 86			}
 87		} else {
 88			// Duration unknown, show elapsed time only
 89			timestamps = &client.Timestamps{
 90				Start: &startTime,
 91				End:   nil,
 92			}
 93		}
 94		SmallImage = ""
 95		SmallText = ""
 96	}
 97
 98	err := discordClient.SetActivity(client.Activity{
 99		Type:       3, // Watching
100		Name:       currentAnimeTitle,
101		Details:    currentAnimeTitle, // Large text
102		LargeImage: anime.CoverImage,
103		LargeText:  currentAnimeTitle, // Would display while hovering over the large image
104		State:      fmt.Sprintf("Episode %d", anime.Ep.Number),
105		SmallImage: SmallImage,
106		SmallText:  SmallText,
107		Timestamps: timestamps,
108		Buttons: []*client.Button{
109			{
110				Label: "View on AniList",                                           // Button label
111				Url:   fmt.Sprintf("https://anilist.co/anime/%d", anime.AnilistId), // Button link
112			},
113			{
114				Label: "View on MAL",                                                // Button label
115				Url:   fmt.Sprintf("https://myanimelist.net/anime/%d", anime.MalId), // Button link
116			},
117		},
118	})
119
120	if err != nil {
121		return fmt.Errorf("failed to set Discord activity: %w", err)
122	}
123
124	lastPausedState = IsPaused
125	lastEpisodeNumber = anime.Ep.Number
126	lastAnimeTitle = currentAnimeTitle
127	lastUpdateTime = now
128	// fmt.Println("Discord presence updated!", time.Now())
129	return nil
130}
131
132func LogoutClient() error {
133	if discordClient != nil && isLoggedIn {
134		if err := discordClient.Logout(); err != nil {
135			return fmt.Errorf("logout failed: %w", err)
136		}
137		isLoggedIn = false
138		discordClient = nil
139		// fmt.Println("Discord RPC logged out!")
140	}
141	return nil
142}
143
144func FormatTime(seconds int) string {
145	hours := seconds / 3600
146	minutes := (seconds % 3600) / 60
147	remainingSeconds := seconds % 60
148
149	if hours > 0 {
150		return fmt.Sprintf("%d:%02d:%02d", hours, minutes, remainingSeconds)
151	}
152	return fmt.Sprintf("%d:%02d", minutes, remainingSeconds)
153}
154
155func ConvertSecondsToMinutes(seconds int) int {
156	return seconds / 60
157}