Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/instance/direct_messages.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
"bytes"
11
"encoding/json"
12
"fmt"
13
"io/ioutil"
14
"math"
15
"math/rand"
16
"strings"
17
"time"
18
19
http "github.com/Danny-Dasilva/fhttp"
20
21
"github.com/V4NSH4J/discord-mass-dm-GO/utilities"
22
)
23
24
func cookieToString(cookies []*http.Cookie, cookieString string) string {
25
for i := 0; i < len(cookies); i++ {
26
if i == len(cookies)-1 {
27
cookieString += fmt.Sprintf(`%s=%s`, cookies[i].Name, cookies[i].Value)
28
} else {
29
cookieString += fmt.Sprintf(`%s=%s; `, cookies[i].Name, cookies[i].Value)
30
}
31
}
32
if !strings.Contains(cookieString, "locale=en-US; ") {
33
cookieString += "; locale=en-US "
34
}
35
return cookieString
36
}
37
38
func (in *Instance) GetCookieString() (string, error) {
39
if in.Config.OtherSettings.ConstantCookies && in.Cookie != "" {
40
return in.Cookie, nil
41
}
42
cookies := []*http.Cookie{}
43
link := "https://discord.com"
44
req, err := http.NewRequest("GET", link, nil)
45
if err != nil {
46
return "", fmt.Errorf("error while making request to get cookies %v", err)
47
}
48
req = in.cookieHeaders(req)
49
resp, err := in.Client.Do(req)
50
if err != nil {
51
return "", fmt.Errorf("error while getting response from cookies request %v", err)
52
}
53
defer resp.Body.Close()
54
if resp.Cookies() == nil {
55
utilities.LogErr("[%v] Error while getting cookies from response %v", time.Now().Format("15:04:05"), err)
56
return "", fmt.Errorf("there are no cookies in response")
57
}
58
if len(resp.Cookies()) == 0 {
59
return "", fmt.Errorf("there are no cookies in response")
60
}
61
cookies = append(cookies, resp.Cookies()...)
62
in.Cookie = cookieToString(cookies, "")
63
return cookieToString(cookies, ""), nil
64
}
65
66
func (in *Instance) GetCfBm(m, r, cookies string) (string, error) {
67
site := fmt.Sprintf(`https://discord.com/cdn-cgi/bm/cv/result?req_id=%s`, r)
68
payload := fmt.Sprintf(
69
`
70
{
71
"m":"%s",
72
"results":["859fe3e432b90450c6ddf8fae54c9a58","460d5f1e93f296a48e3f6745675f27e2"],
73
"timing":%v,
74
"fp":
75
{
76
"id":3,
77
"e":{"r":[1920,1080],
78
"ar":[1032,1920],
79
"pr":1,
80
"cd":24,
81
"wb":true,
82
"wp":false,
83
"wn":false,
84
"ch":false,
85
"ws":false,
86
"wd":false
87
}
88
}
89
}
90
`, m, 60+rand.Intn(60),
91
)
92
req, err := http.NewRequest("POST", site, strings.NewReader(payload))
93
if err != nil {
94
fmt.Println(err)
95
return "", fmt.Errorf("error while making request to get cf-bm %v", err)
96
}
97
req = in.cfBmHeaders(req, cookies)
98
resp, err := in.Client.Do(req)
99
if err != nil {
100
return "", fmt.Errorf("error while getting response from cf-bm request %v", err)
101
}
102
defer resp.Body.Close()
103
if resp.Cookies() == nil {
104
utilities.LogErr("[%v] Error while getting cookies from response %v", time.Now().Format("15:04:05"), err)
105
return "", fmt.Errorf("there are no cookies in response")
106
}
107
if len(resp.Cookies()) == 0 {
108
return cookies, nil
109
}
110
for _, cookie := range resp.Cookies() {
111
cookies = cookies + cookie.Name + "=" + cookie.Value
112
}
113
return cookies, nil
114
115
}
116
117
func (in *Instance) OpenChannel(recepientUID string) (string, error) {
118
url := "https://discord.com/api/v9/users/@me/channels"
119
120
json_data := []byte("{\"recipients\":[\"" + recepientUID + "\"]}")
121
122
req, err := http.NewRequest("POST", url, bytes.NewBuffer(json_data))
123
if err != nil {
124
fmt.Println("Error while making request")
125
return "", fmt.Errorf("error while making open channel request %v", err)
126
}
127
var cookie string
128
if in.Cookie == "" {
129
cookie, err = in.GetCookieString()
130
if err != nil {
131
return "", fmt.Errorf("error while getting cookie %v", err)
132
}
133
} else {
134
cookie = in.Cookie
135
}
136
137
resp, err := in.Client.Do(in.OpenChannelHeaders(req, cookie))
138
139
if err != nil {
140
return "", fmt.Errorf("error while getting response from open channel request %v", err)
141
}
142
defer resp.Body.Close()
143
144
body, err := utilities.ReadBody(*resp)
145
if err != nil {
146
return "", fmt.Errorf("error while reading body from open channel request %v", err)
147
}
148
if strings.Contains(string(body), "Your bot has been flagged") {
149
utilities.LogErr("[%v] Token %v has been quarantined", time.Now().Format("15:04:05"), in.CensorToken())
150
in.Quarantined = true
151
return "", fmt.Errorf("token has been quarantined")
152
}
153
if resp.StatusCode == 401 || resp.StatusCode == 403 {
154
utilities.LogErr("[%v] Token %v has been locked or disabled", time.Now().Format("15:04:05"), in.CensorToken())
155
return "", fmt.Errorf("token has been locked or disabled")
156
}
157
if resp.StatusCode != 200 {
158
fmt.Printf("[%v]Invalid Status Code while sending request %v \n", time.Now().Format("15:04:05"), resp.StatusCode)
159
return "", fmt.Errorf("invalid status code while sending request %v", resp.StatusCode)
160
}
161
type responseBody struct {
162
ID string `json:"id,omitempty"`
163
}
164
165
var channelSnowflake responseBody
166
errx := json.Unmarshal(body, &channelSnowflake)
167
if errx != nil {
168
return "", errx
169
}
170
171
return channelSnowflake.ID, nil
172
}
173
174
// Inputs the Channel snowflake and sends them the message; outputs the response code for error handling.
175
func (in *Instance) SendMessage(channelSnowflake string, memberid string) (int, []byte, error) {
176
// Sending a random message incase there are multiple.
177
index := rand.Intn(len(in.Messages))
178
if in.Config.DirectMessage.MultipleMessages {
179
if in.MessageNumber < len(in.Messages) {
180
index = in.MessageNumber
181
} else {
182
return 0, nil, fmt.Errorf("sent all messages")
183
}
184
}
185
message := in.Messages[index]
186
x := message.Content
187
if strings.Contains(message.Content, "<user>") {
188
ping := "<@" + memberid + ">"
189
x = strings.ReplaceAll(message.Content, "<user>", ping)
190
}
191
192
payload, err := json.Marshal(&map[string]interface{}{
193
"content": x,
194
"tts": false,
195
"nonce": utilities.Snowflake(),
196
})
197
if err != nil {
198
return -1, nil, fmt.Errorf("error while marshalling message %v %v ", index, err)
199
}
200
201
url := "https://discord.com/api/v9/channels/" + channelSnowflake + "/messages"
202
req, err := http.NewRequest("POST", url, strings.NewReader(string(payload)))
203
204
if err != nil {
205
return -1, nil, fmt.Errorf("error while making request to send message %v", err)
206
}
207
var cookie string
208
if in.Cookie == "" {
209
cookie, err = in.GetCookieString()
210
if err != nil {
211
return -1, nil, fmt.Errorf("error while getting cookie %v", err)
212
}
213
} else {
214
cookie = in.Cookie
215
}
216
217
if in.Config.SuspicionAvoidance.Typing {
218
dur := typingSpeed(x, in.Config.SuspicionAvoidance.TypingVariation, in.Config.SuspicionAvoidance.TypingSpeed, in.Config.SuspicionAvoidance.TypingBase)
219
if dur != 0 {
220
iterations := int((int64(dur) / int64(time.Second*10))) + 1
221
for i := 0; i < iterations; i++ {
222
if err := in.typing(channelSnowflake, cookie); err != nil {
223
continue
224
}
225
s := time.Second * 10
226
if i == iterations-1 {
227
s = dur % time.Second * 10
228
}
229
time.Sleep(s)
230
}
231
}
232
}
233
234
res, err := in.Client.Do(in.SendMessageHeaders(req, cookie, channelSnowflake))
235
if err != nil {
236
fmt.Printf("[%v]Error while sending http request %v \n", time.Now().Format("15:04:05"), err)
237
return -1, nil, fmt.Errorf("error while getting send message response %v", err)
238
}
239
body, err := ioutil.ReadAll(res.Body)
240
if err != nil {
241
return res.StatusCode, nil, fmt.Errorf("error while reading body %v", err)
242
}
243
t := res.StatusCode
244
if res.StatusCode == 200 || res.StatusCode == 204 {
245
if in.Config.DirectMessage.MultipleMessages {
246
go func() {
247
for {
248
in.MessageNumber++
249
if in.MessageNumber < len(in.Messages) {
250
time.Sleep(time.Second * time.Duration(in.Config.DirectMessage.DelayBetweenMultipleMessages))
251
status, body, err := in.SendMessage(channelSnowflake, memberid)
252
if err != nil {
253
utilities.LogFailed("%v Error while sending message %v \n", in.CensorToken(), err)
254
continue
255
} else {
256
utilities.LogSuccess("%v Message #%v sent successfully %v %v\n", in.CensorToken(), in.MessageNumber+1, status, body)
257
}
258
}
259
}
260
}()
261
}
262
}
263
if res.StatusCode == 400 || res.StatusCode == 403 {
264
if !strings.Contains(string(body), "captcha") {
265
return res.StatusCode, body, nil
266
}
267
if in.Config.CaptchaSettings.ClientKey == "" && in.Config.CaptchaSettings.CaptchaAPI != "invisifox.com" {
268
return res.StatusCode, body, fmt.Errorf("captcha detected but no client key set")
269
}
270
var captchaDetect captchaDetected
271
err = json.Unmarshal(body, &captchaDetect)
272
if err != nil {
273
return res.StatusCode, body, fmt.Errorf("error while unmarshalling captcha %v", err)
274
}
275
utilities.CaptchaDetected(in.CensorToken(), captchaDetect.Sitekey)
276
solved, err := in.SolveCaptcha(captchaDetect.Sitekey, cookie, captchaDetect.RqData, captchaDetect.RqToken, fmt.Sprintf("https://discord.com/channels/@me/%s", channelSnowflake))
277
if err != nil {
278
return res.StatusCode, body, fmt.Errorf("error while solving captcha %v", err)
279
}
280
payload, err = json.Marshal(&map[string]interface{}{
281
"content": x,
282
"tts": false,
283
"nonce": utilities.Snowflake(),
284
"captcha_key": solved,
285
"captcha_rqtoken": captchaDetect.RqToken,
286
})
287
if err != nil {
288
return res.StatusCode, body, fmt.Errorf("error while marshalling message %v %v ", index, err)
289
}
290
req, err = http.NewRequest("POST", url, strings.NewReader(string(payload)))
291
if err != nil {
292
return res.StatusCode, body, fmt.Errorf("error while making request to send message %v", err)
293
}
294
res, err = in.Client.Do(in.SendMessageHeaders(req, cookie, channelSnowflake))
295
if err != nil {
296
return t, body, fmt.Errorf("error while getting send message response %v", err)
297
}
298
}
299
in.Count++
300
return res.StatusCode, body, nil
301
}
302
303
func (in *Instance) UserInfo(userid string) (UserInf, error) {
304
url := "https://discord.com/api/v9/users/" + userid + "/profile?with_mutual_guilds=true"
305
306
req, err := http.NewRequest("GET", url, nil)
307
if err != nil {
308
return UserInf{}, err
309
}
310
cookie, err := in.GetCookieString()
311
if err != nil {
312
return UserInf{}, fmt.Errorf("error while getting cookie %v", err)
313
}
314
315
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
316
if err != nil {
317
return UserInf{}, err
318
}
319
320
body, err := utilities.ReadBody(*resp)
321
if err != nil {
322
return UserInf{}, err
323
}
324
325
if body == nil {
326
327
return UserInf{}, fmt.Errorf("body is nil")
328
}
329
330
var info UserInf
331
errx := json.Unmarshal(body, &info)
332
if errx != nil {
333
fmt.Println(string(body))
334
return UserInf{}, errx
335
}
336
return info, nil
337
}
338
339
func (in *Instance) Ring(snowflake string) (int, error) {
340
341
url := "https://discord.com/api/v9/channels/" + snowflake + "/call"
342
343
p := RingData{
344
Recipients: nil,
345
}
346
jsonx, err := json.Marshal(&p)
347
if err != nil {
348
return 0, err
349
}
350
351
req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonx)))
352
if err != nil {
353
return 0, err
354
}
355
356
req.Header.Set("Authorization", in.Token)
357
req.Header.Set("Content-Type", "application/json")
358
359
resp, err := in.Client.Do(req)
360
if err != nil {
361
return 0, err
362
}
363
defer resp.Body.Close()
364
365
body, err := utilities.ReadBody(*resp)
366
if err != nil {
367
return 0, err
368
}
369
fmt.Println(string(body))
370
return resp.StatusCode, nil
371
372
}
373
374
func (in *Instance) CloseDMS(snowflake string) (int, error) {
375
site := "https://discord.com/api/v9/channels/" + snowflake
376
req, err := http.NewRequest("DELETE", site, nil)
377
if err != nil {
378
return -1, err
379
}
380
cookie, err := in.GetCookieString()
381
if err != nil {
382
return -1, err
383
}
384
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
385
if err != nil {
386
return -1, err
387
}
388
return resp.StatusCode, nil
389
}
390
391
func (in *Instance) BlockUser(userid string) (int, error) {
392
site := "https://discord.com/api/v9/users/@me/relationships/" + userid
393
payload := `{"type":2}`
394
req, err := http.NewRequest("PUT", site, strings.NewReader(payload))
395
if err != nil {
396
return -1, err
397
}
398
cookie, err := in.GetCookieString()
399
if err != nil {
400
return -1, err
401
}
402
resp, err := in.Client.Do(in.AtMeHeaders(req, cookie))
403
if err != nil {
404
return -1, err
405
}
406
return resp.StatusCode, nil
407
}
408
409
func (in *Instance) greet(channelid, cookie, fingerprint string) (string, error) {
410
site := fmt.Sprintf(`https://discord.com/api/v9/channels/%s/greet`, channelid)
411
payload := `{"sticker_ids":["749054660769218631"]}`
412
req, err := http.NewRequest("POST", site, strings.NewReader(payload))
413
if err != nil {
414
return "", err
415
}
416
req = in.SendMessageHeaders(req, cookie, channelid)
417
resp, err := in.Client.Do(req)
418
if err != nil {
419
return "", err
420
}
421
if resp.StatusCode != 200 {
422
return "", fmt.Errorf(`invalid status code while sending dm %v`, resp.StatusCode)
423
}
424
body, err := ioutil.ReadAll(resp.Body)
425
if err != nil {
426
return "", err
427
}
428
var response map[string]interface{}
429
err = json.Unmarshal(body, &response)
430
if err != nil {
431
return "", err
432
}
433
var msgid string
434
if strings.Contains(string(body), "id") {
435
msgid = response["id"].(string)
436
} else {
437
return "", fmt.Errorf(`invalid response %v`, string(body))
438
}
439
return msgid, nil
440
}
441
442
func (in *Instance) ungreet(channelid, cookie, fingerprint, msgid string) error {
443
site := fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages/%s`, channelid, msgid)
444
req, err := http.NewRequest("DELETE", site, nil)
445
if err != nil {
446
return err
447
}
448
req = in.SendMessageHeaders(req, cookie, channelid)
449
resp, err := in.Client.Do(req)
450
if err != nil {
451
return err
452
}
453
if resp.StatusCode != 204 {
454
return fmt.Errorf(`invalid status code while sending dm%v`, resp.StatusCode)
455
}
456
return nil
457
}
458
459
func (in *Instance) typing(channelID, cookie string) error {
460
reqURL := fmt.Sprintf(`https://discord.com/api/v9/channels/%s/typing`, channelID)
461
req, err := http.NewRequest("POST", reqURL, nil)
462
if err != nil {
463
return err
464
}
465
req = in.TypingHeaders(req, cookie, channelID)
466
resp, err := in.Client.Do(req)
467
if err != nil {
468
return err
469
}
470
if resp.StatusCode != 204 {
471
return fmt.Errorf(`invalid status code while sending dm%v`, resp.StatusCode)
472
}
473
return nil
474
}
475
476
func typingSpeed(msg string, TypingVariation, TypingSpeed, TypingBase int) time.Duration {
477
msPerKey := int(math.Round((1.0 / float64(TypingSpeed)) * 60000))
478
d := TypingBase
479
d += len(msg) * msPerKey
480
if TypingVariation > 0 {
481
d += rand.Intn(TypingVariation)
482
}
483
return time.Duration(d) * time.Millisecond
484
}
485
486
func (in *Instance) Call(snowflake string) error {
487
if in.Ws == nil {
488
return fmt.Errorf("websocket is not initialized")
489
}
490
e := CallEvent{
491
Op: 4,
492
Data: CallData{
493
ChannelId: snowflake,
494
GuildId: nil,
495
SelfDeaf: false,
496
SelfMute: false,
497
SelfVideo: false,
498
},
499
}
500
err := in.Ws.WriteRaw(e)
501
if err != nil {
502
return fmt.Errorf("failed to write to websocket: %s", err)
503
}
504
505
return nil
506
}
507
508