Add option for playing in separate channel

Play sounds in another channel even if not connected.
This commit is contained in:
jude 2022-11-20 10:53:01 +00:00
parent 64e7eb4a53
commit 6307de331d
3 changed files with 33 additions and 16 deletions

View File

@ -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();
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?;
}
Ok(())
}
@ -224,6 +236,7 @@ pub async fn loop_play(
&ctx.data(),
guild,
ctx.author().id,
None,
&name,
true,
)

View File

@ -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,
)

View File

@ -104,15 +104,18 @@ pub async fn play_from_query(
data: &Data,
guild: Guild,
user_id: UserId,
channel: Option<ChannelId>,
query: &str,
loop_: bool,
) -> String {
let guild_id = guild.id;
let channel_to_join = guild
let channel_to_join = channel.or_else(|| {
guild
.voice_states
.get(&user_id)
.and_then(|voice_state| voice_state.channel_id);
.and_then(|voice_state| voice_state.channel_id)
});
match channel_to_join {
Some(user_channel) => {