Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/instance/scrape.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
"strings"
11
12
"github.com/V4NSH4J/discord-mass-dm-GO/utilities"
13
)
14
15
func Scrape(ws *Connection, Guild string, Channel string, index int) error {
16
if index == 0 {
17
err := Subscribe(ws, Guild, Channel)
18
if err != nil {
19
return err
20
}
21
}
22
var x []interface{}
23
if index == 0 {
24
x = []interface{}{[2]int{0, 99}}
25
} else if index == 1 {
26
x = []interface{}{[2]int{0, 99}, [2]int{100, 199}}
27
} else if index == 2 {
28
x = []interface{}{[2]int{0, 99}, [2]int{100, 199}, [2]int{200, 299}}
29
} else {
30
x = []interface{}{[2]int{0, 99}, [2]int{100, 199}, [2]int{index * 100, (index * 100) + 99}}
31
}
32
33
payload := Data{
34
GuildId: Guild,
35
Channels: map[string]interface{}{
36
Channel: x,
37
},
38
}
39
40
err := ws.WriteJSONe(&Event{
41
Op: 14,
42
Data: payload,
43
})
44
45
if err != nil {
46
return err
47
}
48
49
return nil
50
51
}
52
53
type CustomEvent struct {
54
Op int `json:"op,omitempty"`
55
Data Custom `json:"d,omitempty"`
56
}
57
type Custom struct {
58
GuildID interface{} `json:"guild_id"`
59
Limit int `json:"limit"`
60
Query string `json:"query"`
61
Presence bool `json:"presence"`
62
}
63
64
// Write a function which would input the connection, guildid, query, limit and presence.
65
// The function would then make an Event struct and send it to the websocket.
66
// The guild ID is to be put as a list of one item
67
68
func ScrapeOffline(c *Connection, guild string, query string) error {
69
70
custom := Custom{
71
GuildID: []string{guild},
72
Limit: 100,
73
Query: query,
74
Presence: true,
75
}
76
eventx := CustomEvent{
77
Op: 8,
78
Data: custom,
79
}
80
81
err := c.Conn.WriteJSON(eventx)
82
if err != nil {
83
return err
84
}
85
return nil
86
}
87
88
func FindNextQueries(query string, lastName string, completedQueries []string, chars string) []string {
89
if query == "" {
90
utilities.LogErr("query is empty")
91
return nil
92
}
93
lastName = strings.ToLower(lastName)
94
indexQuery := strings.Index(lastName, query)
95
if indexQuery == -1 {
96
return nil
97
}
98
wantedCharIndex := indexQuery + len(query)
99
if wantedCharIndex >= len(lastName) {
100
101
return nil
102
}
103
wantedChar := lastName[wantedCharIndex]
104
queryIndexDone := strings.Index(chars, string(wantedChar))
105
if queryIndexDone == -1 {
106
107
return nil
108
}
109
110
var nextQueries []string
111
for j := queryIndexDone; j < len(chars); j++ {
112
newQuery := query + string(chars[j])
113
if !utilities.Contains(completedQueries, newQuery) && !strings.Contains(newQuery, " ") && string(newQuery[0]) != "" {
114
nextQueries = append(nextQueries, newQuery)
115
}
116
}
117
return nextQueries
118
}
119
120
func Subscribe(ws *Connection, guildid, Channel string) error {
121
payload := Data{
122
GuildId: guildid,
123
Typing: true,
124
Threads: true,
125
Activities: true,
126
Members: []Member{},
127
Channels: map[string]interface{}{
128
Channel: []interface{}{[2]int{0, 99}},
129
},
130
}
131
132
err := ws.WriteJSONe(&Event{
133
Op: 14,
134
Data: payload,
135
})
136
if err != nil {
137
return err
138
}
139
return nil
140
}
141
142