From b91d3acc3147f62160058ee41bd72c6c9c5e25ba Mon Sep 17 00:00:00 2001 From: Alex Page Date: Sun, 26 Sep 2021 00:28:24 -0400 Subject: [PATCH] Add option to loop the current track --- src/commands.rs | 42 +++++++++++++++++++++++++++++++++++++++--- src/main.rs | 14 ++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 1aab5af..5bae010 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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::().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() + } +} + diff --git a/src/main.rs b/src/main.rs index c3e8903..8bdd942 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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");