Add option to loop the current track

This commit is contained in:
Alex Page 2021-09-26 00:28:24 -04:00
parent 6bcedfd0cd
commit b91d3acc31
2 changed files with 53 additions and 3 deletions

View file

@ -5,6 +5,7 @@ use serenity::model::interactions::application_command::{
};
use serenity::utils::{EmbedMessageBuilding, MessageBuilder};
use songbird::create_player;
use songbird::input::Input;
use crate::{CurrentVolume, CurrentlyPlayingTrack};
@ -85,7 +86,7 @@ pub async fn play(ctx: &mut Context, command: &ApplicationCommandInteraction) ->
if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
let source = match songbird::ytdl(&url).await {
let source = match songbird::input::Restartable::ytdl(url.clone(), false).await {
Ok(source) => source,
Err(why) => {
println!("Err starting source: {:?}", why);
@ -94,8 +95,10 @@ pub async fn play(ctx: &mut Context, command: &ApplicationCommandInteraction) ->
}
};
let source_input: Input = source.into();
let message = {
if let Some(title) = &source.metadata.title {
if let Some(title) = &source_input.metadata.title {
let mut msg = MessageBuilder::new();
msg.push_line("Playing song:");
msg.push_named_link(title, url);
@ -105,7 +108,7 @@ pub async fn play(ctx: &mut Context, command: &ApplicationCommandInteraction) ->
}
};
let (mut audio, track_handle) = create_player(source);
let (mut audio, track_handle) = create_player(source_input);
let mut data = ctx.data.write().await;
@ -177,3 +180,36 @@ pub async fn set_volume(ctx: &mut Context, command: &ApplicationCommandInteracti
"No track is currently playing".to_string()
}
}
pub async fn set_loop(ctx: &mut Context, command: &ApplicationCommandInteraction) -> String {
let options = command
.data
.options
.get(0)
.expect("Expected loop option")
.resolved
.as_ref()
.expect("Expected loop object");
let loops = match options {
ApplicationCommandInteractionDataOptionValue::Boolean(loops) => *loops,
_ => {
return "Must provide whether or not to loop".to_string();
}
};
let data = ctx.data.write().await;
let current_track = data.get::<CurrentlyPlayingTrack>().unwrap();
if let Some(track) = current_track {
if loops {
track.enable_loop().expect("Couldn't enable looping");
return format!("Enabling looping on the current track");
} else {
track.disable_loop().expect("Couldn't disable looping");
return format!("Disabling looping on the current track");
}
} else {
"No track is currently playing".to_string()
}
}

View file

@ -42,6 +42,7 @@ impl EventHandler for Handler {
"play" => commands::play(&mut ctx, &command).await,
"stop" => commands::stop(&ctx, &command).await,
"volume" => commands::set_volume(&mut ctx, &command).await,
"loop" => commands::set_loop(&mut ctx, &command).await,
_ => "not implemented :(".to_string(),
};
@ -107,6 +108,19 @@ impl EventHandler for Handler {
})
.default_permission(false)
})
.create_application_command(|command| {
command
.name("loop")
.description("Enable or disable looping the currently playing track")
.create_option(|option| {
option
.name("loop")
.description("Whether or not the track should loop")
.kind(ApplicationCommandOptionType::Boolean)
.required(true)
})
.default_permission(false)
})
})
.await
.expect("Couldn't create commands");