-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (154 loc) · 3.13 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"github.com/gorilla/websocket"
"log"
"net/http"
"os"
"time"
)
var status = "online"
var customStatus = ""
var userToken = ""
type userData struct {
Username string
Discriminator string
Id string
}
type Auth struct {
Op int
D authDetails
S interface{}
T interface{}
}
type authDetails struct {
Token string
Properties Properties
Presence Presence
}
type Properties struct {
OS string
Browser string
Device string
}
type Presence struct {
Status string
Afk bool
}
type CStatus struct {
Op int
D cstatusDetails
}
type cstatusDetails struct {
Since int
Activities []Activity
Status string
Afk bool
}
type Activity struct {
Type int
State string
Name string
Id string
}
func main() {
userToken = os.Getenv("DISCORD_USER_TOKEN")
if len(userToken) == 0 {
log.Panic("Not found env variable DISCORD_USER_TOKEN")
}
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://discordapp.com/api/v9/users/@me", nil)
req.Header.Set("Authorization", userToken)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln("[ERROR] Your token might be invalid. Please check it again.")
}
var receivedData userData
err = json.NewDecoder(resp.Body).Decode(&receivedData)
if err != nil {
log.Fatalln(err)
}
log.Printf("Logged in as %s#%s (%s).\n", receivedData.Username, receivedData.Discriminator, receivedData.Id)
for {
runOnlineWS()
time.Sleep(50 * time.Second)
}
}
func runOnlineWS() {
log.Println("I'am online:)")
c, _, err := websocket.DefaultDialer.Dial("wss://gateway.discord.gg/?v=9&encoding=json", nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
_, msg, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
var result map[string]interface{}
json.Unmarshal(msg, &result)
heartbeat := int(result["d"].(map[string]interface{})["heartbeat_interval"].(float64))
auth := Auth{
Op: 2,
D: authDetails{
Token: userToken,
Properties: Properties{
OS: "Windows 10",
Browser: "Google Chrome",
Device: "Windows",
},
Presence: Presence{
Status: status,
Afk: false,
},
},
}
authJson, _ := json.Marshal(auth)
err = c.WriteMessage(websocket.TextMessage, authJson)
if err != nil {
log.Println("write:", err)
return
}
cstatus := CStatus{
Op: 3,
D: cstatusDetails{
Since: 0,
Activities: []Activity{
{
Type: 4,
State: customStatus,
Name: "Custom Status",
Id: "custom",
},
},
Status: status,
Afk: false,
},
}
cstatusJson, _ := json.Marshal(cstatus)
err = c.WriteMessage(websocket.TextMessage, cstatusJson)
if err != nil {
log.Println("write:", err)
return
}
online := struct {
Op int
D string
}{
Op: 1,
D: "None",
}
onlineJson, _ := json.Marshal(online)
err = c.WriteMessage(websocket.TextMessage, onlineJson)
if err != nil {
log.Println("write:", err)
return
}
time.Sleep(time.Duration(heartbeat) * time.Millisecond)
}