Working on play command, unable to get source to work
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use log::debug;
|
||||
|
||||
use serenity::model::application::interaction::{InteractionResponseType, application_command::ApplicationCommandInteraction};
|
||||
use serenity::model::prelude::{GuildId, ChannelId};
|
||||
use serenity::model::user::User;
|
||||
use serenity::prelude::*;
|
||||
use songbird::id::GuildId as SongbirdGuildId;
|
||||
use songbird::Call;
|
||||
use songbird::input::{Restartable, Input, Metadata, error::Error as SongbirdError};
|
||||
|
||||
pub mod pause;
|
||||
pub mod play;
|
||||
@@ -13,6 +16,15 @@ pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod volume;
|
||||
|
||||
/// Joins a Discord voice channel.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - ctx - The context of the command.
|
||||
/// - guild_id_option - The guild ID of the guild to join.
|
||||
/// - user - The user that is requesting to join the voice channel.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<(), String> - Ok if the bot successfully joined the voice channel, Err if there was an error.
|
||||
pub async fn join(ctx: &Context, guild_id_option: &Option<GuildId>, user: &User) -> Result<(), String> {
|
||||
let guild_id = match guild_id_option {
|
||||
Some(g) => g,
|
||||
@@ -35,6 +47,14 @@ pub async fn join(ctx: &Context, guild_id_option: &Option<GuildId>, user: &User)
|
||||
}
|
||||
}
|
||||
|
||||
/// Leaves a Discord voice channel.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - ctx - The context of the command.
|
||||
/// - guild_id_option - The guild ID of the guild to leave.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<(), String> - Ok if the bot successfully left the voice channel, Err if there was an error.
|
||||
pub async fn leave(ctx: &Context, guild_id_option: &Option<GuildId>) -> Result<(), String> {
|
||||
let guild_id = match guild_id_option {
|
||||
Some(g) => g,
|
||||
@@ -43,20 +63,25 @@ pub async fn leave(ctx: &Context, guild_id_option: &Option<GuildId>) -> Result<(
|
||||
}
|
||||
};
|
||||
|
||||
let songbird_guild_id = SongbirdGuildId {
|
||||
0: guild_id.0
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx).await.expect("Songbird Voice client placed in at initialization").clone();
|
||||
if manager.get(songbird_guild_id).is_some() {
|
||||
if manager.get(*guild_id).is_some() {
|
||||
debug!("<{}> Disconnecting from channel", guild_id.0);
|
||||
if let Err(e) = manager.remove(songbird_guild_id).await {
|
||||
if let Err(e) = manager.remove(*guild_id).await {
|
||||
return Err(format!("{}", e))
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Finds the voice channel that the user is in.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - ctx - The context of the command.
|
||||
/// - guild_id - The guild ID of the guild to search.
|
||||
/// - user - The user to search for.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<ChannelId, String> - Ok if the user is in a voice channel, Err if the user is not in a voice channel.
|
||||
fn find_voice_channel(ctx: &Context, guild_id: &GuildId, user: &User) -> Result<ChannelId, String> {
|
||||
let guild = match guild_id.to_guild_cached(ctx.cache.to_owned()) {
|
||||
Some(g) => g,
|
||||
@@ -69,16 +94,70 @@ fn find_voice_channel(ctx: &Context, guild_id: &GuildId, user: &User) -> Result<
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a response to an interaction.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - ctx - The context of the command.
|
||||
/// - command - The command that was sent.
|
||||
/// - content - The content of the response.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<(), SerenityError> - Ok if the response was created successfully, Err if there was an error.
|
||||
pub async fn create_response(ctx: &Context, command: &ApplicationCommandInteraction, content: String) -> Result<(), SerenityError> {
|
||||
command.create_interaction_response(&ctx.http, |response: &mut serenity::builder::CreateInteractionResponse<'_>| {
|
||||
response
|
||||
.kind(InteractionResponseType::ChannelMessageWithSource)
|
||||
.interaction_response_data(|message: &mut serenity::builder::CreateInteractionResponseData<'_>| message.content(content))
|
||||
}).await
|
||||
response
|
||||
.kind(InteractionResponseType::ChannelMessageWithSource)
|
||||
.interaction_response_data(|message: &mut serenity::builder::CreateInteractionResponseData<'_>| message.content(content))
|
||||
}).await
|
||||
}
|
||||
|
||||
/// Edits a response to an interaction.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - ctx - The context of the command.
|
||||
/// - command - The command that was sent.
|
||||
/// - content - The content of the response.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<Message, SerenityError> - Ok if the response was edited successfully, Err if there was an error.
|
||||
pub async fn edit_response(ctx: &Context, command: &ApplicationCommandInteraction, content: String) -> Result<serenity::model::channel::Message, SerenityError> {
|
||||
command.edit_original_interaction_response(&ctx.http, |response: &mut serenity::builder::EditInteractionResponse| {
|
||||
response.content(content)
|
||||
}).await
|
||||
}
|
||||
|
||||
/// Adds a song to the queue.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - call - The call to add the song to.
|
||||
/// - url - The URL of the song to add.
|
||||
/// - lazy - Whether or not to lazy load the song.
|
||||
///
|
||||
/// # Returns
|
||||
/// Result<Metadata, SongbirdError> - Ok if the song was added successfully, Err if there was an error.
|
||||
pub async fn add_song(call: Arc<Mutex<Call>>, url: &str, lazy: bool) -> Result<Metadata, SongbirdError> {
|
||||
let source = if is_valid_url(url) {
|
||||
Restartable::ytdl(url.to_owned(), lazy).await?
|
||||
} else {
|
||||
Restartable::ytdl_search(url, lazy).await?
|
||||
};
|
||||
let mut handler = call.lock().await;
|
||||
let track: Input = source.into();
|
||||
let metadata = *track.metadata.clone();
|
||||
handler.enqueue_source(track);
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
/// Checks if a string is a valid URL.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - url - The string to check.
|
||||
///
|
||||
/// # Returns
|
||||
/// bool - True if the string is a valid URL, false if it is not.
|
||||
fn is_valid_url(url: &str) -> bool {
|
||||
match url.parse::<reqwest::Url>() {
|
||||
Ok(_) => return true,
|
||||
Err(_) => return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +1,91 @@
|
||||
use log::{debug, warn, error};
|
||||
|
||||
use serenity::prelude::*;
|
||||
use serenity::builder::{CreateApplicationCommand};
|
||||
use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use songbird::id::GuildId as SongbirdGuildId;
|
||||
use songbird::input;
|
||||
|
||||
use crate::commands::audio::{join, leave};
|
||||
use crate::commands::audio::{join, leave, add_song};
|
||||
|
||||
use super::{create_response, edit_response};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
let track_option = match command.data.options.get(0) {
|
||||
// Get the track url
|
||||
let track_url = match command.data.options.get(0) {
|
||||
Some(t) => match &t.value {
|
||||
Some(v) => match v.as_str() {
|
||||
Some(s) => s.to_owned(),
|
||||
None => {
|
||||
warn!("Missing track option");
|
||||
return
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Track option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing track option");
|
||||
return
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Track option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing track option");
|
||||
return
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Track option is missing")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
match create_response(&ctx, &command, format!("Playing track \"{}\"", track_option)).await {
|
||||
// Create the initial response
|
||||
if let Err(why) = create_response(&ctx, &command, format!("Processing command...")).await {
|
||||
error!("Failed to create response message: {}", why);
|
||||
return;
|
||||
}
|
||||
|
||||
match join(&ctx, &command.guild_id, &command.user).await {
|
||||
Ok(_) => {
|
||||
match join(&ctx, &command.guild_id, &command.user).await {
|
||||
Ok(_) => {
|
||||
let guild_id = match command.guild_id {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
let _ = edit_response(&ctx, &command, "Unable to join voice channel".to_string());
|
||||
return;
|
||||
}
|
||||
};
|
||||
debug!("Play command executed with track: {:?}", track_option);
|
||||
|
||||
let songbird_guild_id = SongbirdGuildId {
|
||||
0: guild_id.0
|
||||
};
|
||||
let manager = songbird::get(ctx).await.expect("Songbird Voice client placed in at initialization").clone();
|
||||
if let Some(handler_lock) = manager.get(songbird_guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
let source: input::Input;
|
||||
if track_option.starts_with("http") {
|
||||
// Play remote track
|
||||
source = match input::ytdl(&track_option).await {
|
||||
Ok(source) => source,
|
||||
Err(why) => {
|
||||
warn!("Unable to get source: {}", why);
|
||||
let _ = leave(&ctx, &command.guild_id).await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
} else if track_option.starts_with("#") {
|
||||
// Play tracks based on tag
|
||||
let _ = leave(&ctx, &command.guild_id).await;
|
||||
return;
|
||||
} else {
|
||||
// Play local track
|
||||
let _ = leave(&ctx, &command.guild_id).await;
|
||||
return;
|
||||
};
|
||||
|
||||
let _song = handler.play_source(source);
|
||||
let guild_id = match command.guild_id {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
if let Err(why) = edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
let _ = edit_response(&ctx, &command, "Unable to join voice channel".to_string());
|
||||
return;
|
||||
}
|
||||
};
|
||||
debug!("Play command executed with track: {:?}", track_url);
|
||||
|
||||
let manager = songbird::get(ctx).await.expect("Songbird Voice client placed in at initialization").clone();
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
match add_song(handler_lock, &track_url, true).await {
|
||||
Ok(added_song) => {
|
||||
let track_title = added_song.title.unwrap();
|
||||
debug!("Added song: {}", track_title);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Added song to queue: {}", track_title)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
warn!("Failed to add song: {}", why);
|
||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to add song: {}", why)).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
if let Err(why) = leave(&ctx, &command.guild_id).await {
|
||||
error!("Failed to leave voice channel: {}", why);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
if let Err(why) = edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await {
|
||||
error!("Failed to edit response message: {}", why);
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
error!("Failed to create a response message: {}", why);
|
||||
let _ = edit_response(&ctx, &command, "Unable to play track".to_string());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user