-
Notifications
You must be signed in to change notification settings - Fork 0
/
roblox.go
111 lines (96 loc) · 2.54 KB
/
roblox.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type UserPresence struct {
UserPresenceType int
LastOnline time.Time
PlaceID int64
RootPlaceID int64
GameID string
UniverseID int64
UserID int64
}
type UserPresenceResponse struct {
UserPresences []UserPresence
}
type User struct {
Description string `json:"description"`
Created string `json:"created"`
IsBanned bool `json:"isBanned"`
ExternalAppDisplayName string `json:"externalAppDisplayName"`
HasVerifiedBadge bool `json:"hasVerifiedBadge"`
ID int64 `json:"id"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
Presence UserPresence `json:"userPresence"`
LastPresenceChange time.Time `json:"lastPresenceChange"`
LastPresenceType int `json:"lastPresenceType"`
Metrics Metrics `json:"metrics"`
}
func getUsernameFromID(id int64) (User, error) {
resp, err := http.Get(fmt.Sprintf("https://users.roblox.com/v1/users/%d", id))
if err != nil {
return User{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return User{}, err
}
var user User
err = json.Unmarshal(body, &user)
if err != nil {
return User{}, err
}
return user, nil
}
func checkPresence(userID int64) (UserPresence, error) {
requestBody := struct {
UserIDs []int64 `json:"userIds"`
}{
UserIDs: []int64{userID},
}
reqBytes, err := json.Marshal(requestBody)
if err != nil {
fmt.Println("Error encoding request body:", err)
return UserPresence{}, err
}
resp, err := http.Post("https://presence.roblox.com/v1/presence/users", "application/json", bytes.NewBuffer(reqBytes))
if err != nil {
fmt.Println("Error making request:", err)
return UserPresence{}, err
}
defer resp.Body.Close()
var presenceResponse UserPresenceResponse
err = json.NewDecoder(resp.Body).Decode(&presenceResponse)
if err != nil {
fmt.Println("Error decoding response:", err)
return UserPresence{}, err
}
if len(presenceResponse.UserPresences) == 0 {
fmt.Println("User not found")
return UserPresence{}, nil
}
presence := presenceResponse.UserPresences[0]
return presence, nil
}
func presenceTypeToString(presenceType int) string {
switch presenceType {
case 0:
return "Offline"
case 1:
return "Online"
case 2:
return "InGame"
case 3:
return "InStudio"
default:
return "Unknown"
}
}