zynko 0.1.0

Biblioteca oficial para bots do Zynko: API, gateway e voz.
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! The Zynko API's objects, as the server sends them (docs.zynko.org/api/objetos). Ids are
//! strings; times are milliseconds since 1970.
//!
//! Every model reads tolerantly: unknown fields are ignored and missing ones take their default,
//! so a newer server does not break an older bot.

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// An id (a snowflake, as a string).
pub type Id = String;

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct User {
    pub id: Id,
    pub username: String,
    pub display_name: String,
    pub avatar: Option<String>,
    pub bot: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Profile {
    pub user: User,
    pub bio: Option<String>,
    pub banner: Option<String>,
    pub accent: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Guild {
    pub id: Id,
    pub name: String,
    pub icon: Option<String>,
    pub owner_id: Id,
}

/// A server as READY and GUILD_CREATE bring it.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct GuildFull {
    pub id: Id,
    pub name: String,
    pub icon: Option<String>,
    pub owner_id: Id,
    pub channels: Vec<Channel>,
    pub members: Vec<Member>,
    /// Ids of the members online now.
    pub presences: Vec<Id>,
    pub roles: Vec<Role>,
    pub emojis: Vec<Emoji>,
}

/// Channel types (`Channel::kind`).
pub struct ChannelType;

impl ChannelType {
    pub const TEXT: u8 = 0;
    pub const DM: u8 = 1;
    pub const VOICE: u8 = 2;
    pub const CATEGORY: u8 = 4;
    pub const POST: u8 = 11;
    pub const FORUM: u8 = 15;
}

/// A channel's permission overwrite.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Overwrite {
    pub id: Id,
    /// 0 = role, 1 = member.
    #[serde(rename = "type")]
    pub kind: u8,
    pub allow: u64,
    pub deny: u64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ForumTag {
    pub id: Id,
    pub name: String,
    pub emoji: Option<String>,
    pub moderated: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Channel {
    pub id: Id,
    pub guild_id: Option<Id>,
    /// See [`ChannelType`].
    #[serde(rename = "type")]
    pub kind: u8,
    pub name: String,
    pub topic: Option<String>,
    pub position: i64,
    pub parent_id: Option<Id>,
    pub last_message_id: Option<Id>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rate_limit_per_user: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner_id: Option<Id>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pinned: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locked: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub applied_tags: Option<Vec<Id>>,
    /// The voice server a room runs on, when someone picked one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_server: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub available_tags: Option<Vec<ForumTag>>,
    /// In a DM: the other person.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recipients: Option<Vec<User>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_overwrites: Option<Vec<Overwrite>>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Member {
    pub guild_id: Id,
    pub user: User,
    pub nick: Option<String>,
    pub is_admin: bool,
    pub joined_at: i64,
    /// Not including @everyone, which everybody has.
    pub roles: Vec<Id>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Role {
    pub id: Id,
    pub guild_id: Id,
    pub name: String,
    pub color: Option<u32>,
    pub position: i64,
    pub permissions: u64,
    pub hoist: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Emoji {
    pub id: Id,
    pub guild_id: Id,
    pub name: String,
    pub url: String,
    pub animated: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Attachment {
    pub id: Id,
    pub filename: String,
    pub content_type: String,
    pub size: u64,
    /// `/api/attachments/{id}`: needs the token to download.
    pub url: String,
    pub thumbnail: Option<String>,
    pub width: Option<u32>,
    pub height: Option<u32>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ReactionCount {
    /// A Unicode emoji, or `name:id` for a server emoji.
    pub emoji: String,
    pub count: u64,
    pub me: bool,
}

/// The message a reply answers.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MessageRef {
    pub id: Id,
    pub author: Option<User>,
    pub content: Option<String>,
}

/// A link preview.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Embed {
    pub url: String,
    pub site_name: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
    pub author: Option<String>,
    pub color: Option<String>,
    pub image: Option<Value>,
    pub video: Option<Value>,
}

/// A card's small line on top.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CardAuthor {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon_url: Option<String>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CardField {
    pub name: String,
    pub value: String,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub inline: bool,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CardImage {
    pub url: String,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CardFooter {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon_url: Option<String>,
}

/// A bot's card: a title, text, fields and pictures with a colored edge.
///
/// ```
/// use zynko::Card;
/// let card = Card::new().title("Placar").field("Ana", "3", true).color(0xa594f9);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Card {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Markdown, like a message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Where the title points.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// The edge's color, 0xRRGGBB.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<CardAuthor>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<CardField>,
    /// Wide, under the text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<CardImage>,
    /// Small, beside the text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumbnail: Option<CardImage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub footer: Option<CardFooter>,
    /// Shown in the footer, in ms.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<i64>,
}

impl Card {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }
    pub fn description(mut self, text: impl Into<String>) -> Self {
        self.description = Some(text.into());
        self
    }
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self
    }
    pub fn color(mut self, color: u32) -> Self {
        self.color = Some(color);
        self
    }
    pub fn author(mut self, name: impl Into<String>) -> Self {
        self.author = Some(CardAuthor {
            name: name.into(),
            ..Default::default()
        });
        self
    }
    pub fn field(
        mut self,
        name: impl Into<String>,
        value: impl Into<String>,
        inline: bool,
    ) -> Self {
        self.fields.push(CardField {
            name: name.into(),
            value: value.into(),
            inline,
        });
        self
    }
    pub fn image(mut self, url: impl Into<String>) -> Self {
        self.image = Some(CardImage { url: url.into() });
        self
    }
    pub fn thumbnail(mut self, url: impl Into<String>) -> Self {
        self.thumbnail = Some(CardImage { url: url.into() });
        self
    }
    pub fn footer(mut self, text: impl Into<String>) -> Self {
        self.footer = Some(CardFooter {
            text: text.into(),
            icon_url: None,
        });
        self
    }
    pub fn timestamp(mut self, ms: i64) -> Self {
        self.timestamp = Some(ms);
        self
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ButtonStyle {
    /// Lavender.
    #[default]
    Primary,
    /// Grey.
    Secondary,
    /// Green.
    Success,
    /// Red.
    Danger,
    /// Opens `url`; the bot is not told.
    Link,
}

/// A button on a bot's message.
///
/// ```
/// use zynko::Button;
/// let row = vec![Button::primary("Mais um", "mais").emoji("âž•"), Button::link("Site", "https://zynko.org")];
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Button {
    pub style: ButtonStyle,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emoji: Option<String>,
    /// What the bot gets back when it is clicked (all but links).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_id: Option<String>,
    /// Link buttons only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub disabled: bool,
}

impl Button {
    /// A button of any style but `Link`.
    pub fn new(style: ButtonStyle, label: impl Into<String>, custom_id: impl Into<String>) -> Self {
        Self {
            style,
            label: Some(label.into()),
            custom_id: Some(custom_id.into()),
            ..Default::default()
        }
    }
    pub fn primary(label: impl Into<String>, custom_id: impl Into<String>) -> Self {
        Self::new(ButtonStyle::Primary, label, custom_id)
    }
    pub fn secondary(label: impl Into<String>, custom_id: impl Into<String>) -> Self {
        Self::new(ButtonStyle::Secondary, label, custom_id)
    }
    pub fn success(label: impl Into<String>, custom_id: impl Into<String>) -> Self {
        Self::new(ButtonStyle::Success, label, custom_id)
    }
    pub fn danger(label: impl Into<String>, custom_id: impl Into<String>) -> Self {
        Self::new(ButtonStyle::Danger, label, custom_id)
    }
    pub fn link(label: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            style: ButtonStyle::Link,
            label: Some(label.into()),
            url: Some(url.into()),
            ..Default::default()
        }
    }
    pub fn emoji(mut self, emoji: impl Into<String>) -> Self {
        self.emoji = Some(emoji.into());
        self
    }
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

/// Rows of buttons, top to bottom: up to 5 rows of 5.
pub type Components = Vec<Vec<Button>>;

/// The command or click a bot's message answers.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct InteractionRef {
    pub id: Id,
    /// The command; absent for a button.
    pub name: Option<String>,
    pub user: User,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OptionType {
    #[default]
    String,
    Integer,
    Number,
    Boolean,
    /// Someone in the server (arrives as an id).
    User,
    /// A text channel (arrives as an id).
    Channel,
    /// A role (arrives as an id).
    Role,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CommandChoice {
    pub name: String,
    /// A string or a number.
    pub value: Value,
}

/// One field of a "/" command.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct CommandOption {
    pub name: String,
    pub description: String,
    #[serde(rename = "type")]
    pub kind: OptionType,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub required: bool,
    /// The only values allowed.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub choices: Vec<CommandChoice>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_value: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_value: Option<f64>,
}

impl CommandOption {
    pub fn new(name: impl Into<String>, description: impl Into<String>, kind: OptionType) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            kind,
            ..Default::default()
        }
    }
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }
    pub fn choice(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
        self.choices.push(CommandChoice {
            name: name.into(),
            value: value.into(),
        });
        self
    }
    pub fn min(mut self, v: f64) -> Self {
        self.min_value = Some(v);
        self
    }
    pub fn max(mut self, v: f64) -> Self {
        self.max_value = Some(v);
        self
    }
}

/// A "/" command (docs.zynko.org/bots/comandos).
///
/// ```
/// use zynko::{Command, CommandOption, OptionType};
/// let dado = Command::new("dado", "Rola um dado")
///     .option(CommandOption::new("lados", "Quantos lados", OptionType::Integer).min(2.0).max(100.0));
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Command {
    /// 1 to 32 lowercase letters, digits, _ or -.
    pub name: String,
    pub description: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub options: Vec<CommandOption>,
}

impl Command {
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            options: Vec::new(),
        }
    }
    pub fn option(mut self, option: CommandOption) -> Self {
        self.options.push(option);
        self
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InteractionType {
    #[default]
    Command,
    Button,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct CommandOptionValue {
    pub name: String,
    #[serde(rename = "type")]
    pub kind: OptionType,
    pub value: Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct InteractionCommand {
    pub name: String,
    pub options: Vec<CommandOptionValue>,
}

/// INTERACTION_CREATE, as it comes.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct InteractionData {
    pub id: Id,
    pub token: String,
    #[serde(rename = "type")]
    pub kind: InteractionType,
    pub channel_id: Id,
    pub guild_id: Option<Id>,
    pub user: User,
    pub member: Option<Member>,
    pub command: Option<InteractionCommand>,
    pub custom_id: Option<String>,
    pub message: Option<MessageData>,
}

/// A message, as it comes. MESSAGE_UPDATE brings only some fields; the rest stay at their
/// defaults.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MessageData {
    pub id: Id,
    pub channel_id: Id,
    pub author: User,
    pub content: String,
    pub timestamp: i64,
    pub edited_timestamp: Option<i64>,
    pub reply_to: Option<MessageRef>,
    pub attachments: Vec<Attachment>,
    pub reactions: Vec<ReactionCount>,
    pub nonce: Option<String>,
    pub embeds: Vec<Embed>,
    pub cards: Vec<Card>,
    pub components: Components,
    pub interaction: Option<InteractionRef>,
    /// Only the person who used the command sees it.
    pub ephemeral: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Invite {
    pub code: String,
    pub guild_id: Id,
    pub creator_id: Id,
    pub uses: u64,
    pub max_uses: Option<u64>,
    pub expires_at: Option<i64>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct VoiceState {
    pub user_id: Id,
    pub guild_id: Option<Id>,
    /// `None` when the account left voice.
    pub channel_id: Option<Id>,
    pub self_mute: bool,
    pub self_deaf: bool,
    pub self_stream: bool,
    pub self_video: bool,
    pub session_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Ready {
    pub user: User,
    pub guilds: Vec<GuildFull>,
    pub dm_channels: Vec<Channel>,
    pub presences: Vec<Id>,
    pub voice_states: Vec<VoiceState>,
    pub session_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ReactionEvent {
    pub message_id: Id,
    pub channel_id: Id,
    pub user_id: Id,
    pub emoji: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct TypingEvent {
    pub channel_id: Id,
    pub user_id: Id,
    pub timestamp: i64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MessageDeleteEvent {
    pub id: Id,
    pub channel_id: Id,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MemberRemoveEvent {
    pub guild_id: Id,
    pub user_id: Id,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct PresenceEvent {
    pub user_id: Id,
    /// `"online"` or `"offline"`.
    pub status: String,
}

/// The answer to joining a voice room (POST /channels/{id}/voice).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct VoiceJoin {
    pub url: String,
    /// The voice server's id (Brasil 1, Brasil 2...).
    pub server: Option<String>,
    pub token: String,
    pub room: String,
    pub can_speak: Option<bool>,
    pub can_stream: Option<bool>,
}