Add song queue

This commit is contained in:
Alex Page 2022-01-20 23:01:32 -05:00
parent 81ea66d2e5
commit 433bce9915
3 changed files with 131 additions and 2 deletions

View file

@ -131,6 +131,109 @@ pub async fn play(ctx: &mut Context, command: &ApplicationCommandInteraction) ->
}
}
pub async fn queue(ctx: &mut Context, command: &ApplicationCommandInteraction) -> String {
let options = command
.data
.options
.get(0)
.expect("Expected url option")
.resolved
.as_ref()
.expect("Expected url object");
let url = match options {
ApplicationCommandInteractionDataOptionValue::String(url) => url,
_ => {
return "You didn't tell me what to play.".to_string();
}
};
if !url.starts_with("http") {
return "That's not a real URL. I'm onto you.".to_string();
}
let guild_id = command.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
// Try to join the caller's channel
if manager.get(guild_id).is_none() {
join(ctx, command).await;
}
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
let source = match songbird::input::Restartable::ytdl(url.clone(), false).await {
Ok(source) => source,
Err(why) => {
println!("Err starting source: {:?}", why);
return "Something went horribly wrong. Go yell at Valter.".to_string();
}
};
let source_input: Input = source.into();
let message = {
if let Some(title) = &source_input.metadata.title {
let mut msg = MessageBuilder::new();
msg.push_line(format!(
"Queueing this up at position {}:",
handler.queue().len()
));
msg.push_named_link(title, url);
msg.build()
} else {
format!(
"Queueing something up at position {}, I dunno what.",
handler.queue().len()
)
.to_string()
}
};
let (mut audio, track_handle) = create_player(source_input);
let mut data = ctx.data.write().await;
let current_track = data.get_mut::<CurrentlyPlayingTrack>().unwrap();
*current_track = Some(track_handle);
let volume = data.get::<CurrentVolume>().unwrap();
audio.set_volume(*volume);
handler.enqueue(audio);
message
} else {
"Somehow neither of us are in a voice channel to begin with.".to_string()
}
}
pub async fn skip(ctx: &Context, command: &ApplicationCommandInteraction) -> String {
let guild_id = command.guild_id.unwrap();
let manager = songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
if let Some(handler_lock) = manager.get(guild_id) {
let handler = handler_lock.lock().await;
let queue = handler.queue();
let _ = queue.skip();
format!("Yeah, I didn't like this one very much anyway. Skip! Now we're at number {} in the queue.", queue.len())
} else {
"I'm not even in a channel to begin with. Silly.".to_string()
}
}
pub async fn stop(ctx: &Context, command: &ApplicationCommandInteraction) -> String {
let guild_id = command.guild_id.unwrap();