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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//! The bot: gateway, caches and events.

use std::panic::AssertUnwindSafe;
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard};
use std::time::Duration;

use futures_util::FutureExt;
use serde::de::DeserializeOwned;
use serde_json::Value;
use tokio::sync::{broadcast, mpsc, oneshot, watch};

use crate::bits::Intents;
use crate::cache::{Cache, CachedGuild};
use crate::error::{Error, Result};
use crate::events::{self, Event, EventHandler, ReconnectInfo};
use crate::gateway::{Gateway, GatewayEvent};
use crate::interaction::Interaction;
use crate::message::Message;
use crate::permissions::compute_permissions;
use crate::rest::{Rest, SendOptions};
use crate::types::*;

/// Sets a client up: `Client::builder(token).intents(...).api(...).event_handler(...).build()`.
pub struct ClientBuilder {
    token: String,
    intents: u64,
    api: Option<String>,
    gateway: Option<String>,
    retries: u32,
    handler: Option<Arc<dyn EventHandler>>,
}

impl ClientBuilder {
    /// What to receive (see [`Intents`]). Default: servers and messages.
    pub fn intents(mut self, intents: u64) -> Self {
        self.intents = intents;
        self
    }

    /// https://zynko.org/api unless you run your own server.
    pub fn api(mut self, api: impl Into<String>) -> Self {
        self.api = Some(api.into());
        self
    }

    /// The gateway; worked out from `api` unless given.
    pub fn gateway(mut self, url: impl Into<String>) -> Self {
        self.gateway = Some(url.into());
        self
    }

    /// How many times a 429 is waited out and tried again (default 3; 0: never).
    pub fn retries(mut self, retries: u32) -> Self {
        self.retries = retries;
        self
    }

    /// Who reacts to events.
    pub fn event_handler(mut self, handler: impl EventHandler) -> Self {
        self.handler = Some(Arc::new(handler));
        self
    }

    pub fn build(self) -> Result<Client> {
        if self.token.trim().is_empty() {
            return Err(Error::Other(
                "Falta o token do bot (crie um em dev.zynko.org)".into(),
            ));
        }
        let rest = Rest::new(&self.token, self.api.as_deref(), self.retries);
        let url = self.gateway.unwrap_or_else(|| {
            let api = rest.api();
            let ws = if let Some(rest) = api.strip_prefix("https") {
                format!("wss{rest}")
            } else if let Some(rest) = api.strip_prefix("http") {
                format!("ws{rest}")
            } else {
                api.to_string()
            };
            format!("{ws}/gateway")
        });
        let (events, _) = broadcast::channel(1024);
        let (status, _) = watch::channel(Status::Idle);
        Ok(Client {
            inner: Arc::new(Inner {
                gateway: Gateway::new(url, self.token, self.intents),
                rest,
                intents: self.intents,
                cache: RwLock::new(Cache::default()),
                handler: self.handler,
                events,
                status,
                first_ready: Mutex::new(None),
                #[cfg(feature = "voice")]
                voice: Mutex::new(None),
            }),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
enum Status {
    Idle,
    Running,
    /// Stopped, with the reason when it was not asked for.
    Closed(Option<String>),
}

struct Inner {
    rest: Rest,
    intents: u64,
    gateway: Arc<Gateway>,
    cache: RwLock<Cache>,
    handler: Option<Arc<dyn EventHandler>>,
    events: broadcast::Sender<Event>,
    status: watch::Sender<Status>,
    first_ready: Mutex<Option<oneshot::Sender<Result<User>>>>,
    #[cfg(feature = "voice")]
    voice: Mutex<Option<crate::voice::VoiceConnection>>,
}

/// A Zynko bot: connects to the gateway, keeps servers, channels and members up to date, and
/// turns events into [`EventHandler`] calls (or an [`Event`] stream). Cheap to clone: every
/// clone is the same bot.
///
/// ```no_run
/// use zynko::{async_trait, Client, EventHandler, HandlerResult, Intents, Message};
///
/// struct Bot;
///
/// #[async_trait]
/// impl EventHandler for Bot {
///     async fn message_create(&self, _ctx: Client, msg: Message) -> HandlerResult {
///         if !msg.mine() && msg.content == "!ping" {
///             msg.reply("pong").await?;
///         }
///         Ok(())
///     }
/// }
///
/// # async fn run() -> zynko::Result<()> {
/// let client = Client::builder(std::env::var("ZYNKO_TOKEN").unwrap())
///     .intents(Intents::GUILDS | Intents::MESSAGES)
///     .event_handler(Bot)
///     .build()?;
/// client.start().await
/// # }
/// ```
#[derive(Clone)]
pub struct Client {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for Client {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("api", &self.inner.rest.api())
            .field("intents", &self.inner.intents)
            .finish_non_exhaustive()
    }
}

fn parse<T: DeserializeOwned>(t: &str, d: &Value) -> Result<T> {
    serde_json::from_value(d.clone()).map_err(|e| Error::Other(format!("{t} inesperado: {e}")))
}

impl Client {
    pub fn builder(token: impl Into<String>) -> ClientBuilder {
        ClientBuilder {
            token: token.into(),
            intents: Intents::GUILDS | Intents::MESSAGES,
            api: None,
            gateway: None,
            retries: 3,
            handler: None,
        }
    }

    /// The HTTP API.
    pub fn rest(&self) -> &Rest {
        &self.inner.rest
    }

    pub fn intents(&self) -> u64 {
        self.inner.intents
    }

    /// Everything the client knows. A read lock: do not hold it across an `.await`.
    pub fn cache(&self) -> RwLockReadGuard<'_, Cache> {
        self.inner.cache.read().unwrap_or_else(|e| e.into_inner())
    }

    fn cache_mut(&self) -> std::sync::RwLockWriteGuard<'_, Cache> {
        self.inner.cache.write().unwrap_or_else(|e| e.into_inner())
    }

    /// The bot's account, after READY.
    pub fn user(&self) -> Option<User> {
        self.cache().user.clone()
    }

    /// A server, as kept.
    pub fn guild(&self, id: &str) -> Option<CachedGuild> {
        self.cache().guilds.get(id).cloned()
    }

    /// A channel the bot sees (DMs included).
    pub fn channel(&self, id: &str) -> Option<Channel> {
        self.cache().channels.get(id).cloned()
    }

    // ---------- connection ----------

    /// Connects; resolves with the bot's account once READY arrives. The client keeps running
    /// in the background (reconnecting on its own) until [`destroy`](Self::destroy).
    pub async fn login(&self) -> Result<User> {
        if *self.inner.status.borrow() == Status::Running {
            return self
                .user()
                .ok_or_else(|| Error::Other("já conectando".into()));
        }
        let (tx, rx) = oneshot::channel();
        *self
            .inner
            .first_ready
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = Some(tx);
        self.inner.status.send_replace(Status::Running);
        let (gtx, mut grx) = mpsc::unbounded_channel();
        tokio::spawn(self.inner.gateway.clone().run(gtx));
        let client = self.clone();
        tokio::spawn(async move {
            let mut fatal = None;
            while let Some(ev) = grx.recv().await {
                match ev {
                    GatewayEvent::Dispatch(t, d) => client.dispatch(&t, d),
                    GatewayEvent::Reconnecting { code, reason, wait } => {
                        client.emit(Event::Reconnecting(ReconnectInfo { code, reason, wait }))
                    }
                    GatewayEvent::Fatal(msg) => fatal = Some(msg),
                }
            }
            // The gateway stopped: asked to, or the token was refused.
            let pending = client
                .inner
                .first_ready
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .take();
            let msg = fatal.clone().unwrap_or_else(|| "Gateway fechado".into());
            match pending {
                Some(tx) => {
                    let _ = tx.send(Err(Error::Gateway(msg)));
                }
                None => {
                    if fatal.is_some() {
                        client.emit(Event::Error(Arc::new(Error::Gateway(msg))));
                    }
                }
            }
            client.inner.status.send_replace(Status::Closed(fatal));
        });
        rx.await
            .unwrap_or_else(|_| Err(Error::Gateway("Gateway fechado".into())))
    }

    /// Waits until the client stops: `Ok` after [`destroy`](Self::destroy), `Err` when the
    /// gateway refused the token.
    pub async fn closed(&self) -> Result<()> {
        let mut rx = self.inner.status.subscribe();
        let status = rx
            .wait_for(|s| matches!(s, Status::Closed(_)))
            .await
            .map(|s| s.clone());
        match status {
            Ok(Status::Closed(Some(msg))) => Err(Error::Gateway(msg)),
            _ => Ok(()),
        }
    }

    /// Connects and runs until [`destroy`](Self::destroy) or a refused token: the usual end of
    /// `main`.
    pub async fn start(&self) -> Result<()> {
        self.login().await?;
        self.closed().await
    }

    /// Leaves voice and closes the link.
    pub async fn destroy(&self) {
        #[cfg(feature = "voice")]
        {
            let voice = self.voice();
            if let Some(v) = voice {
                v.leave().await;
            }
        }
        self.inner.gateway.close();
    }

    /// Sends a gateway op (the voice connection uses op 4). Returns whether the link was open.
    pub fn send_op(&self, op: u8, d: Value) -> bool {
        self.inner.gateway.send(op, d)
    }

    // ---------- events ----------

    /// Every event from now on, for a `while let Ok(ev) = rx.recv().await` loop. Works with or
    /// without an [`EventHandler`]. A receiver that falls more than 1024 events behind skips
    /// the oldest.
    pub fn subscribe(&self) -> broadcast::Receiver<Event> {
        self.inner.events.subscribe()
    }

    /// Waits for the next event that passes `filter`.
    ///
    /// ```no_run
    /// # async fn f(client: zynko::Client, msg: zynko::Message) -> zynko::Result<()> {
    /// use std::time::Duration;
    /// use zynko::Event;
    /// msg.reply("Qual o seu nome?").await?;
    /// let author = msg.author.id.clone();
    /// let answer = client
    ///     .wait_for(Duration::from_secs(30), |e| matches!(e, Event::MessageCreate(m) if m.author.id == author))
    ///     .await?;
    /// # Ok(()) }
    /// ```
    pub async fn wait_for(
        &self,
        timeout: Duration,
        filter: impl Fn(&Event) -> bool,
    ) -> Result<Event> {
        let mut rx = self.subscribe();
        let wait = async {
            loop {
                match rx.recv().await {
                    Ok(ev) if filter(&ev) => return Ok(ev),
                    Ok(_) | Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(broadcast::error::RecvError::Closed) => {
                        return Err(Error::Gateway("cliente fechado".into()))
                    }
                }
            }
        };
        tokio::time::timeout(timeout, wait)
            .await
            .unwrap_or_else(|_| Err(Error::Timeout("esperando um evento".into())))
    }

    /// Waits for the next new message that passes `filter`.
    pub async fn wait_for_message(
        &self,
        timeout: Duration,
        filter: impl Fn(&Message) -> bool,
    ) -> Result<Message> {
        match self
            .wait_for(
                timeout,
                |e| matches!(e, Event::MessageCreate(m) if filter(m)),
            )
            .await?
        {
            Event::MessageCreate(m) => Ok(m),
            _ => unreachable!(),
        }
    }

    /// Hands an event to the handler (in its own task) and to the subscribers.
    fn emit(&self, event: Event) {
        if self.inner.events.receiver_count() > 0 {
            let _ = self.inner.events.send(event.clone());
        }
        let Some(handler) = self.inner.handler.clone() else {
            return;
        };
        let client = self.clone();
        tokio::spawn(async move {
            let is_error = matches!(event, Event::Error(_));
            let run = AssertUnwindSafe(events::call(handler.as_ref(), client.clone(), event))
                .catch_unwind();
            let err = match run.await {
                Ok(Ok(())) => return,
                Ok(Err(e)) => Error::Handler(e),
                Err(panic) => {
                    let what = panic
                        .downcast_ref::<&str>()
                        .map(|s| s.to_string())
                        .or_else(|| panic.downcast_ref::<String>().cloned())
                        .unwrap_or_else(|| "panic".into());
                    Error::Handler(format!("panic: {what}").into())
                }
            };
            // An error in the error handler is not passed around again.
            if !is_error {
                client.emit(Event::Error(Arc::new(err)));
            }
        });
    }

    // ---------- shortcuts ----------

    /// Sends a message to a channel; files are uploaded first.
    pub async fn send(&self, channel_id: &str, message: impl Into<SendOptions>) -> Result<Message> {
        let d = self.inner.rest.send(channel_id, message).await?;
        Ok(Message::new(self.clone(), d))
    }

    /// Sends someone a direct message.
    pub async fn send_dm(&self, user_id: &str, message: impl Into<SendOptions>) -> Result<Message> {
        let dm = self.inner.rest.dm(user_id).await?;
        let id = dm.id.clone();
        self.cache_mut().channels.insert(id.clone(), dm);
        self.send(&id, message).await
    }

    /// Replaces all of the bot's "/" commands (they show up in the apps right away).
    pub async fn set_commands(&self, commands: &[Command]) -> Result<Vec<Command>> {
        self.inner.rest.set_commands(commands).await
    }

    /// What someone may do in a channel, the server's way: @everyone and their roles, then the
    /// channel's overwrites. Owners and admins may do everything. 0 when unknown.
    pub fn permissions_in(&self, channel_id: &str, user_id: &str) -> u64 {
        let cache = self.cache();
        let Some(channel) = cache.channels.get(channel_id) else {
            return 0;
        };
        let Some(guild) = channel.guild_id.as_ref().and_then(|g| cache.guilds.get(g)) else {
            return 0;
        };
        compute_permissions(guild, channel, user_id)
    }

    /// Whether someone has a permission in a channel (see [`Permissions`](crate::Permissions)).
    pub fn can(&self, channel_id: &str, user_id: &str, permission: u64) -> bool {
        self.permissions_in(channel_id, user_id) & permission == permission
    }

    /// A full link to an avatar, emoji or other `/api/...` path the API hands out.
    pub fn link(&self, path: Option<&str>) -> Option<String> {
        let path = path.filter(|p| !p.is_empty())?;
        if path.starts_with("http:") || path.starts_with("https:") {
            Some(path.to_string())
        } else {
            Some(format!("{}{}", self.inner.rest.base(), path))
        }
    }

    // ---------- voice ----------

    /// The voice room the bot is in: one at a time, as for anyone.
    #[cfg(feature = "voice")]
    pub fn voice(&self) -> Option<crate::voice::VoiceConnection> {
        self.inner
            .voice
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
    }

    #[cfg(feature = "voice")]
    pub(crate) fn set_voice(&self, v: Option<crate::voice::VoiceConnection>) {
        *self.inner.voice.lock().unwrap_or_else(|e| e.into_inner()) = v;
    }

    /// Clears the voice slot if it still holds `conn`.
    #[cfg(feature = "voice")]
    pub(crate) fn clear_voice(&self, conn: &crate::voice::VoiceConnection) {
        let mut slot = self.inner.voice.lock().unwrap_or_else(|e| e.into_inner());
        if slot.as_ref().is_some_and(|v| v.same(conn)) {
            *slot = None;
        }
    }

    /// Joins a voice room to play (and optionally hear) audio. Leaves the room it was in.
    #[cfg(feature = "voice")]
    pub async fn join_voice(
        &self,
        channel_id: &str,
        opts: crate::voice::JoinVoiceOptions,
    ) -> Result<crate::voice::VoiceConnection> {
        if let Some(v) = self.voice() {
            if v.channel_id() == channel_id && !v.is_closed() {
                return Ok(v);
            }
            v.leave().await;
        }
        let conn = crate::voice::VoiceConnection::new(self.clone(), channel_id.to_string(), opts);
        self.set_voice(Some(conn.clone()));
        // LiveKit's connect future is large: run it in its own task, on the heap, so it does not
        // overflow a small (main thread) stack.
        let connecting = conn.clone();
        let result = tokio::spawn(async move { Box::pin(connecting.connect()).await })
            .await
            .unwrap_or_else(|e| Err(Error::Voice(format!("a conexão de voz falhou: {e}"))));
        if let Err(e) = result {
            self.clear_voice(&conn);
            conn.leave().await;
            return Err(e);
        }
        Ok(conn)
    }

    // ---------- cache ----------

    fn dispatch(&self, t: &str, d: Value) {
        self.emit(Event::Raw {
            kind: t.to_string(),
            data: d.clone(),
        });
        if let Err(e) = self.apply(t, d) {
            self.emit(Event::Error(Arc::new(e)));
        }
    }

    fn apply(&self, t: &str, d: Value) -> Result<()> {
        match t {
            "READY" => {
                let r: Ready = parse(t, &d)?;
                let guilds: Vec<CachedGuild> = {
                    let mut c = self.cache_mut();
                    c.user = Some(r.user.clone());
                    c.session_id = r.session_id.clone();
                    c.guilds.clear();
                    c.channels.clear();
                    c.voice_states.clear();
                    let guilds = r.guilds.into_iter().map(|g| c.add_guild(g)).collect();
                    for ch in r.dm_channels {
                        for u in ch.recipients.iter().flatten() {
                            c.users.insert(u.id.clone(), u.clone());
                        }
                        c.channels.insert(ch.id.clone(), ch);
                    }
                    for v in r.voice_states {
                        if v.channel_id.is_some() {
                            c.voice_states.insert(v.user_id.clone(), v);
                        }
                    }
                    guilds
                };
                if let Some(tx) = self
                    .inner
                    .first_ready
                    .lock()
                    .unwrap_or_else(|e| e.into_inner())
                    .take()
                {
                    let _ = tx.send(Ok(r.user.clone()));
                }
                #[cfg(feature = "voice")]
                if let Some(v) = self.voice() {
                    v.resync();
                }
                self.emit(Event::Ready(r.user));
                for g in guilds {
                    self.emit(Event::GuildCreate(g));
                }
            }
            "GUILD_CREATE" => {
                let g: GuildFull = parse(t, &d)?;
                let cached = self.cache_mut().add_guild(g);
                self.emit(Event::GuildCreate(cached));
            }
            "GUILD_UPDATE" => {
                let u: Guild = parse(t, &d)?;
                let g = {
                    let mut c = self.cache_mut();
                    let Some(g) = c.guilds.get_mut(&u.id) else {
                        return Ok(());
                    };
                    g.name = u.name;
                    g.icon = u.icon;
                    g.owner_id = u.owner_id;
                    g.clone()
                };
                self.emit(Event::GuildUpdate(g));
            }
            "GUILD_DELETE" => {
                let id = d["id"].as_str().unwrap_or_default().to_string();
                self.cache_mut().drop_guild(&id);
                self.emit(Event::GuildDelete(id));
            }
            "GUILD_ROLE_CREATE" | "GUILD_ROLE_UPDATE" => {
                let r: Role = parse(t, &d)?;
                if let Some(g) = self.cache_mut().guilds.get_mut(&r.guild_id) {
                    g.roles.insert(r.id.clone(), r);
                }
            }
            "GUILD_ROLE_DELETE" => {
                let gid = d["guild_id"].as_str().unwrap_or_default();
                let rid = d["role_id"]
                    .as_str()
                    .or(d["id"].as_str())
                    .unwrap_or_default();
                if let Some(g) = self.cache_mut().guilds.get_mut(gid) {
                    g.roles.remove(rid);
                }
            }
            "GUILD_EMOJIS_UPDATE" => {
                let gid = d["guild_id"].as_str().unwrap_or_default().to_string();
                let emojis: Vec<Emoji> = parse(t, &d["emojis"])?;
                if let Some(g) = self.cache_mut().guilds.get_mut(&gid) {
                    g.emojis = emojis;
                }
            }
            "CHANNEL_CREATE" | "CHANNEL_UPDATE" => {
                let mut ch: Channel = parse(t, &d)?;
                let previous = {
                    let mut c = self.cache_mut();
                    let previous = c.channels.get(&ch.id).cloned();
                    if ch.recipients.is_none() {
                        if let Some(r) = previous.as_ref().and_then(|p| p.recipients.clone()) {
                            ch.recipients = Some(r);
                        }
                    }
                    c.channels.insert(ch.id.clone(), ch.clone());
                    if let Some(gid) = &ch.guild_id {
                        if let Some(g) = c.guilds.get_mut(gid) {
                            g.channels.insert(ch.id.clone(), ch.clone());
                        }
                    }
                    for u in ch.recipients.iter().flatten() {
                        c.users.insert(u.id.clone(), u.clone());
                    }
                    previous
                };
                if t == "CHANNEL_CREATE" {
                    self.emit(Event::ChannelCreate(ch));
                } else {
                    #[cfg(feature = "voice")]
                    if let Some(v) = self.voice() {
                        v.on_channel_update(&ch);
                    }
                    self.emit(Event::ChannelUpdate {
                        channel: ch,
                        previous,
                    });
                }
            }
            "CHANNEL_DELETE" => {
                let ch: Channel = parse(t, &d)?;
                {
                    let mut c = self.cache_mut();
                    c.channels.remove(&ch.id);
                    if let Some(g) = ch.guild_id.as_ref().and_then(|g| c.guilds.get_mut(g)) {
                        g.channels.remove(&ch.id);
                    }
                }
                self.emit(Event::ChannelDelete(ch));
            }
            "GUILD_MEMBER_ADD" | "GUILD_MEMBER_UPDATE" => {
                let m: Member = parse(t, &d)?;
                let previous = {
                    let mut c = self.cache_mut();
                    let previous = c
                        .guilds
                        .get_mut(&m.guild_id)
                        .and_then(|g| g.members.insert(m.user.id.clone(), m.clone()));
                    c.users.insert(m.user.id.clone(), m.user.clone());
                    previous
                };
                if t == "GUILD_MEMBER_ADD" {
                    self.emit(Event::MemberAdd(m));
                } else {
                    self.emit(Event::MemberUpdate {
                        member: m,
                        previous,
                    });
                }
            }
            "GUILD_MEMBER_REMOVE" => {
                let e: MemberRemoveEvent = parse(t, &d)?;
                if let Some(g) = self.cache_mut().guilds.get_mut(&e.guild_id) {
                    g.members.remove(&e.user_id);
                }
                self.emit(Event::MemberRemove(e));
            }
            "PRESENCE_UPDATE" => {
                let p: PresenceEvent = parse(t, &d)?;
                for g in self.cache_mut().guilds.values_mut() {
                    if !g.members.contains_key(&p.user_id) {
                        continue;
                    }
                    if p.status == "online" {
                        g.online.insert(p.user_id.clone());
                    } else {
                        g.online.remove(&p.user_id);
                    }
                }
                self.emit(Event::PresenceUpdate(p));
            }
            "USER_UPDATE" => {
                let u: User = parse(t, &d)?;
                let mut c = self.cache_mut();
                if c.user.as_ref().is_some_and(|me| me.id == u.id) {
                    c.user = Some(u.clone());
                }
                c.users.insert(u.id.clone(), u);
            }
            "MESSAGE_CREATE" => {
                let m: MessageData = parse(t, &d)?;
                {
                    let mut c = self.cache_mut();
                    if let Some(ch) = c.channels.get_mut(&m.channel_id) {
                        ch.last_message_id = Some(m.id.clone());
                    }
                    c.users.insert(m.author.id.clone(), m.author.clone());
                }
                self.emit(Event::MessageCreate(Message::new(self.clone(), m)));
            }
            "MESSAGE_UPDATE" => {
                let m: MessageData = parse(t, &d)?;
                self.emit(Event::MessageUpdate(Message::new(self.clone(), m)));
            }
            "MESSAGE_DELETE" => self.emit(Event::MessageDelete(parse(t, &d)?)),
            "MESSAGE_REACTION_ADD" => self.emit(Event::ReactionAdd(parse(t, &d)?)),
            "MESSAGE_REACTION_REMOVE" => self.emit(Event::ReactionRemove(parse(t, &d)?)),
            "TYPING_START" => self.emit(Event::TypingStart(parse(t, &d)?)),
            "INTERACTION_CREATE" => {
                let i: InteractionData = parse(t, &d)?;
                self.emit(Event::Interaction(Interaction::new(self.clone(), i)));
            }
            "VOICE_STATE_UPDATE" => {
                let v: VoiceState = parse(t, &d)?;
                let (previous, mine) = {
                    let mut c = self.cache_mut();
                    let previous = if v.channel_id.is_some() {
                        c.voice_states.insert(v.user_id.clone(), v.clone())
                    } else {
                        c.voice_states.remove(&v.user_id)
                    };
                    (previous, c.user.as_ref().is_some_and(|u| u.id == v.user_id))
                };
                #[cfg(feature = "voice")]
                if mine {
                    if let Some(conn) = self.voice() {
                        conn.on_own_state(&v);
                    }
                }
                let _ = mine;
                self.emit(Event::VoiceStateUpdate { state: v, previous });
            }
            _ => {}
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bits::Permissions;
    use serde_json::json;

    fn ready() -> Value {
        json!({
            "user": { "id": "1", "username": "bot", "display_name": "Bot", "avatar": null, "bot": true },
            "session_id": "s1",
            "guilds": [{
                "id": "10", "name": "Casa", "icon": null, "owner_id": "2",
                "channels": [
                    { "id": "20", "guild_id": "10", "type": 0, "name": "geral", "topic": null, "position": 0, "parent_id": null, "last_message_id": null,
                      "permission_overwrites": [{ "id": "10", "type": 0, "allow": 0, "deny": 512 }] },
                    { "id": "21", "guild_id": "10", "type": 2, "name": "voz", "topic": null, "position": 1, "parent_id": null, "last_message_id": null }
                ],
                "members": [
                    { "guild_id": "10", "user": { "id": "1", "username": "bot", "display_name": "Bot", "avatar": null, "bot": true }, "nick": null, "is_admin": false, "joined_at": 0, "roles": [] },
                    { "guild_id": "10", "user": { "id": "3", "username": "ana", "display_name": "Ana", "avatar": null, "bot": false }, "nick": null, "is_admin": false, "joined_at": 0, "roles": [] }
                ],
                "presences": ["3"],
                "roles": [{ "id": "10", "guild_id": "10", "name": "@everyone", "color": null, "position": 0, "permissions": 16384 | 512 | 2048, "hoist": false }],
                "emojis": [],
                "unknown_field": 1
            }],
            "dm_channels": [{ "id": "30", "guild_id": null, "type": 1, "name": "", "topic": null, "position": 0, "parent_id": null, "last_message_id": null,
                              "recipients": [{ "id": "4", "username": "bia", "display_name": "Bia", "avatar": null, "bot": false }] }],
            "presences": [],
            "voice_states": [{ "user_id": "3", "guild_id": "10", "channel_id": "21", "self_mute": false, "self_deaf": false, "self_stream": false, "self_video": false, "session_id": "x" }],
            "read_states": []
        })
    }

    #[tokio::test]
    async fn events_keep_the_cache() {
        let client = Client::builder("x")
            .api("http://127.0.0.1:1/api")
            .build()
            .unwrap();
        let mut rx = client.subscribe();
        client.dispatch("READY", ready());
        {
            let c = client.cache();
            assert_eq!(c.user.as_ref().unwrap().id, "1");
            assert_eq!(c.guilds["10"].channels.len(), 2);
            assert_eq!(c.channels.len(), 3);
            assert!(c.users.contains_key("4"));
            assert!(c.guilds["10"].online.contains("3"));
            assert_eq!(c.voice_states["3"].channel_id.as_deref(), Some("21"));
        }
        // The @everyone overwrite on #geral takes SEND_MESSAGES away.
        assert!(!client.can("20", "3", Permissions::SEND_MESSAGES));
        assert!(client.can("21", "3", Permissions::CONNECT));

        client.dispatch("GUILD_MEMBER_UPDATE", json!({ "guild_id": "10", "user": { "id": "3", "username": "ana", "display_name": "Ana", "avatar": null, "bot": false }, "nick": "Aninha", "is_admin": true, "joined_at": 0, "roles": [] }));
        assert!(client.can("20", "3", Permissions::SEND_MESSAGES));
        client.dispatch(
            "CHANNEL_UPDATE",
            json!({ "id": "30", "guild_id": null, "type": 1, "name": "", "position": 0 }),
        );
        assert!(
            client.channel("30").unwrap().recipients.is_some(),
            "a DM keeps its recipients"
        );
        client.dispatch(
            "VOICE_STATE_UPDATE",
            json!({ "user_id": "3", "guild_id": "10", "channel_id": null, "session_id": "x" }),
        );
        assert!(client.cache().voice_states.is_empty());
        client.dispatch("MESSAGE_CREATE", json!({ "id": "50", "channel_id": "20", "author": { "id": "3", "username": "ana", "display_name": "Ana", "avatar": null, "bot": false }, "content": "oi", "timestamp": 1 }));
        client.dispatch(
            "MESSAGE_UPDATE",
            json!({ "id": "50", "channel_id": "20", "content": "oi!", "edited_timestamp": 2 }),
        );
        client.dispatch("GUILD_DELETE", json!({ "id": "10" }));
        assert!(client.cache().guilds.is_empty());
        assert_eq!(client.cache().channels.len(), 1);

        let mut kinds = Vec::new();
        while let Ok(ev) = rx.try_recv() {
            match ev {
                Event::Raw { .. } => {}
                Event::MessageCreate(m) => {
                    assert_eq!(
                        m.guild_id().as_deref(),
                        None,
                        "the server was deleted after it"
                    );
                    assert!(!m.mine());
                    kinds.push("message_create")
                }
                Event::MessageUpdate(m) => {
                    assert_eq!((m.content.as_str(), m.edited_timestamp), ("oi!", Some(2)));
                    kinds.push("message_update")
                }
                Event::Ready(_) => kinds.push("ready"),
                Event::GuildCreate(_) => kinds.push("guild_create"),
                Event::MemberUpdate { previous, .. } => {
                    assert!(previous.is_some_and(|p| p.nick.is_none()));
                    kinds.push("member_update")
                }
                Event::ChannelUpdate { .. } => kinds.push("channel_update"),
                Event::VoiceStateUpdate { previous, .. } => {
                    assert!(previous.is_some());
                    kinds.push("voice_state_update")
                }
                Event::GuildDelete(id) => {
                    assert_eq!(id, "10");
                    kinds.push("guild_delete")
                }
                other => panic!("unexpected {other:?}"),
            }
        }
        assert_eq!(
            kinds,
            [
                "ready",
                "guild_create",
                "member_update",
                "channel_update",
                "voice_state_update",
                "message_create",
                "message_update",
                "guild_delete"
            ]
        );
    }

    #[test]
    fn gateway_url_comes_from_the_api() {
        // Built without a runtime: nothing connects until login.
        let c = Client::builder("x").build().unwrap();
        assert_eq!(c.rest().api(), "https://zynko.org/api");
        assert_eq!(
            c.link(Some("/api/media/a.png")).as_deref(),
            Some("https://zynko.org/api/media/a.png")
        );
        assert_eq!(
            c.link(Some("https://x.y/z")).as_deref(),
            Some("https://x.y/z")
        );
        assert!(Client::builder(" ").build().is_err());
    }
}