internal/discord.go

55 lines · 1612 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	var state string
14	if IsPaused {
15		state = fmt.Sprintf("\nEpisode %d - %d:%02d (Paused)", 
16			anime.Ep.Number, 
17			ConvertSecondsToMinutes(anime.Ep.Player.PlaybackTime), 
18			anime.Ep.Player.PlaybackTime % 60,
19		)
20	} else {
21		state = fmt.Sprintf("\nEpisode %d - %d:%02d / %d:%02d", 
22			anime.Ep.Number, 
23			ConvertSecondsToMinutes(anime.Ep.Player.PlaybackTime), 
24			anime.Ep.Player.PlaybackTime % 60,
25			ConvertSecondsToMinutes(anime.Ep.Duration),
26			anime.Ep.Duration % 60,
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		// SmallImage: anime.CoverImage, // Image would appear in the bottom left corner
35		// SmallText:  fmt.Sprintf("%s", anime.Ep.Title.English), // Would display while hovering over the small image
36		Buttons: []*client.Button{
37			&client.Button{
38				Label: "View on AniList", // Button label
39				Url:   fmt.Sprintf("https://anilist.co/anime/%d", anime.AnilistId), // Button link
40			},
41			&client.Button{
42				Label: "View on MAL", // Button label
43				Url:   fmt.Sprintf("https://myanimelist.net/anime/%d", anime.MalId), // Button link
44			},
45		},
46	})
47	if err != nil {
48		return err
49	}
50	return nil
51}
52
53func ConvertSecondsToMinutes(seconds int) int {
54	return seconds / 60
55}