serenity-rs / songbird

An async Rust library for the Discord voice API
ISC License
394 stars 116 forks source link

`CoreEvent::ClientConnect` not firing #104

Closed anden3 closed 2 years ago

anden3 commented 2 years ago

Songbird version: v0.2.0

Rust version (rustc -V): v1.56.0

Serenity/Twilight version: Serenity: v0.1.0

Description: Greetings! I am having some issues with CoreEvent::ClientConnect. It doesn't seem to fire for me, and my event handler doesn't get invoked. CoreEvent::ClientDisconnect works flawlessly however, using the same event handler.

Steps to reproduce: I first join a channel, and then request the bot to join as well. I then leave the channel, and it receives the ClientDisconnect event. However, when I rejoin again, it doesn't receive a ClientConnect event.

Code

Event handler

pub(crate) struct GlobalEvent {
    pub channel: mpsc::Sender<QueueUpdate>,
}

#[async_trait]
impl EventHandler for GlobalEvent {
    #[instrument(skip(self, ctx))]
    async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
        let update = match ctx {
            EventContext::ClientConnect(client) => {
                QueueUpdate::ClientConnected(UserId(client.user_id.0))
            }
            EventContext::ClientDisconnect(client) => {
                QueueUpdate::ClientDisconnected(UserId(client.user_id.0))
            }
            _ => {
                error!(event = ?ctx, "Unhandled event!");
                return None;
            }
        };

        if let Err(e) = self.channel.send(update).await {
            error!("{:?}", e);
        }

        None
    }
}

Events being added

let mut call = self.handler.lock().await;

call.add_global_event(
    Event::Core(CoreEvent::ClientConnect),
    GlobalEvent {
        channel: self.update_sender.clone(),
    },
);

call.add_global_event(
    Event::Core(CoreEvent::ClientDisconnect),
    GlobalEvent {
        channel: self.update_sender.clone(),
    },
);
FelixMcFelix commented 2 years ago

Apologies for the long followup. Following some talk on Discord last night about this, it seems that this message type (Opcode 12) is no longer transmitted at all to bot sessions. It has a different use for users accounts, who still send and receive these messages (i.e., on gateway version 6).

The fix is probably to deprecate this event class on current, document that events are no longer fired (and that connections should be observed on the normal bot gateway), and remove it completely on next.