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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user