-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
95 lines (83 loc) · 2.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// DiscordLastfmScrobbler project main.go
package main
import (
"fmt"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-ini/ini"
"github.com/shkh/lastfm-go/lastfm"
)
func Print(text string) {
currentTime := time.Now().Format("2006-01-02 15:04:05")
fmt.Println(currentTime, " - ", text)
}
func scrobbler() error {
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Println(err)
return err
}
token := cfg.Section("discord").Key("token").String()
apiKey := cfg.Section("lastfm").Key("api_key").String()
username := cfg.Section("lastfm").Key("username").String()
configInterval, err := cfg.Section("lastfm").Key("interval").Int()
if err != nil {
fmt.Println(err)
return err
}
api := lastfm.New(apiKey, "")
Print("Settings loaded: config.ini")
dg, err := discordgo.New(token)
if err != nil {
fmt.Println("Discord error: ", err)
return err
}
Print("Authorized to Discord")
if err := dg.Open(); err != nil {
fmt.Println("Discord error: ", err)
return err
}
Print("Connected to Discord")
interval := time.Duration(configInterval*1000) * time.Millisecond
ticker := time.NewTicker(interval)
var prevTrack = ""
for {
select {
case <-ticker.C:
result, err := api.User.GetRecentTracks(lastfm.P{"limit": "1", "user": username})
if err != nil {
fmt.Println("LastFM error: ", err)
} else {
if len(result.Tracks) > 0 {
currentTrack := result.Tracks[0]
isNowPlaying, _ := strconv.ParseBool(currentTrack.NowPlaying)
trackName := currentTrack.Artist.Name + " - " + currentTrack.Name
if isNowPlaying && prevTrack != trackName {
prevTrack = trackName
statusData := discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: prevTrack,
Type: discordgo.GameTypeListening,
Details: "LAST.FM",
State: "DiscordLastfmScrobbler",
},
AFK: false,
Status: "online",
}
if err := dg.UpdateStatusComplex(statusData); err != nil {
fmt.Println("Discord error: ", err)
return err
}
Print("Now playing: " + trackName)
}
}
}
}
}
}
func main() {
_ = scrobbler()
fmt.Println("Press the Enter Key to terminate the console screen!")
_, _ = fmt.Scanln()
}