serenity icon indicating copy to clipboard operation
serenity copied to clipboard

voice_state_update's "old" parameter is always None

Open villuna opened this issue 3 years ago • 1 comments
trafficstars

Hi! I'm writing a bot which needs to know the new and old states of the voice channels in my server at each update, but I'm running into a problem with that. Right now I just have a basic bot setup with this EventHandler:

struct Handler;

#[async_trait]
impl EventHandler for Handler {
    async fn voice_state_update(&self, _ctx: Context, old: Option<VoiceState>, new: VoiceState) {
        println!("updated!");
        println!("old: {old:?}");
        println!("new: {new:?}");
    }
}

But whenever this event triggers, old is always None, even if it shouldn't be. I don't get this problem on version 0.10, so is it just an issue with how I've set up my intents or something? my intents are

let intents = GatewayIntents::GUILD_VOICE_STATES
        | GatewayIntents::GUILD_MESSAGES
        | GatewayIntents::MESSAGE_CONTENT
        | GatewayIntents::GUILD_MESSAGE_REACTIONS;

villuna avatar Jul 27 '22 14:07 villuna

What happens if you add the GatewayIntents::non_privileged() intent?

vncsalencar avatar Jul 28 '22 15:07 vncsalencar

Here's the function which updates the cache and returns the old voice state

https://github.com/serenity-rs/serenity/blob/27d89a62e24601babb7478c7cb13ab57c37422c2/src/cache/event.rs#L701-L719

As you can see, the current guild must be in cache.guilds. Guilds are cached from the GUILD_CREATE event (you can see that by searching for cache.guilds.insert( in the codebase). GUILD_CREATE event is part of the GUILDS gateway intent. With the intent enabled, the old parameter works fine:

Screenshot_20220910_113953

@Ulfr26 Long story short: add GatewayIntents::GUILDS

kangalio avatar Sep 10 '22 09:09 kangalio