diff --git a/Cargo.toml b/Cargo.toml index d3ec81d..918ac0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,12 +103,12 @@ default-features = false [dependencies.twilight-gateway] optional = true -version = "0.5" +version = ">=0.5, <0.7" default-features = false [dependencies.twilight-model] optional = true -version = "0.5" +version = ">=0.5, <0.7" default-features = false [dependencies.typemap_rev] diff --git a/examples/twilight/Cargo.toml b/examples/twilight/Cargo.toml index e22601a..914f381 100644 --- a/examples/twilight/Cargo.toml +++ b/examples/twilight/Cargo.toml @@ -8,14 +8,13 @@ edition = "2018" futures = "0.3" tracing = "0.1" tracing-subscriber = "0.2" -serde_json = { version = "1" } tokio = { features = ["macros", "rt-multi-thread", "sync"], version = "1" } -twilight-gateway = "0.5" -twilight-http = "0.5" -twilight-model = "0.5" -twilight-standby = "0.5" +twilight-gateway = "0.6" +twilight-http = "0.6" +twilight-model = "0.6" +twilight-standby = "0.6" [dependencies.songbird] -path = "../.." default-features = false -features = ["twilight-rustls", "gateway", "driver", "zlib-stock"] +path = "../.." +features = ["driver", "twilight-rustls", "zlib-stock"] diff --git a/examples/twilight/src/main.rs b/examples/twilight/src/main.rs index 12813c7..3e88775 100644 --- a/examples/twilight/src/main.rs +++ b/examples/twilight/src/main.rs @@ -18,7 +18,7 @@ //! THIS SOFTWARE. //! //! -//! [basic lavalink bot]: https://github.com/twilight-rs/twilight/tree/trunk/lavalink/examples/basic-lavalink-bot +//! [basic lavalink bot]: https://github.com/twilight-rs/twilight/tree/main/lavalink/examples/basic-lavalink-bot use futures::StreamExt; use songbird::{ @@ -33,12 +33,14 @@ use twilight_http::Client as HttpClient; use twilight_model::{channel::Message, gateway::payload::MessageCreate, id::GuildId}; use twilight_standby::Standby; -#[derive(Clone, Debug)] -struct State { +type State = Arc; + +#[derive(Debug)] +struct StateRef { cluster: Cluster, http: HttpClient, - trackdata: Arc>>, - songbird: Arc, + trackdata: RwLock>, + songbird: Songbird, standby: Standby, } @@ -57,47 +59,47 @@ async fn main() -> Result<(), Box> { // Initialize the tracing subscriber. tracing_subscriber::fmt::init(); - let token = env::var("DISCORD_TOKEN")?; + let (mut events, state) = { + let token = env::var("DISCORD_TOKEN")?; - let http = HttpClient::new(&token); - - let (cluster, mut events) = - Cluster::new(token, Intents::GUILD_MESSAGES | Intents::GUILD_VOICE_STATES).await?; - - let state = { - let user_id = http.current_user().await?.id; - - let shard_count = cluster.shards().len(); - let songbird = Songbird::twilight(cluster.clone(), shard_count as u64, user_id); + let http = HttpClient::new(token.clone()); + let user_id = http.current_user().exec().await?.model().await?.id; + let intents = Intents::GUILD_MESSAGES | Intents::GUILD_VOICE_STATES; + let (cluster, events) = Cluster::new(token, intents).await?; cluster.up().await; - State { - cluster, - http, - trackdata: Default::default(), - songbird, - standby: Standby::new(), - } + let songbird = Songbird::twilight(cluster.clone(), user_id); + + ( + events, + Arc::new(StateRef { + cluster, + http, + trackdata: Default::default(), + songbird, + standby: Standby::new(), + }, + )) }; - while let Some(event) = events.next().await { - state.standby.process(&event.1); - state.songbird.process(&event.1).await; + while let Some((_, event)) = events.next().await { + state.standby.process(&event); + state.songbird.process(&event).await; - if let Event::MessageCreate(msg) = event.1 { + if let Event::MessageCreate(msg) = event { if msg.guild_id.is_none() || !msg.content.starts_with('!') { continue; } match msg.content.splitn(2, ' ').next() { - Some("!join") => spawn(join(msg.0, state.clone())), - Some("!leave") => spawn(leave(msg.0, state.clone())), - Some("!pause") => spawn(pause(msg.0, state.clone())), - Some("!play") => spawn(play(msg.0, state.clone())), - Some("!seek") => spawn(seek(msg.0, state.clone())), - Some("!stop") => spawn(stop(msg.0, state.clone())), - Some("!volume") => spawn(volume(msg.0, state.clone())), + Some("!join") => spawn(join(msg.0, Arc::clone(&state))), + Some("!leave") => spawn(leave(msg.0, Arc::clone(&state))), + Some("!pause") => spawn(pause(msg.0, Arc::clone(&state))), + Some("!play") => spawn(play(msg.0, Arc::clone(&state))), + Some("!seek") => spawn(seek(msg.0, Arc::clone(&state))), + Some("!stop") => spawn(stop(msg.0, Arc::clone(&state))), + Some("!volume") => spawn(volume(msg.0, Arc::clone(&state))), _ => continue, } } @@ -111,6 +113,7 @@ async fn join(msg: Message, state: State) -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box { let _success = handle.pause(); false - }, + } _ => { let _success = handle.play(); true - }, + } }; let action = if paused { "Unpaused" } else { "Paused" }; @@ -258,7 +266,8 @@ async fn pause(msg: Message, state: State) -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box(cluster: Cluster, shard_count: u64, user_id: U) -> Arc + pub fn twilight(cluster: Cluster, user_id: U) -> Self where U: Into, { - Self::twilight_from_config(cluster, shard_count, user_id, Default::default()) + Self::twilight_from_config(cluster, user_id, Default::default()) } #[cfg(feature = "twilight")] @@ -102,25 +102,24 @@ impl Songbird { /// [`process`]. /// /// [`process`]: Songbird::process - pub fn twilight_from_config( - cluster: Cluster, - shard_count: u64, - user_id: U, - config: Config, - ) -> Arc + pub fn twilight_from_config(cluster: Cluster, user_id: U, config: Config) -> Self where U: Into, { - Arc::new(Self { + Self { client_data: PRwLock::new(ClientData { - shard_count, + shard_count: cluster + .config() + .shard_scheme() + .total() + .unwrap_or_else(|| cluster.shards().len() as u64), initialised: true, user_id: user_id.into(), }), calls: Default::default(), sharder: Sharder::Twilight(cluster), config: Some(config).into(), - }) + } } /// Set the bot's user, and the number of shards in use.