diff --git a/src/commands/music/leave.rs b/src/commands/music/leave.rs index 952ecdb..5c319a8 100644 --- a/src/commands/music/leave.rs +++ b/src/commands/music/leave.rs @@ -12,7 +12,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { let manager = songbird::get(ctx) .await - .expect("Client placed in at init.") + .expect("Client placed in at init") .clone(); let has_handler = manager.get(guild_id).is_some(); diff --git a/src/commands/music/mod.rs b/src/commands/music/mod.rs index ae89599..73e0309 100644 --- a/src/commands/music/mod.rs +++ b/src/commands/music/mod.rs @@ -1,4 +1,7 @@ pub mod deafen; pub mod join; pub mod leave; -pub mod mute; \ No newline at end of file +pub mod mute; +pub mod play; +pub mod undeafen; +pub mod unmute; diff --git a/src/commands/music/play.rs b/src/commands/music/play.rs index e69de29..af8e02e 100644 --- a/src/commands/music/play.rs +++ b/src/commands/music/play.rs @@ -0,0 +1,78 @@ +use serenity::framework::standard::Args; +use serenity::framework::standard::{ + macros::command, + CommandResult, +}; + +use reqwest::Client as HttpClient; + +use serenity::client::Context; + +use serenity::prelude::TypeMapKey; + +use serenity::model::prelude::*; + +use songbird::input::YoutubeDl; + +use crate::commands::misc::check_msg; + +pub struct HttpKey; + +impl TypeMapKey for HttpKey { + type Value = HttpClient; +} + +#[command] +#[only_in(guilds)] +async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { + let url = match args.single::() { + Ok(url) => url, + Err(_) => { + check_msg( + msg.channel_id + .say(&ctx.http, "Must provide a URL to a video or audio") + .await, + ); + + return Ok(()); + } + }; + + let do_search = !url.starts_with("http"); + + let guild_id = msg.guild_id.unwrap(); + + let http_client = { + let data = ctx.data.read().await; + data.get::() + .cloned() + .expect("Guaranteed to exist in the typemap.") + }; + + 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; + + let src = if do_search { + YoutubeDl::new(http_client, url) + } else { + YoutubeDl::new(http_client, url) + }; + + let _ = handler.play_input(src.clone().into()); + + check_msg(msg.channel_id.say(&ctx.http, "Playing song").await); + } else { + check_msg( + msg.channel_id + .say(&ctx.http, "Not in a voice channel!") + .await, + ); + } + + Ok(()) +} diff --git a/src/commands/music/undeafen.rs b/src/commands/music/undeafen.rs new file mode 100644 index 0000000..e06b650 --- /dev/null +++ b/src/commands/music/undeafen.rs @@ -0,0 +1,38 @@ +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 new file mode 100644 index 0000000..ba6ad45 --- /dev/null +++ b/src/commands/music/unmute.rs @@ -0,0 +1,38 @@ +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 95f7df4..431fd00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use serenity::{ StandardFramework, }, model::gateway::Ready, - prelude::{GatewayIntents, TypeMapKey}, + prelude::GatewayIntents, }; mod commands; @@ -22,6 +22,9 @@ use crate::commands::music::deafen::*; use crate::commands::music::join::*; use crate::commands::music::leave::*; use crate::commands::music::mute::*; +use crate::commands::music::play::*; +use crate::commands::music::undeafen::*; +use crate::commands::music::unmute::*; // tools use crate::commands::tools::ping::*; @@ -29,12 +32,6 @@ use crate::commands::tools::ping::*; // kashi use crate::commands::kashi::kashi::*; -struct HttpKey; - -impl TypeMapKey for HttpKey { - type Value = HttpClient; -} - struct Handler; #[async_trait] @@ -45,11 +42,7 @@ impl EventHandler for Handler { } #[group] -#[commands( - join, deafen, leave, mute, - ping, - kashi -)] +#[commands(join, deafen, leave, mute, play, unmute, undeafen, ping, kashi)] struct General; #[tokio::main]