forked from imkirko/discord-gs2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.txt
253 lines (198 loc) · 5.95 KB
/
discord.txt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
vars
.channels {channel collection}
.debug {boolean} - Displays debug messages
.token {string} - set token with login() function only
.guilds {guild collection}
.ready {boolean} - client loaded all info
.webhooks {webhook collection}
.users {user collection} - All users
funcs
.login(token)
.newRichEmbed()
events
*onReady()
*onSynced()
onError(error)
Notes:
* Not all data will be ready when called
*/
const CACHE_TIME = 3600 * 2; // two hours
/*
Predefine collections
*/
function onCreated() {
this.ready = false;
this.guilds = new TCollection().init("guilds", this);
this.channels = new TCollection().init("channels", this);
this.webhooks = new TCollection().init("webhooks", this);
this.users = new TCollection().init("users", this);
}
/*
Caches app details and joined guilds.
@param {string} token - Your applications token, optional to include token type.
*/
public function login(temp.token) {
RC.debug("Client logging in (not really)");
if (token.starts("Bearer"))
return RC.error("Discord class doesn't support bearer token types, only bot");
if (!token.starts("Bot"))
token = "Bot" SPC temp.token;
this.token = temp.token;
this.starting = true;
this._syncAttempts = 0;
temp.success = onSyncGuilds();
if (temp.success) {
this.starting = false;
this.ready = true;
this.trigger("onReady");
this.scheduleEvent(CACHE_TIME, "onSyncGuilds", null);
}
}
/*
Will begin to cache client and guild data
@return {boolean} - Was successfull
*/
public function onSyncGuilds() {
// Sync main data
if (!this.cacheClientUser() || !this.cacheGuilds() || !this.cacheChannels()) {
this._syncAttempts ++;
temp.seconds = 15 * this._syncAttempts;
this.triggerError(format("Failed to sync, will try again in %s", temp.seconds));
this.scheduleEvent(temp.seconds, "onSyncGuilds", null);
return false;
}
// Continue to sync other data w/o interruption
this.scheduleEvent(this, "onSyncContinue", null);
this.trigger("onSynced");
RC.debug("Client resynced");
// First time syncing
if (!this.ready && this.starting) {
this.starting = false;
this.ready = true;
this.trigger("onReady");
}
return true;
}
function onSyncContinue() {
this.cacheWebhooks();
this.cacheUsers();
}
/*
Sync application details
@return {boolean} - Successfull or not
*/
function cacheClientUser() {
temp.req = this.makeRequest("get", this.endpoints.me, null, null, null, false);
if (this.checkForError(temp.req) == true) return;
temp.vars = temp.req.getdynamicvarnames();
for (temp.var : temp.vars)
this.user.(@ temp.var) = temp.req.(@ temp.var);
return true;
}
/*
Sync guilds client is in
@return {boolean} - Successfull or not
*/
function cacheGuilds() {
temp.req = this.makeRequest("get", this.endpoints.me.guilds, null, null, null, false);
if (this.checkForError(temp.req) == true) return;
for (temp.guild : temp.req) {
temp.guild.join("discord_guild");
temp.guild.init(this);
this.guilds.add(temp.guild);
}
return true;
}
/*
Sync all channel types
@return {boolean} - Successfull or not
*/
function cacheChannels() {
// Guild channels
for (temp.guild : this.guilds) {
temp.guild.channels = new TCollection().init("channels", temp.guild);
temp.url = format(this.endpoints.guild.channels, temp.guild.id, "", false);
temp.req = this.makeRequest("get", temp.url, null, null, null, false);
if (checkForError(temp.req) == true)
return;
for (temp.channel : temp.req) {
temp.channel.join("discord_channel");
temp.channel.init(this);
this.channels.add(temp.channel);
temp.guild.channels.add(temp.channel);
}
}
// Private channels
temp.req = this.makeRequest("get", this.endpoints.me.channels, null, null, null, false);
if (checkForError(temp.req) == true)
return;
for (temp.channel : temp.req) {
temp.channel.join("discord_channel");
temp.channel.init(this);
this.channels.add(temp.channel);
}
return true;
}
/*
Sync webhooks from all guilds
@return {boolean} - Successfull or not
*/
function cacheWebhooks() {
for (temp.guild : this.guilds) {
temp.guild.webhooks = new TCollection().init("webhooks", temp.guild);
temp.url = format(this.endpoints.guild.webhooks, temp.guild.id);
temp.req = this.makeRequest("get", temp.url, null, null, null, false);
if (this.checkForError(temp.req) == true)
return false;
for (temp.webhook : temp.req) {
temp.webhook.join("discord_webhook");
temp.webhook.init(this);
this.webhooks.add(temp.webhook);
temp.guild.webhooks.add(temp.webhook);
//TODO: Add to appropriate channel
}
}
return true;
}
/*
Sync users and members
@return {boolean} - Successfull or not
*/
function cacheUsers() {
for (temp.guild : this.guilds) {
temp.guild.members = new TCollection().init("members", temp.guild);
temp.req = this.makeRequest("get", format(this.endpoints.guild.members, temp.guild.id), {
{"limit", 1000}
}, null, null, false);
if (this.checkForError(temp.req) == true)
return false;
for (temp.member : temp.req) {
temp.member.join("discord_member");
temp.member.user.join("discord_user");
temp.member.user.init(this);
temp.member.init(this);
this.users.add(temp.member.user);
temp.guild.members.add(temp.member);
}
}
return true;
}
/*
Emebed object that can be parsed into json string
@return TStaticVar
*/
public function newRichEmbed() {
// yes all this is needed
temp.obj = new TStaticVar();
temp.obj.base = new TStaticVar();
temp.obj.base.embed = new TStaticVar();
temp.obj.join("discord_embed");
return temp.obj;
}
/*
Triggers error event with message
@param {string} error - Error message to display
*/
public function triggerError(temp.error)
this.trigger("onError", temp.error);