This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
/
guild.cr
265 lines (237 loc) · 6.17 KB
/
guild.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
require "./converters"
require "./voice"
module Discord
struct Guild
# :nodoc:
def initialize(payload : Gateway::GuildCreatePayload)
@id = payload.id
@name = payload.name
@icon = payload.icon
@splash = payload.splash
@owner_id = payload.owner_id
@region = payload.region
@afk_channel_id = payload.afk_channel_id
@afk_timeout = payload.afk_timeout
@verification_level = payload.verification_level
@roles = payload.roles
@emoji = payload.emoji
@features = payload.features
@widget_channel_id = payload.widget_channel_id
@default_message_notifications = payload.default_message_notifications
@explicit_content_filter = payload.explicit_content_filter
@system_channel_id = payload.system_channel_id
end
JSON.mapping(
id: Snowflake,
name: String,
icon: String?,
splash: String?,
owner_id: Snowflake,
region: String,
afk_channel_id: Snowflake?,
afk_timeout: Int32?,
embed_enabled: Bool?,
embed_channel_id: Snowflake?,
verification_level: UInt8,
roles: Array(Role),
emoji: {type: Array(Emoji), key: "emojis"},
features: Array(String),
widget_enabled: {type: Bool, nilable: true},
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 %}
# Produces a CDN URL to this guild's icon in the given `format` and `size`,
# or `nil` if no icon is set.
def icon_url(format : CDN::GuildIconFormat = CDN::GuildIconFormat::WebP,
size : Int32 = 128)
if icon = @icon
CDN.guild_icon(id, icon, format, size)
end
end
# Produces a CDN URL to this guild's splash in the given `format` and `size`,
# or `nil` if no splash is set.
def splash_url(format : CDN::GuildSplashFormat = CDN::GuildSplashFormat::WebP,
size : Int32 = 128)
if splash = @splash
CDN.guild_splash(id, splash, format, size)
end
end
end
struct UnavailableGuild
JSON.mapping(
id: Snowflake,
unavailable: Bool
)
end
struct GuildEmbed
JSON.mapping(
enabled: Bool,
channel_id: Snowflake?
)
end
struct GuildMember
# :nodoc:
def initialize(user : User, partial_member : PartialGuildMember)
@user = user
@roles = partial_member.roles
@nick = partial_member.nick
@joined_at = partial_member.joined_at
@mute = partial_member.mute
@deaf = partial_member.deaf
end
# :nodoc:
def initialize(payload : Gateway::GuildMemberAddPayload | GuildMember, roles : Array(Snowflake), nick : String?)
initialize(payload)
@nick = nick
@roles = roles
end
# :nodoc:
def initialize(payload : Gateway::GuildMemberAddPayload | GuildMember)
@user = payload.user
@nick = payload.nick
@roles = payload.roles
@joined_at = payload.joined_at
@deaf = payload.deaf
@mute = payload.mute
end
# :nodoc:
def initialize(payload : Gateway::PresenceUpdatePayload)
@user = User.new(payload.user)
@nick = payload.nick
@roles = payload.roles
# Presence updates have no joined_at or deaf/mute, thanks Discord
end
JSON.mapping(
user: User,
nick: String?,
roles: Array(Snowflake),
joined_at: {type: Time?, converter: MaybeTimestampConverter},
deaf: Bool?,
mute: Bool?
)
# Produces a string to mention this member in a message
def mention
if nick
"<@!#{user.id}>"
else
"<@#{user.id}>"
end
end
end
struct PartialGuildMember
JSON.mapping(
nick: String?,
roles: Array(Snowflake),
joined_at: {type: Time, converter: TimestampConverter},
deaf: Bool,
mute: Bool
)
end
struct Integration
JSON.mapping(
id: Snowflake,
name: String,
type: String,
enabled: Bool,
syncing: Bool,
role_id: Snowflake,
expire_behaviour: {type: UInt8, key: "expire_behavior"},
expire_grace_period: Int32,
user: User,
account: IntegrationAccount,
synced_at: {type: Time, converter: Time::EpochConverter}
)
{% unless flag?(:correct_english) %}
def expire_behavior
expire_behaviour
end
{% end %}
end
struct IntegrationAccount
JSON.mapping(
id: String,
name: String
)
end
struct Emoji
JSON.mapping(
id: Snowflake,
name: String,
roles: Array(Snowflake),
require_colons: Bool,
managed: Bool,
animated: Bool
)
# Produces a CDN URL to this emoji's image in the given `size`. Will return
# a PNG, or GIF if the emoji is animated.
def image_url(size : Int32 = 128)
if animated
image_url(:gif, size)
else
image_url(:png, size)
end
end
# Produces a CDN URL to this emoji's image in the given `format` and `size`
def image_url(format : CDN::CustomEmojiFormat, size : Int32 = 128)
CDN.custom_emoji(id, format, size)
end
# Produces a string to mention this emoji in a message
def mention
if animated
"<a:#{name}:#{id}>"
else
"<:#{name}:#{id}>"
end
end
end
struct Role
JSON.mapping(
id: Snowflake,
name: String,
permissions: Permissions,
colour: {type: UInt32, key: "color"},
hoist: Bool,
position: Int32,
managed: Bool,
mentionable: Bool
)
{% unless flag?(:correct_english) %}
def color
colour
end
{% end %}
# Produces a string to mention this role in a message
def mention
"<@&#{id}>"
end
end
struct GuildBan
JSON.mapping(
user: User,
reason: String?
)
end
struct GamePlaying
def initialize(@name = nil, @type = nil, @url = nil)
end
JSON.mapping(
name: String?,
type: Int64? | String?,
url: String?
)
end
struct Presence
JSON.mapping(
user: PartialUser,
game: GamePlaying?,
status: String
)
end
end