Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/instance/token_util.go
310 views
1
// Copyright (C) 2021 github.com/V4NSH4J
2
//
3
// This source code has been released under the GNU Affero General Public
4
// License v3.0. A copy of this license is available at
5
// https://www.gnu.org/licenses/agpl-3.0.en.html
6
7
package instance
8
9
import (
10
"bufio"
11
"bytes"
12
"encoding/base64"
13
"encoding/json"
14
"fmt"
15
"image/jpeg"
16
"image/png"
17
"io/ioutil"
18
"math/rand"
19
"os"
20
"strings"
21
22
http "github.com/Danny-Dasilva/fhttp"
23
24
"github.com/V4NSH4J/discord-mass-dm-GO/utilities"
25
)
26
27
// @me Discord Patch request to change Username
28
func (in *Instance) NameChanger(name string) (http.Response, error) {
29
30
url := "https://discord.com/api/v9/users/@me"
31
32
data := NameChange{
33
Username: name,
34
Password: in.Password,
35
}
36
bytes, err := json.Marshal(data)
37
if err != nil {
38
return http.Response{}, err
39
}
40
41
req, err := http.NewRequest("PATCH", url, strings.NewReader(string(bytes)))
42
43
if err != nil {
44
return http.Response{}, err
45
}
46
cookie, err := in.GetCookieString()
47
if err != nil {
48
return http.Response{}, fmt.Errorf("error while getting cookie %v", err)
49
}
50
51
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
52
if err != nil {
53
return http.Response{}, err
54
}
55
56
return *resp, nil
57
58
}
59
60
// @me Discord Patch request to change Nickname
61
func (in *Instance) NickNameChanger(name string, guildid string) (http.Response, error) {
62
63
url := fmt.Sprintf("https://discord.com/api/v9/guilds/%s/members/@me", guildid)
64
65
data := NickNameChange{
66
Nickname: name,
67
}
68
bytes, err := json.Marshal(data)
69
if err != nil {
70
return http.Response{}, err
71
}
72
73
req, err := http.NewRequest("PATCH", url, strings.NewReader(string(bytes)))
74
75
if err != nil {
76
return http.Response{}, err
77
}
78
cookie, err := in.GetCookieString()
79
if err != nil {
80
return http.Response{}, fmt.Errorf("error while getting cookie %v", err)
81
}
82
83
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
84
if err != nil {
85
return http.Response{}, err
86
}
87
88
return *resp, nil
89
90
}
91
92
// @me Discord Patch request to change Avatar
93
func (in *Instance) AvatarChanger(avatar string) (http.Response, error) {
94
95
url := "https://discord.com/api/v9/users/@me"
96
97
avatar = "data:image/png;base64," + avatar
98
99
data := AvatarChange{
100
Avatar: avatar,
101
}
102
103
bytes, err := json.Marshal(data)
104
if err != nil {
105
return http.Response{}, err
106
}
107
req, err := http.NewRequest("PATCH", url, strings.NewReader(string(bytes)))
108
109
if err != nil {
110
return http.Response{}, err
111
}
112
cookie, err := in.GetCookieString()
113
if err != nil {
114
return http.Response{}, fmt.Errorf("error while getting cookie %v", err)
115
}
116
117
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
118
if err != nil {
119
return http.Response{}, err
120
}
121
122
return *resp, nil
123
124
}
125
126
// Encoding images to b64
127
func EncodeImg(pathToImage string) (string, error) {
128
129
image, err := os.Open(pathToImage)
130
131
if err != nil {
132
return "", err
133
}
134
135
defer image.Close()
136
137
reader := bufio.NewReader(image)
138
imagebytes, _ := ioutil.ReadAll(reader)
139
140
extension := http.DetectContentType(imagebytes)
141
142
switch extension {
143
default:
144
return "", fmt.Errorf("unsupported image type: %s", extension)
145
case "image/jpeg":
146
img, err := jpeg.Decode(bytes.NewReader(imagebytes))
147
if err != nil {
148
return "", err
149
}
150
buf := new(bytes.Buffer)
151
if err := png.Encode(buf, img); err != nil {
152
return "", err
153
}
154
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
155
156
case "image/png":
157
return base64.StdEncoding.EncodeToString(imagebytes), nil
158
}
159
}
160
161
// Get all file paths in a directory
162
func GetFiles(dir string) ([]string, error) {
163
files, err := ioutil.ReadDir(dir)
164
if err != nil {
165
return nil, err
166
}
167
168
var paths []string
169
for _, file := range files {
170
if file.IsDir() {
171
continue
172
}
173
paths = append(paths, dir+"/"+file.Name())
174
}
175
return paths, nil
176
}
177
178
func (in *Instance) BioChanger(bios []string) error {
179
chosenOne := bios[rand.Intn(len(bios))]
180
site := "https://discord.com/api/v9/users/@me"
181
req, err := http.NewRequest(http.MethodPatch, site, strings.NewReader(`{"bio": "`+chosenOne+`"}`))
182
if err != nil {
183
return fmt.Errorf("error while making request: %v", err)
184
}
185
req.Header.Set("Authorization", in.Token)
186
cookie, err := in.GetCookieString()
187
if err != nil {
188
return fmt.Errorf("error while getting cookie: %v", err)
189
}
190
191
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
192
if err != nil {
193
return fmt.Errorf("error while sending request: %v", err)
194
}
195
body, err := utilities.ReadBody(*resp)
196
if err != nil {
197
return fmt.Errorf("error while reading body: %v", err)
198
}
199
200
if resp.StatusCode != 200 {
201
return fmt.Errorf("error while changing bio %v %v", resp.StatusCode, string(body))
202
}
203
204
return nil
205
}
206
207
func ValidateBios(bios []string) []string {
208
var validBios []string
209
for i := 0; i < len(bios); i++ {
210
if len(bios[i]) > 190 {
211
continue
212
}
213
validBios = append(validBios, bios[i])
214
}
215
return validBios
216
}
217
218
func (in *Instance) RandomHypeSquadChanger() error {
219
site := "https://discord.com/api/v9/hypesquad/online"
220
req, err := http.NewRequest(http.MethodPost, site, strings.NewReader(fmt.Sprintf(`{"house_id": %v}`, rand.Intn(3)+1)))
221
if err != nil {
222
return fmt.Errorf("error while making request: %v", err)
223
}
224
cookie, err := in.GetCookieString()
225
if err != nil {
226
return fmt.Errorf("error while getting cookie: %v", err)
227
}
228
req = in.AtMeHeaders(req, cookie)
229
resp, err := in.Client.Do(req)
230
if err != nil {
231
return fmt.Errorf("error while sending request: %v", err)
232
}
233
if resp.StatusCode != 204 {
234
defer resp.Body.Close()
235
body, err := utilities.ReadBody(*resp)
236
if err != nil {
237
return fmt.Errorf("error while reading body: %v", err)
238
}
239
return fmt.Errorf("error while changing hype squad %v %v", resp.StatusCode, string(body))
240
}
241
return nil
242
}
243
244
func (in *Instance) ChangeToken(newPassword string) (string, error) {
245
site := "https://discord.com/api/v9/users/@me"
246
payload := fmt.Sprintf(`
247
{
248
"password": "%v",
249
"new_password": "%v"
250
}
251
`, in.Password, newPassword)
252
req, err := http.NewRequest(http.MethodPatch, site, strings.NewReader(payload))
253
if err != nil {
254
return "", fmt.Errorf("error while making request: %v", err)
255
}
256
cookie, err := in.GetCookieString()
257
if err != nil {
258
return "", fmt.Errorf("error while getting cookie: %v", err)
259
}
260
req = in.AtMeHeaders(req, cookie)
261
resp, err := in.Client.Do(req)
262
if err != nil {
263
return "", fmt.Errorf("error while sending request: %v", err)
264
}
265
defer resp.Body.Close()
266
body, err := ioutil.ReadAll(resp.Body)
267
if err != nil {
268
return "", fmt.Errorf("error while reading body: %v", err)
269
}
270
if resp.StatusCode != 200 {
271
return "", fmt.Errorf("invalid status code %v while changing token %v", resp.StatusCode, string(body))
272
}
273
if strings.Contains(string(body), "token") {
274
var response map[string]interface{}
275
err := json.Unmarshal(body, &response)
276
if err != nil {
277
return "", fmt.Errorf("error while unmarshalling response: %v", err)
278
}
279
return response["token"].(string), nil
280
} else {
281
return "", fmt.Errorf("error while changing token %v body does not contain token", string(body))
282
}
283
}
284
285