diff --git a/src/cmds/play.rs b/src/cmds/play.rs index 4eb03b3..86e1297 100644 --- a/src/cmds/play.rs +++ b/src/cmds/play.rs @@ -1,4 +1,7 @@ -use poise::serenity::{builder::CreateActionRow, model::application::component::ButtonStyle}; +use poise::{ + serenity::{builder::CreateActionRow, model::application::component::ButtonStyle}, + serenity_prelude::GuildChannel, +}; use crate::{ cmds::autocomplete_sound, @@ -14,23 +17,32 @@ pub async fn play( #[description = "Name or ID of sound to play"] #[autocomplete = "autocomplete_sound"] name: String, + #[description = "Channel to play in (default: your current voice channel)"] channel: Option< + GuildChannel, + >, ) -> Result<(), Error> { ctx.defer().await?; let guild = ctx.guild().unwrap(); - ctx.say( - play_from_query( - &ctx.discord(), - &ctx.data(), - guild, - ctx.author().id, - &name, - false, + if channel.as_ref().map_or(false, |c| c.is_text_based()) { + ctx.say("The channel specified is not a voice channel.") + .await?; + } else { + ctx.say( + play_from_query( + &ctx.discord(), + &ctx.data(), + guild, + ctx.author().id, + channel.map(|c| c.id), + &name, + false, + ) + .await, ) - .await, - ) - .await?; + .await?; + } Ok(()) } @@ -224,6 +236,7 @@ pub async fn loop_play( &ctx.data(), guild, ctx.author().id, + None, &name, true, ) diff --git a/src/event_handlers.rs b/src/event_handlers.rs index 4622e4b..6a0490a 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -142,6 +142,7 @@ SELECT name, id, public, server_id, uploader_id &data, guild_id.to_guild_cached(&ctx).unwrap(), component.user.id, + None, &component.data.custom_id, false, ) diff --git a/src/utils.rs b/src/utils.rs index 9593619..4b62d57 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -104,15 +104,18 @@ pub async fn play_from_query( data: &Data, guild: Guild, user_id: UserId, + channel: Option, query: &str, loop_: bool, ) -> String { let guild_id = guild.id; - let channel_to_join = guild - .voice_states - .get(&user_id) - .and_then(|voice_state| voice_state.channel_id); + let channel_to_join = channel.or_else(|| { + guild + .voice_states + .get(&user_id) + .and_then(|voice_state| voice_state.channel_id) + }); match channel_to_join { Some(user_channel) => {