From 70c249787c425c1a096091cef9380a86976db6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Czy=C5=BC?= Date: Wed, 7 Feb 2024 23:36:46 +0100 Subject: [PATCH] 0.2.0 player update --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/music/deafen.rs | 10 +++++++- src/commands/music/join.rs | 13 +++-------- src/commands/music/loop.rs | 0 src/commands/music/mod.rs | 2 -- src/commands/music/mute.rs | 12 ++++++++-- src/commands/music/pause.rs | 0 src/commands/music/play.rs | 30 +++++++++++------------- src/commands/music/queue.rs | 42 +++++++++++++++++++++++++++++++--- src/commands/music/resume.rs | 0 src/commands/music/skip.rs | 34 ++++++++++++++++++++++++--- src/commands/music/stop.rs | 7 +++--- src/commands/music/undeafen.rs | 38 ------------------------------ src/commands/music/unmute.rs | 38 ------------------------------ src/main.rs | 4 +--- 16 files changed, 112 insertions(+), 122 deletions(-) create mode 100644 src/commands/music/loop.rs create mode 100644 src/commands/music/pause.rs create mode 100644 src/commands/music/resume.rs delete mode 100644 src/commands/music/undeafen.rs delete mode 100644 src/commands/music/unmute.rs diff --git a/Cargo.lock b/Cargo.lock index 8a11b4c..01c68a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -892,7 +892,7 @@ dependencies = [ [[package]] name = "lyra" -version = "0.1.0" +version = "0.2.0" dependencies = [ "dotenv", "openssl", diff --git a/Cargo.toml b/Cargo.toml index 6e0992c..3e7b1c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lyra" -version = "0.1.0" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/src/commands/music/deafen.rs b/src/commands/music/deafen.rs index 130e40d..7206521 100644 --- a/src/commands/music/deafen.rs +++ b/src/commands/music/deafen.rs @@ -27,7 +27,15 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { let mut handler = handler_lock.lock().await; if handler.is_deaf() { - check_msg(msg.channel_id.say(&ctx.http, "Already deafened").await); + if let Err(err) = handler.deafen(false).await { + check_msg( + msg.channel_id + .say(&ctx.http, format!("Failed: {:?}", err)) + .await, + ); + } + + check_msg(msg.channel_id.say(&ctx.http, "Undeafened").await); } else { if let Err(err) = handler.deafen(true).await { check_msg( diff --git a/src/commands/music/join.rs b/src/commands/music/join.rs index 18d1fb4..b8cb3f7 100644 --- a/src/commands/music/join.rs +++ b/src/commands/music/join.rs @@ -9,15 +9,8 @@ use crate::commands::{misc::check_msg, music::misc::TrackErrorNotifier}; #[command] #[only_in(guilds)] async fn join(ctx: &Context, msg: &Message) -> CommandResult { - let (guild_id, channel_id) = { - let guild = msg.guild(&ctx.cache).unwrap(); - let channel_id = guild - .voice_states - .get(&msg.author.id) - .and_then(|voice_state| voice_state.channel_id); - - (guild.id, channel_id) - }; + let guild_id = msg.guild_id.unwrap(); + let channel_id = msg.guild(&ctx.cache).unwrap().voice_states.get(&msg.author.id).and_then(|voice_state| voice_state.channel_id); let connect_to = match channel_id { Some(channel) => channel, @@ -39,4 +32,4 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult { } Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/music/loop.rs b/src/commands/music/loop.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/music/mod.rs b/src/commands/music/mod.rs index 4a2185a..31d9376 100644 --- a/src/commands/music/mod.rs +++ b/src/commands/music/mod.rs @@ -7,5 +7,3 @@ pub mod play; pub mod queue; pub mod skip; pub mod stop; -pub mod undeafen; -pub mod unmute; diff --git a/src/commands/music/mute.rs b/src/commands/music/mute.rs index f5f261d..edcf6fa 100644 --- a/src/commands/music/mute.rs +++ b/src/commands/music/mute.rs @@ -26,8 +26,16 @@ async fn mute(ctx: &Context, msg: &Message) -> CommandResult { let mut handler = handler_lock.lock().await; - if handler.is_mute() { - check_msg(msg.channel_id.say(&ctx.http, "Already muted").await); + if handler.is_mute() { + if let Err(e) = handler.mute(false).await { + check_msg( + msg.channel_id + .say(&ctx.http, format!("failed: {:?}", e)) + .await, + ); + } + + check_msg(msg.channel_id.say(&ctx.http, "Unmuted").await); } else { if let Err(err) = handler.mute(true).await { check_msg( diff --git a/src/commands/music/pause.rs b/src/commands/music/pause.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/music/play.rs b/src/commands/music/play.rs index a28538b..a80b8de 100644 --- a/src/commands/music/play.rs +++ b/src/commands/music/play.rs @@ -3,7 +3,7 @@ use serenity::framework::standard::{Args, CommandResult}; use serenity::model::prelude::*; use serenity::prelude::*; use reqwest::Client as HttpClient; -use songbird::input::YoutubeDl; +use songbird::input::{Compose, YoutubeDl}; use songbird::events::TrackEvent; use crate::commands::{misc::check_msg, music::misc::TrackErrorNotifier}; @@ -31,18 +31,11 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { } }; - let do_search = !url.starts_with("http"); + let is_search = !url.starts_with("http"); - let (guild_id, channel_id) = { - let guild = msg.guild(&ctx.cache).unwrap(); - let channel_id = guild - .voice_states - .get(&msg.author.id) - .and_then(|voice_state| voice_state.channel_id); + let guild_id = msg.guild_id.unwrap(); + let channel_id = msg.guild(&ctx.cache).unwrap().voice_states.get(&msg.author.id).and_then(|voice_state| voice_state.channel_id); - (guild.id, channel_id) - }; - let connect_to = match channel_id { Some(channel) => channel, None => { @@ -66,20 +59,23 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { if let Ok(handler_lock) = manager.join(guild_id, connect_to).await { let mut handler = handler_lock.lock().await; - + + if let Err(err) = handler.deafen(true).await {println!("Failed to deafen: {:?}", err)}; handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier); - let src = if do_search { - YoutubeDl::new_ytdl_like("yt-dlp", http_client, url) + let mut src = if is_search { + println!("ytsearch:{}", url); + YoutubeDl::new_ytdl_like("yt-dlp", http_client, format!("ytsearch:{}", args.clone().message())) } else { - YoutubeDl::new(http_client, url) + YoutubeDl::new_ytdl_like("yt-dlp", http_client, url) }; let _ = handler.enqueue_input(src.clone().into()).await; - + + let metadata = src.aux_metadata().await.unwrap(); // let _ = handler.play_input(src.clone().into()); - check_msg(msg.channel_id.say(&ctx.http, "Playing song").await); + check_msg(msg.channel_id.say(&ctx.http, format!("Playing song: {}", metadata.title.unwrap())).await); } else { check_msg( msg.channel_id diff --git a/src/commands/music/queue.rs b/src/commands/music/queue.rs index 33b9df5..9cc9c78 100644 --- a/src/commands/music/queue.rs +++ b/src/commands/music/queue.rs @@ -3,10 +3,46 @@ use serenity::framework::standard::CommandResult; use serenity::model::prelude::*; use serenity::prelude::*; -// use crate::commands::misc::check_msg; +use crate::commands::misc::check_msg; #[command] #[only_in(guilds)] -async fn queue(_ctx: &Context, _msg: &Message) -> CommandResult { +async fn queue(ctx: &Context, msg: &Message) -> CommandResult { + let guild_id = msg.guild_id.unwrap(); + + let manager = songbird::get(ctx) + .await + .expect("Client placed at init") + .clone(); + + if let Some(handler_lock) = manager.get(guild_id) { + let handler = handler_lock.lock().await; + let queue = handler.queue(); + let mut queue_res = String::from("Queue: \n"); + + for (i, song) in queue.current_queue().iter().enumerate() { + queue_res.push_str(&format!( + "{}. {} - {}\n", + i + 1, + song.uuid(), + "Artist" + // song.metadata().artist.clone().unwrap_or_else(|| String::from("Unknown")) + )); + } + + check_msg( + msg.channel_id + .say(&ctx.http, queue_res) + .await, + ); + + } else { + check_msg( + msg.channel_id + .say(&ctx.http, "Not in a voice channel!") + .await, + ); + } + Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/music/resume.rs b/src/commands/music/resume.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/music/skip.rs b/src/commands/music/skip.rs index 7b01d2c..f6efc5a 100644 --- a/src/commands/music/skip.rs +++ b/src/commands/music/skip.rs @@ -3,10 +3,38 @@ use serenity::framework::standard::CommandResult; use serenity::model::prelude::*; use serenity::prelude::*; -// use crate::commands::misc::check_msg; +use crate::commands::misc::check_msg; #[command] #[only_in(guilds)] -async fn skip(_ctx: &Context, _msg: &Message) -> CommandResult { +async fn skip(ctx: &Context, msg: &Message) -> CommandResult { + let guild_id = msg.guild_id.unwrap(); + + let manager = songbird::get(ctx) + .await + .expect("Client placed at init") + .clone(); + + if let Some(handler_lock) = manager.get(guild_id) { + let handler = handler_lock.lock().await; + let queue = handler.queue(); + let _ = queue.skip(); + + check_msg( + msg.channel_id + .say( + &ctx.http, + format!("Song skipped: {} in queue.", queue.len()), + ) + .await, + ); + } else { + check_msg( + msg.channel_id + .say(&ctx.http, "Not in a voice channel to play in") + .await, + ); + } + Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/music/stop.rs b/src/commands/music/stop.rs index 2375a54..0ccfb45 100644 --- a/src/commands/music/stop.rs +++ b/src/commands/music/stop.rs @@ -17,7 +17,8 @@ async fn stop(ctx: &Context, msg: &Message) -> CommandResult { if let Some(handler_lock) = manager.get(guild_id) { let mut handler = handler_lock.lock().await; - let _queue = handler.queue(); + let queue = handler.queue(); + queue.stop(); if let Err(e) = handler.deafen(false).await { check_msg( @@ -31,10 +32,10 @@ async fn stop(ctx: &Context, msg: &Message) -> CommandResult { } else { check_msg( msg.channel_id - .say(&ctx.http, "Not in a voice channel to undeafen in") + .say(&ctx.http, "Not in a voice channel!") .await, ); } Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/music/undeafen.rs b/src/commands/music/undeafen.rs deleted file mode 100644 index e06b650..0000000 --- a/src/commands/music/undeafen.rs +++ /dev/null @@ -1,38 +0,0 @@ -use serenity::framework::standard::macros::command; -use serenity::framework::standard::CommandResult; -use serenity::model::prelude::*; -use serenity::prelude::*; - -use crate::commands::misc::check_msg; - -#[command] -#[only_in(guilds)] -async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild_id = msg.guild_id.unwrap(); - - let manager = songbird::get(ctx) - .await - .expect("Client placed at init") - .clone(); - - if let Some(handler_lock) = manager.get(guild_id) { - let mut handler = handler_lock.lock().await; - if let Err(e) = handler.deafen(false).await { - check_msg( - msg.channel_id - .say(&ctx.http, format!("Failed: {:?}", e)) - .await, - ); - } - - check_msg(msg.channel_id.say(&ctx.http, "Undeafened").await); - } else { - check_msg( - msg.channel_id - .say(&ctx.http, "Not in a voice channel to undeafen in") - .await, - ); - } - - Ok(()) -} \ No newline at end of file diff --git a/src/commands/music/unmute.rs b/src/commands/music/unmute.rs deleted file mode 100644 index ba6ad45..0000000 --- a/src/commands/music/unmute.rs +++ /dev/null @@ -1,38 +0,0 @@ -use serenity::framework::standard::macros::command; -use serenity::framework::standard::CommandResult; -use serenity::model::prelude::*; -use serenity::prelude::*; - -use crate::commands::misc::check_msg; - -#[command] -#[only_in(guilds)] -async fn unmute(ctx: &Context, msg: &Message) -> CommandResult { - let guild_id = msg.guild_id.unwrap(); - - let manager = songbird::get(ctx) - .await - .expect("Client placed at init") - .clone(); - - if let Some(handler_lock) = manager.get(guild_id) { - let mut handler = handler_lock.lock().await; - if let Err(e) = handler.mute(false).await { - check_msg( - msg.channel_id - .say(&ctx.http, format!("Failed: {:?}", e)) - .await, - ); - } - - check_msg(msg.channel_id.say(&ctx.http, "Unmuted").await); - } else { - check_msg( - msg.channel_id - .say(&ctx.http, "Not in a voice channel to unmute in") - .await, - ); - } - - Ok(()) -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8e53c42..9b2d995 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,8 +30,6 @@ use crate::commands::music::play::*; use crate::commands::music::queue::*; use crate::commands::music::skip::*; use crate::commands::music::stop::*; -use crate::commands::music::undeafen::*; -use crate::commands::music::unmute::*; // tools use crate::commands::tools::ping::*; @@ -61,7 +59,7 @@ async fn before(_: &Context, msg: &Message, command_name: &str) -> bool { #[group] #[commands( - join, deafen, leave, mute, play, unmute, undeafen, ping, kashi, queue, stop, skip + join, deafen, leave, mute, play, ping, kashi, queue, stop, skip )] struct General;