internal/discord.go

69 lines · 1769 bytes

 1package internal
 2
 3import (
 4	"fmt"
 5	"github.com/hugolgst/rich-go/client"
 6)
 7
 8func LoginClient(clientId string) error {
 9	err := client.Login(clientId)
10	if err != nil {
11		return err
12	}
13	return nil
14}
15
16func DiscordPresence(anime Anime, IsPaused bool) error {
17
18	var state string
19	if IsPaused {
20		state = fmt.Sprintf("\nEpisode %d - %s (Paused)", 
21			anime.Ep.Number, 
22			FormatTime(anime.Ep.Player.PlaybackTime),
23		)
24	} else {
25		state = fmt.Sprintf("\nEpisode %d - %s / %s", 
26			anime.Ep.Number, 
27			FormatTime(anime.Ep.Player.PlaybackTime), 
28			FormatTime(anime.Ep.Duration),
29		)
30	}
31
32	err := client.SetActivity(client.Activity{
33		Details:    fmt.Sprintf("%s", GetAnimeName(anime)), // Large text
34		LargeImage: anime.CoverImage,
35		LargeText:  GetAnimeName(anime), // Would display while hovering over the large image
36		State:      state,
37		//SmallImage: anime.SmallCoverImage, // Image for the bottom left corner
38		//SmallText:  fmt.Sprintf("Episode: %s", anime.Ep.Title.English), // Text when hovering over the small image
39		Buttons: []*client.Button{
40			&client.Button{
41				Label: "View on AniList", // Button label
42				Url:   fmt.Sprintf("https://anilist.co/anime/%d", anime.AnilistId), // Button link
43			},
44			&client.Button{
45				Label: "View on MAL", // Button label
46				Url:   fmt.Sprintf("https://myanimelist.net/anime/%d", anime.MalId), // Button link
47			},
48		},
49	})
50	if err != nil {
51		return err
52	}
53	return nil
54}
55
56func FormatTime(seconds int) string {
57	hours := seconds / 3600
58	minutes := (seconds % 3600) / 60
59	remainingSeconds := seconds % 60
60
61	if hours > 0 {
62		return fmt.Sprintf("%d:%02d:%02d", hours, minutes, remainingSeconds)
63	}
64	return fmt.Sprintf("%d:%02d", minutes, remainingSeconds)
65}
66
67func ConvertSecondsToMinutes(seconds int) int {
68	return seconds / 60
69}