This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
/
gateway.cr
374 lines (326 loc) · 8.21 KB
/
gateway.cr
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
require "./converters"
require "./user"
require "./channel"
require "./guild"
module Discord
module Gateway
struct ReadyPayload
JSON.mapping(
v: UInt8,
user: User,
private_channels: Array(PrivateChannel),
guilds: Array(UnavailableGuild),
session_id: String
)
end
struct ResumedPayload
JSON.mapping(
_trace: Array(String)
)
end
struct IdentifyPacket
def initialize(token, properties, large_threshold, compress, shard)
@op = Discord::Client::OP_IDENTIFY
@d = IdentifyPayload.new(token, properties, large_threshold, compress, shard)
end
JSON.mapping(
op: Int32,
d: IdentifyPayload
)
end
struct IdentifyPayload
def initialize(@token, @properties, @compress, @large_threshold, @shard)
end
JSON.mapping({
token: String,
properties: IdentifyProperties,
compress: Bool,
large_threshold: Int32,
shard: Tuple(Int32, Int32)?,
})
end
struct IdentifyProperties
def initialize(@os, @browser, @device, @referrer, @referring_domain)
end
JSON.mapping(
os: {key: "$os", type: String},
browser: {key: "$browser", type: String},
device: {key: "$device", type: String},
referrer: {key: "$referrer", type: String},
referring_domain: {key: "$referring_domain", type: String}
)
end
struct ResumePacket
def initialize(token, session_id, seq)
@op = Discord::Client::OP_RESUME
@d = ResumePayload.new(token, session_id, seq)
end
JSON.mapping(
op: Int32,
d: ResumePayload
)
end
# :nodoc:
struct ResumePayload
def initialize(@token, @session_id, @seq)
end
JSON.mapping(
token: String,
session_id: String,
seq: Int64
)
end
struct StatusUpdatePacket
def initialize(status, game, afk, since)
@op = Discord::Client::OP_STATUS_UPDATE
@d = StatusUpdatePayload.new(status, game, afk, since)
end
JSON.mapping(
op: Int32,
d: StatusUpdatePayload
)
end
# :nodoc:
struct StatusUpdatePayload
def initialize(@status, @game, @afk, @since)
end
JSON.mapping(
status: {type: String?, emit_null: true},
game: {type: GamePlaying?, emit_null: true},
afk: Bool,
since: {type: Int64, nilable: true, emit_null: true}
)
end
struct VoiceStateUpdatePacket
def initialize(guild_id, channel_id, self_mute, self_deaf)
@op = Discord::Client::OP_VOICE_STATE_UPDATE
@d = VoiceStateUpdatePayload.new(guild_id, channel_id, self_mute, self_deaf)
end
JSON.mapping(
op: Int32,
d: VoiceStateUpdatePayload
)
end
# :nodoc:
struct VoiceStateUpdatePayload
def initialize(@guild_id, @channel_id, @self_mute, @self_deaf)
end
JSON.mapping(
guild_id: UInt64,
channel_id: {type: UInt64?, emit_null: true},
self_mute: Bool,
self_deaf: Bool
)
end
struct RequestGuildMembersPacket
def initialize(guild_id, query, limit)
@op = Discord::Client::OP_REQUEST_GUILD_MEMBERS
@d = RequestGuildMembersPayload.new(guild_id, query, limit)
end
JSON.mapping(
op: Int32,
d: RequestGuildMembersPayload
)
end
# :nodoc:
struct RequestGuildMembersPayload
def initialize(@guild_id, @query, @limit)
end
JSON.mapping(
guild_id: UInt64,
query: String,
limit: Int32
)
end
struct HelloPayload
JSON.mapping(
heartbeat_interval: UInt32,
_trace: Array(String)
)
end
# This one is special from simply Guild since it also has fields for members
# and presences.
struct GuildCreatePayload
JSON.mapping(
id: Snowflake,
name: String,
icon: String?,
splash: String?,
owner_id: Snowflake,
region: String,
afk_channel_id: Snowflake?,
afk_timeout: Int32?,
verification_level: UInt8,
roles: Array(Role),
emoji: {type: Array(Emoji), key: "emojis"},
features: Array(String),
large: Bool,
voice_states: Array(VoiceState),
unavailable: Bool?,
member_count: Int32,
members: Array(GuildMember),
channels: Array(Channel),
presences: Array(Presence),
widget_channel_id: Snowflake?,
default_message_notifications: UInt8,
explicit_content_filter: UInt8,
system_channel_id: Snowflake?
)
{% unless flag?(:correct_english) %}
def emojis
emoji
end
{% end %}
end
struct GuildDeletePayload
JSON.mapping(
id: Snowflake,
unavailable: Bool?
)
end
struct GuildBanPayload
JSON.mapping(
user: User,
guild_id: Snowflake
)
end
struct GuildEmojiUpdatePayload
JSON.mapping(
guild_id: Snowflake,
emoji: {type: Array(Emoji), key: "emojis"}
)
{% unless flag?(:correct_english) %}
def emojis
emoji
end
{% end %}
end
struct GuildIntegrationsUpdatePayload
JSON.mapping(
guild_id: Snowflake
)
end
struct GuildMemberAddPayload
JSON.mapping(
user: User,
nick: String?,
roles: Array(Snowflake),
joined_at: {type: Time?, converter: TimestampConverter},
deaf: Bool,
mute: Bool,
guild_id: Snowflake
)
end
struct GuildMemberUpdatePayload
JSON.mapping(
user: User,
roles: Array(Snowflake),
nick: {type: String, nilable: true},
guild_id: Snowflake
)
end
struct GuildMemberRemovePayload
JSON.mapping(
user: User,
guild_id: Snowflake
)
end
struct GuildMembersChunkPayload
JSON.mapping(
guild_id: Snowflake,
members: Array(GuildMember)
)
end
struct GuildRolePayload
JSON.mapping(
guild_id: Snowflake,
role: Role
)
end
struct GuildRoleDeletePayload
JSON.mapping(
guild_id: Snowflake,
role_id: Snowflake
)
end
struct MessageReactionPayload
JSON.mapping(
user_id: Snowflake,
channel_id: Snowflake,
message_id: Snowflake,
emoji: ReactionEmoji
)
end
struct MessageReactionRemoveAllPayload
JSON.mapping(
channel_id: Snowflake,
message_id: Snowflake
)
end
struct MessageUpdatePayload
JSON.mapping(
type: UInt8?,
content: String?,
id: Snowflake,
channel_id: Snowflake,
author: User?,
timestamp: {type: Time?, converter: TimestampConverter},
tts: Bool?,
mention_everyone: Bool?,
mentions: Array(User)?,
mention_roles: Array(Snowflake),
attachments: Array(Attachment)?,
embeds: Array(Embed)?,
pinned: Bool?
)
end
struct MessageDeletePayload
JSON.mapping(
id: Snowflake,
channel_id: Snowflake
)
end
struct MessageDeleteBulkPayload
JSON.mapping(
ids: Array(Snowflake),
channel_id: Snowflake
)
end
struct PresenceUpdatePayload
JSON.mapping(
user: PartialUser,
roles: Array(Snowflake),
game: GamePlaying?,
nick: String?,
guild_id: Snowflake,
status: String
)
end
struct TypingStartPayload
JSON.mapping(
channel_id: Snowflake,
user_id: Snowflake,
timestamp: {type: Time, converter: Time::EpochConverter}
)
end
struct VoiceServerUpdatePayload
JSON.mapping(
token: String,
guild_id: Snowflake,
endpoint: String
)
end
struct WebhooksUpdatePayload
JSON.mapping(
guild_id: Snowflake,
channel_id: Snowflake
)
end
struct ChannelPinsUpdatePayload
JSON.mapping(
last_pin_timestamp: {type: Time, converter: TimestampConverter},
channel_id: Snowflake
)
end
end
end