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