internal/discord.go
162 lines · 4207 bytes · executable
13 symbols in this file
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 largeImage := anime.CoverImage
99 if largeImage == "" {
100 largeImage = "https://anilist.co/img/icons/icon.svg" // fallback image
101 }
102
103 err := discordClient.SetActivity(client.Activity{
104 Type: 3, // Watching
105 Name: currentAnimeTitle,
106 Details: currentAnimeTitle, // Large text
107 LargeImage: largeImage,
108 LargeText: currentAnimeTitle, // Would display while hovering over the large image
109 State: fmt.Sprintf("Episode %d", anime.Ep.Number),
110 SmallImage: SmallImage,
111 SmallText: SmallText,
112 Timestamps: timestamps,
113 Buttons: []*client.Button{
114 {
115 Label: "View on AniList", // Button label
116 Url: fmt.Sprintf("https://anilist.co/anime/%d", anime.AnilistId), // Button link
117 },
118 {
119 Label: "View on MAL", // Button label
120 Url: fmt.Sprintf("https://myanimelist.net/anime/%d", anime.MalId), // Button link
121 },
122 },
123 })
124
125 if err != nil {
126 return fmt.Errorf("failed to set Discord activity: %w", err)
127 }
128
129 lastPausedState = IsPaused
130 lastEpisodeNumber = anime.Ep.Number
131 lastAnimeTitle = currentAnimeTitle
132 lastUpdateTime = now
133 // fmt.Println("Discord presence updated!", time.Now())
134 return nil
135}
136
137func LogoutClient() error {
138 if discordClient != nil && isLoggedIn {
139 if err := discordClient.Logout(); err != nil {
140 return fmt.Errorf("logout failed: %w", err)
141 }
142 isLoggedIn = false
143 discordClient = nil
144 // fmt.Println("Discord RPC logged out!")
145 }
146 return nil
147}
148
149func FormatTime(seconds int) string {
150 hours := seconds / 3600
151 minutes := (seconds % 3600) / 60
152 remainingSeconds := seconds % 60
153
154 if hours > 0 {
155 return fmt.Sprintf("%d:%02d:%02d", hours, minutes, remainingSeconds)
156 }
157 return fmt.Sprintf("%d:%02d", minutes, remainingSeconds)
158}
159
160func ConvertSecondsToMinutes(seconds int) int {
161 return seconds / 60
162}