internal/discord.go

65 lines · 1728 bytes

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