Add option to loop the current track
This commit is contained in:
parent
6bcedfd0cd
commit
b91d3acc31
2 changed files with 53 additions and 3 deletions
|
@ -5,6 +5,7 @@ use serenity::model::interactions::application_command::{
|
||||||
};
|
};
|
||||||
use serenity::utils::{EmbedMessageBuilding, MessageBuilder};
|
use serenity::utils::{EmbedMessageBuilding, MessageBuilder};
|
||||||
use songbird::create_player;
|
use songbird::create_player;
|
||||||
|
use songbird::input::Input;
|
||||||
|
|
||||||
use crate::{CurrentVolume, CurrentlyPlayingTrack};
|
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) {
|
if let Some(handler_lock) = manager.get(guild_id) {
|
||||||
let mut handler = handler_lock.lock().await;
|
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,
|
Ok(source) => source,
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
println!("Err starting source: {:?}", 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 = {
|
let message = {
|
||||||
if let Some(title) = &source.metadata.title {
|
if let Some(title) = &source_input.metadata.title {
|
||||||
let mut msg = MessageBuilder::new();
|
let mut msg = MessageBuilder::new();
|
||||||
msg.push_line("Playing song:");
|
msg.push_line("Playing song:");
|
||||||
msg.push_named_link(title, url);
|
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;
|
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()
|
"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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -42,6 +42,7 @@ impl EventHandler for Handler {
|
||||||
"play" => commands::play(&mut ctx, &command).await,
|
"play" => commands::play(&mut ctx, &command).await,
|
||||||
"stop" => commands::stop(&ctx, &command).await,
|
"stop" => commands::stop(&ctx, &command).await,
|
||||||
"volume" => commands::set_volume(&mut ctx, &command).await,
|
"volume" => commands::set_volume(&mut ctx, &command).await,
|
||||||
|
"loop" => commands::set_loop(&mut ctx, &command).await,
|
||||||
_ => "not implemented :(".to_string(),
|
_ => "not implemented :(".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -107,6 +108,19 @@ impl EventHandler for Handler {
|
||||||
})
|
})
|
||||||
.default_permission(false)
|
.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
|
.await
|
||||||
.expect("Couldn't create commands");
|
.expect("Couldn't create commands");
|
||||||
|
|
Loading…
Add table
Reference in a new issue