Skip to content

Commit e03ef07

Browse files
committed
Use stable version of librespot, fix various bugs
1 parent 21d4bd8 commit e03ef07

File tree

5 files changed

+113
-66
lines changed

5 files changed

+113
-66
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
/cache

Cargo.lock

+39-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ authors = ["Max Isom <hi@maxisom.me>"]
55
edition = "2018"
66

77
[dependencies]
8-
# TODO: Update this to stable version when async version is released
9-
librespot = {git = "https://github.com/librespot-org/librespot", branch = "dev", default-features = false}
8+
librespot = {version = "0.2.0", default-features = false}
109
songbird = "0.1.6"
1110
tracing = "0.1"
1211
tracing-subscriber = "0.2"

src/lib/player.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ impl SpotifyPlayer {
147147

148148
let mut cache: Option<Cache> = None;
149149

150-
if let Ok(c) = Cache::new(cache_dir.clone(), cache_dir) {
150+
// 4 GB
151+
let mut cache_limit: u64 = 10;
152+
cache_limit = cache_limit.pow(9);
153+
cache_limit *= 4;
154+
155+
if let Ok(c) = Cache::new(cache_dir.clone(), cache_dir, Some(cache_limit)) {
151156
cache = Some(c);
152157
}
153158

@@ -218,13 +223,14 @@ impl SpotifyPlayer {
218223
self.spirc = Some(Box::new(spirc));
219224

220225
let mut channel_lock = self.event_channel.as_ref().unwrap().lock().await;
221-
222226
*channel_lock = player_events;
223227
}
224228

225-
pub fn disable_connect(&mut self) {
229+
pub async fn disable_connect(&mut self) {
226230
if let Some(spirc) = self.spirc.as_ref() {
227231
spirc.shutdown();
232+
233+
self.event_channel.as_ref().unwrap().lock().await.close();
228234
}
229235
}
230236
}

src/main.rs

+63-24
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use songbird::SerenityInit;
55

66
mod lib {
77
pub mod player;
8+
// pub mod forward_mpsc;
89
}
910
use lib::player::{SpotifyPlayer, SpotifyPlayerKey};
1011
use librespot::core::mercury::MercuryError;
1112
use librespot::playback::config::Bitrate;
1213
use librespot::playback::player::PlayerEvent;
1314
use std::sync::Arc;
1415
use tokio::sync::Mutex;
16+
use tokio::time::{sleep, Duration};
1517

1618
use serenity::client::Context;
1719

@@ -37,31 +39,36 @@ impl EventHandler for Handler {
3739
println!("Ready!");
3840
println!("Invite me with https://discord.com/api/oauth2/authorize?client_id={}&permissions=36700160&scope=bot", ready.user.id);
3941

42+
ctx.set_presence(None, user::OnlineStatus::Online).await;
43+
}
44+
45+
async fn cache_ready(&self, ctx: Context, guilds: Vec<id::GuildId>) {
46+
let guild_id = match guilds.first() {
47+
Some(guild_id) => *guild_id,
48+
None => {
49+
panic!("Not currently in any guilds.");
50+
}
51+
};
52+
4053
let data = ctx.data.read().await;
4154

4255
let player = data.get::<SpotifyPlayerKey>().unwrap().clone();
4356
let user_id = *data
4457
.get::<UserIdKey>()
4558
.expect("User ID placed in at initialisation.");
4659

47-
// Get guild so it's cached
48-
let _guilds = ctx.cache.current_user().await.guilds(&ctx.http).await;
49-
50-
let guild = match ctx.cache.guilds().await.first() {
51-
Some(guild_id) => match ctx.cache.guild(guild_id).await {
52-
Some(guild) => guild,
53-
None => panic!("Could not find guild."),
54-
},
55-
None => {
56-
panic!("Not currently in any guilds.");
57-
}
58-
};
59-
6060
// Handle case when user is in VC when bot starts
61+
let guild = ctx
62+
.cache
63+
.guild(guild_id)
64+
.await
65+
.expect("Could not find guild in cache.");
66+
6167
let channel_id = guild
6268
.voice_states
6369
.get(&user_id)
6470
.and_then(|voice_state| voice_state.channel_id);
71+
drop(guild);
6572

6673
if channel_id.is_some() {
6774
// Enable casting
@@ -70,7 +77,7 @@ impl EventHandler for Handler {
7077

7178
let c = ctx.clone();
7279

73-
// Spawn event channel handler for Spotify
80+
// Handle Spotify events
7481
tokio::spawn(async move {
7582
loop {
7683
let channel = player.lock().await.event_channel.clone().unwrap();
@@ -79,6 +86,8 @@ impl EventHandler for Handler {
7986
let event = match receiver.recv().await {
8087
Some(e) => e,
8188
None => {
89+
// Busy waiting bad but quick and easy
90+
sleep(Duration::from_millis(256)).await;
8291
continue;
8392
}
8493
};
@@ -92,7 +101,7 @@ impl EventHandler for Handler {
92101
.expect("Songbird Voice client placed in at initialisation.")
93102
.clone();
94103

95-
let _ = manager.remove(guild.id).await;
104+
let _ = manager.remove(guild_id).await;
96105
}
97106

98107
PlayerEvent::Started { .. } => {
@@ -101,6 +110,12 @@ impl EventHandler for Handler {
101110
.expect("Songbird Voice client placed in at initialisation.")
102111
.clone();
103112

113+
let guild = c
114+
.cache
115+
.guild(guild_id)
116+
.await
117+
.expect("Could not find guild in cache.");
118+
104119
let channel_id = match guild
105120
.voice_states
106121
.get(&user_id)
@@ -113,9 +128,9 @@ impl EventHandler for Handler {
113128
}
114129
};
115130

116-
let _handler = manager.join(guild.id, channel_id).await;
131+
let _handler = manager.join(guild_id, channel_id).await;
117132

118-
if let Some(handler_lock) = manager.get(guild.id) {
133+
if let Some(handler_lock) = manager.get(guild_id) {
119134
let mut handler = handler_lock.lock().await;
120135

121136
let mut decoder = input::codec::OpusDecoderState::new().unwrap();
@@ -194,6 +209,12 @@ impl EventHandler for Handler {
194209

195210
let player = data.get::<SpotifyPlayerKey>().unwrap();
196211

212+
let guild = ctx
213+
.cache
214+
.guild(ctx.cache.guilds().await.first().unwrap())
215+
.await
216+
.unwrap();
217+
197218
// If user just connected
198219
if old.clone().is_none() {
199220
// Enable casting
@@ -204,19 +225,37 @@ impl EventHandler for Handler {
204225
// If user disconnected
205226
if old.clone().unwrap().channel_id.is_some() && new.channel_id.is_none() {
206227
// Disable casting
207-
player.lock().await.disable_connect();
208-
return;
209-
}
228+
player.lock().await.disable_connect().await;
210229

211-
// If user moved channels
212-
if old.unwrap().channel_id.unwrap() != new.channel_id.unwrap() {
230+
// Disconnect
213231
let manager = songbird::get(&ctx)
214232
.await
215233
.expect("Songbird Voice client placed in at initialisation.")
216234
.clone();
217235

218-
if let Some(guild_id) = ctx.cache.guilds().await.first() {
219-
let _handler = manager.join(*guild_id, new.channel_id.unwrap()).await;
236+
let _handler = manager.remove(guild.id).await;
237+
238+
return;
239+
}
240+
241+
// If user moved channels
242+
if old.unwrap().channel_id.unwrap() != new.channel_id.unwrap() {
243+
let bot_id = ctx.cache.current_user_id().await;
244+
245+
let bot_channel = guild
246+
.voice_states
247+
.get(&bot_id)
248+
.and_then(|voice_state| voice_state.channel_id);
249+
250+
if Option::is_some(&bot_channel) {
251+
let manager = songbird::get(&ctx)
252+
.await
253+
.expect("Songbird Voice client placed in at initialisation.")
254+
.clone();
255+
256+
if let Some(guild_id) = ctx.cache.guilds().await.first() {
257+
let _handler = manager.join(*guild_id, new.channel_id.unwrap()).await;
258+
}
220259
}
221260

222261
return;

0 commit comments

Comments
 (0)