Serenity 0.10.9中voice_state_update方法参数与文档不符导致编译错误的原因咨询
voice_state_update trait method parameter count mismatch despite docs showing 5 parameters Problem Description
I'm developing a Discord bot with Serenity 0.10.9, and I'm hitting a compilation error when trying to implement the voice_state_update method from the EventHandler trait. Here's my code:
use std::env; use serenity::{ async_trait, model::{channel::Message, gateway::Ready}, prelude::*, utils::MessageBuilder, }; struct Handler; use serenity::model::id::GuildId; use serenity::model::voice::VoiceState; #[async_trait] impl EventHandler for Handler { async fn voice_state_update(&self, context: Context, arg2 : Option<GuildId>, old : Option<VoiceState>, new : VoiceState) { // My intended logic here } } #[tokio::main] async fn main() { let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let mut client = Client::builder(&token).event_handler(Handler).await.expect("Err creating client"); if let Err(why) = client.start().await { println!("Client error: {:?}", why); } }
When compiling, I get this error:
error[E0050]: method voice_state_update has 5 parameters but the declaration in trait voice_state_update has 4 --> src/main.rs:18:33 | 18 | async fn voice_state_update(&self, context: Context, arg2 : Option, old : Option, new : VoiceState) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 4 parameters, found 5
This doesn't make sense to me because when I checked the source code docs for Serenity 0.10.9 (both on docs.rs and GitHub), the method is defined with 5 parameters including the old: Option<VoiceState> argument. But when I remove the old parameter, the code compiles successfully. My Cargo.lock confirms I'm using Serenity 0.10.9:
[[package]] name = "serenity" version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6275d443266aedf2be507a245ddc23db0c07b1b99774e16f3c879e96a78b067a"
Why is this happening?
Solution
The key issue here is feature-gated API differences in Serenity 0.10.9. Let me break this down:
Default trait signature (no extra features)
In the default configuration (without enabling optional features), Serenity 0.10.9'sEventHandler::voice_state_updateonly has 4 parameters:async fn voice_state_update(&self, ctx: Context, guild_id: Option<GuildId>, new: VoiceState)This is the signature your compiler is using because you haven't enabled the feature that adds the
oldparameter.Why you saw 5 parameters in the source
The GitHub commit and docs.rs source you looked at likely had theunstablefeature enabled. In Serenity 0.10.x, theold: Option<VoiceState>parameter was hidden behind theunstablefeature flag (which exposes experimental or upcoming API changes). If you view the source without that feature enabled, theoldparameter isn't part of the trait method.How to fix it
If you need access to the old voice state, explicitly enable theunstablefeature in yourCargo.toml:serenity = { version = "0.10.9", features = ["unstable"] }Once you add this, your original 5-parameter method signature will compile correctly.
Alternatively, if you don't need the old state, just stick with the 4-parameter version that works without extra features.
内容的提问来源于stack exchange,提问作者Chris Stryczynski




