-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (133 loc) · 3.81 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"errors"
"fmt"
"github.com/hugolgst/rich-go/client"
"github.com/tyler58546/go-hive-api/hive"
"log"
"os"
"os/exec"
"strings"
"time"
)
const (
DiscordToken = "1070830691757068389"
Timeout = time.Minute * 20
RefreshInterval = 30 * time.Second
)
var Months = map[time.Month]string{
1: "JAN",
2: "FEB",
3: "MAR",
4: "APR",
5: "MAY",
6: "JUN",
7: "JUL",
8: "AUG",
9: "SEP",
10: "OCT",
11: "NOV",
12: "DEC",
}
func main() {
fmt.Printf("Hive Discord Rich Presence\n\n")
for {
fmt.Print("Enter username: ")
reader := bufio.NewReader(os.Stdin)
username, _, _ := reader.ReadLine()
player, err := hive.GetPlayer(string(username))
if err != nil {
if errors.Is(err, hive.ErrorInvalidPlayer) {
fmt.Println("Invalid player. Please check your spelling and try again.")
continue
} else {
log.Fatalln(err.Error())
}
}
fmt.Printf("\nNow tracking %s.\n", player.UsernameCc)
fmt.Printf("Rich presence will start after you finish your next game.\n\n")
rpc := HiveDiscordRpc{player: player}
rpc.Start()
break
}
}
type HiveDiscordRpc struct {
player *hive.Player
currentGame *hive.Game
startTime time.Time
lastUpdate time.Time
}
func (r *HiveDiscordRpc) Start() {
r.player.Handler = r
_ = r.player.Update()
t := time.NewTicker(RefreshInterval)
for {
<-t.C
err := r.player.Update()
if err != nil {
log.Printf(err.Error())
}
if r.currentGame != nil {
if !isMinecraftRunning() {
log.Printf("Rich presence disabled because Minecraft was closed or suspended.")
r.currentGame = nil
client.Logout()
} else if time.Now().Sub(r.lastUpdate) > Timeout {
log.Printf("Rich presence disabled due to no recent activity.")
r.currentGame = nil
client.Logout()
}
}
}
}
func (r *HiveDiscordRpc) HandleStatsUpdated(currentGame *hive.Game) {
r.lastUpdate = time.Now()
allTimeStats := r.player.AllTimeStatistics(currentGame)
monthlyStats := r.player.MonthlyStatistics(currentGame)
if r.currentGame != currentGame {
r.startTime = time.Now()
r.currentGame = currentGame
log.Printf("Current game is now %s.", currentGame.Name)
}
var gameInfo []string
if allTimePos, ok := r.player.AllTimeLeaderboardPosition(currentGame); ok {
gameInfo = append(gameInfo, fmt.Sprintf("#%d All-Time [%d]", allTimePos, allTimeStats.GetInt(hive.StatisticWins)))
} else {
gameInfo = append(gameInfo, fmt.Sprintf("%d All-Time Wins", allTimeStats.GetInt(hive.StatisticWins)))
}
if monthlyPos, ok := r.player.MonthlyLeaderboardPosition(currentGame); ok {
month := Months[time.Now().UTC().Month()]
gameInfo = append(gameInfo, fmt.Sprintf("#%d %s [%d]", monthlyPos, month, monthlyStats.GetInt(hive.StatisticWins)))
} else {
if level := allTimeStats.GetInt(hive.StatisticLevel); level != 0 {
gameInfo = append(gameInfo, fmt.Sprintf("| Level %d", level))
} else {
gameInfo = append(gameInfo, fmt.Sprintf("| %d XP", allTimeStats.GetInt(hive.StatisticExperience)))
}
}
err := client.Login(DiscordToken)
if err != nil {
log.Printf("Failed to start Discord rich presence. Is Discord running?")
return
}
err = client.SetActivity(client.Activity{
Details: currentGame.Name,
State: strings.Join(gameInfo, " "),
LargeImage: currentGame.Id,
Timestamps: &client.Timestamps{
Start: &r.startTime,
},
})
if err != nil {
log.Printf(err.Error())
}
}
func isMinecraftRunning() bool {
out, _ := runPowershellCommand("$process = Get-Process -Name Minecraft.Windows -ErrorAction SilentlyContinue;$suspendedThreads = $process.Threads | Where-Object {$_.WaitReason -eq \"Suspended\"};$suspendedThreads.Count -eq $process.Threads.Count")
return out == "False\r\n"
}
func runPowershellCommand(cmd string) (string, error) {
outBytes, err := exec.Command("powershell", "-Command", cmd).Output()
return string(outBytes), err
}