-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord.gleam
347 lines (312 loc) · 9.05 KB
/
discord.gleam
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
//// This contains all types needed to communicate with either the Gateway or REST API.
import gleam/dynamic
import gleam/option.{type Option}
import glyph/internal/network/rest
/// Generic Discord Error
pub type DiscordError
/// The [data structure](https://discord.com/developers/docs/reference#snowflakes) Discord uses for UUIDs.
pub type Snowflake =
String
/// Model for a Discord [Application](https://discord.com/developers/docs/resources/application).
///
/// TODO: summary is deprecated and will be removed in v11
pub type Application {
Application(
id: Snowflake,
name: String,
icon: Option(String),
description: String,
rpc_origins: Option(List(String)),
bot_public: Bool,
bot_require_code_grant: Bool,
bot: Option(Bot),
terms_of_service_url: Option(String),
privacy_policy_url: Option(String),
owner: Option(Owner),
summary: Option(String),
verify_key: String,
team: Option(Team),
guild_id: Option(Snowflake),
primary_sku_id: Option(Snowflake),
slug: Option(String),
cover_image: Option(String),
flags: Option(Int),
approximate_guild_count: Option(Int),
redirect_uris: Option(List(String)),
interactions_endpoint_url: Option(String),
role_connections_verification_url: Option(String),
tags: Option(List(String)),
install_params: Option(InstallParams),
custom_install_url: Option(String),
)
}
/// Model for a partial Application received in the Ready event
pub type ReadyApplication {
ReadyApplication(id: Snowflake, flags: Int)
}
/// Model for a Discord [User](https://discord.com/developers/docs/resources/user#user-object).
///
/// Some additional fields marked as optional here due to the use of partial user objects
/// in other areas of the API.
pub type User {
User(
id: Snowflake,
username: String,
discriminator: String,
global_name: Option(String),
avatar: Option(String),
bot: Option(Bool),
system: Option(Bool),
mfa_enabled: Option(Bool),
banner: Option(String),
accent_color: Option(Int),
locale: Option(String),
email: Option(String),
flags: Option(Int),
premium_type: Option(Int),
public_flags: Option(Int),
avatar_decoration: Option(String),
)
}
pub type Bot =
User
pub type Owner =
User
/// Model for a [Message](https://discord.com/developers/docs/resources/channel#message-object) object as received from the gateway.
///
/// TODO: add remaining fields
pub type Message {
Message(
id: Snowflake,
channel_id: Snowflake,
author: User,
content: String,
tts: Bool,
mention_everyone: Bool,
pinned: Bool,
message_type: Int,
)
}
/// Model for the [payload](https://discord.com/developers/docs/resources/channel#create-message) when creating a message.
///
/// TODO: message components (will come w/ interactions / commands), files
pub type MessagePayload {
MessagePayload(
content: Option(String),
tts: Option(Bool),
embeds: Option(List(Embed)),
allowed_mentions: Option(AllowedMentions),
message_reference: Option(MessageReference),
sticker_ids: Option(List(Snowflake)),
flags: Option(Int),
nonce: Option(String),
enforce_nonce: Option(Bool),
)
}
/// This contains information needed for a `message_reference` attached
/// to a `MessagePayload`. Needed when replying to a message.
pub type MessageReference {
MessageReference(
message_id: Option(Snowflake),
channel_id: Option(Snowflake),
guild_id: Option(Snowflake),
fail_if_not_exists: Option(Bool),
)
}
/// This contains information regarding the mentions contained within
/// a message's content.
pub type AllowedMentions {
AllowedMentions(
parse: List(MentionType),
roles: List(Snowflake),
users: List(Snowflake),
replied_user: Bool,
)
}
/// The supported types of mentions.
pub type MentionType {
Roles
Users
Everyone
}
pub fn mention_type_to_string(m: MentionType) -> String {
case m {
Roles -> "roles"
Users -> "users"
Everyone -> "everyone"
}
}
/// Model for an [embed](https://discord.com/developers/docs/resources/channel#embed-object) that can be
/// attached to a message.
pub type Embed {
Embed(
title: Option(String),
description: Option(String),
url: Option(String),
timestamp: Option(String),
color: Option(Int),
footer: Option(EmbedFooter),
image: Option(EmbedImage),
thumbnail: Option(EmbedThumbnail),
video: Option(EmbedVideo),
provider: Option(EmbedProvider),
author: Option(EmbedAuthor),
fields: Option(List(EmbedField)),
)
}
/// Supported information for an embed's footer.
pub type EmbedFooter {
EmbedFooter(
text: String,
icon_url: Option(String),
proxy_icon_url: Option(String),
)
}
/// The information necessary to send an image with an embed.
pub type EmbedImage {
EmbedImage(
url: String,
proxy_url: Option(String),
height: Option(Int),
width: Option(Int),
)
}
/// The information necessary to send a thumbnail with an embed.
pub type EmbedThumbnail {
EmbedThumbnail(
url: String,
proxy_url: Option(String),
height: Option(Int),
width: Option(Int),
)
}
/// The information necessary to send a video with an embed.
pub type EmbedVideo {
EmbedVideo(
url: Option(String),
proxy_url: Option(String),
height: Option(Int),
width: Option(Int),
)
}
/// Metadata about the source of the embedded data.
pub type EmbedProvider {
EmbedProvider(name: Option(String), url: Option(String))
}
/// Metadata about the author of the embedded data.
pub type EmbedAuthor {
EmbedAuthor(
name: String,
url: Option(String),
icon_url: Option(String),
proxy_icon_url: Option(String),
)
}
/// Generic fields that can be added to an embed.
pub type EmbedField {
EmbedField(name: String, value: String, inline: Option(Bool))
}
/// Model for a [Team](https://discord.com/developers/docs/topics/teams#data-models-team-object) object.
pub type Team {
Team(
id: Snowflake,
icon: Option(String),
members: List(Member),
name: String,
owner_user_id: Snowflake,
)
}
/// Model for a [Team Member](https://discord.com/developers/docs/topics/teams#data-models-team-member-object) object.
pub type Member {
Member(membership_state: Int, team_id: Snowflake, user: User, role: String)
}
/// Model for [Membership State](https://discord.com/developers/docs/topics/teams#data-models-membership-state-enum).
pub type MembershipState {
INVITED
// -> 1
ACCEPTED
// -> 2
}
/// Model for [Install Params](https://discord.com/developers/docs/resources/application#install-params-object).
pub type InstallParams {
InstallParams(scopes: List(String), permissions: String)
}
/// Model for the [response](https://discord.com/developers/docs/topics/gateway#get-gateway-bot) received from `/gateway/bot`.
pub type GetGatewayBot {
GetGatewayBot(
url: String,
shards: Int,
session_start_limit: SessionStartLimit,
)
}
/// Model for [Session Start Limit Object](https://discord.com/developers/docs/topics/gateway#session-start-limit-object).
pub type SessionStartLimit {
SessionStartLimit(
total: Int,
remaining: Int,
reset_after: Int,
max_concurrency: Int,
)
}
/// Top-level structure of [payloads](https://discord.com/developers/docs/topics/gateway-events#payload-structure) between gateway and client.
pub type GatewayEvent {
GatewayEvent(op: Int, d: dynamic.Dynamic, s: Option(Int), t: Option(String))
}
/// These represent Glyph's supported gateway events and the necessary function signature
/// for callbacks that get passed in to the `BotClient`.
pub type EventHandler {
EventHandler(
on_message_create: fn(BotClient, Message) -> Result(Nil, DiscordError),
)
}
// The following are Gateway data models for the data contained within the `d` field of a GatewayEvent
/// Structure of a [Hello](https://discord.com/developers/docs/topics/gateway#hello-event) event.
pub type HelloEvent {
HelloEvent(heartbeat_interval: Int)
}
/// Structure of a [Ready](https://discord.com/developers/docs/topics/gateway-events#ready-ready-event-fields) event.
pub type ReadyEvent {
ReadyEvent(
v: Int,
user: User,
guilds: dynamic.Dynamic,
session_id: String,
resume_gateway_url: String,
shard: Option(List(Int)),
application: ReadyApplication,
)
}
/// The following are [gateway intents](https://discord.com/developers/docs/topics/gateway#gateway-intents) which represent what events you subscribe to.
pub type GatewayIntent {
Guilds
GuildMembers
GuildModeration
GuildEmojisAndStickers
GuildIntegrations
GuildWebhooks
GuildInvites
GuildVoiceStates
GuildPresences
GuildMessages
GuildMessageReactions
GuildMessageTyping
DirectMessages
DirectMessageReactions
DirectMessageTyping
MessageContent
GuildScheduledEvents
AutoModerationConfiguration
AutoModerationExecution
}
/// Type that contains necessary information when communicating with the Discord API
pub type BotClient {
BotClient(
token_type: rest.TokenType,
token: String,
client_url: String,
client_version: String,
intents: Int,
handlers: EventHandler,
rest_client: rest.RESTClient,
)
}