Adding play command
This commit is contained in:
@@ -1,6 +1,84 @@
|
||||
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;
|
||||
|
||||
pub mod pause;
|
||||
pub mod play;
|
||||
pub mod resume;
|
||||
pub mod skip;
|
||||
pub mod stop;
|
||||
pub mod volume;
|
||||
pub mod volume;
|
||||
|
||||
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,
|
||||
None => {
|
||||
return Err(format!("{}", "No guild ID set"));
|
||||
}
|
||||
};
|
||||
|
||||
let channel_id = match find_voice_channel(&ctx, &guild_id, &user) {
|
||||
Ok(channel) => channel,
|
||||
Err(err) => return Err(format!("{}", err))
|
||||
};
|
||||
|
||||
debug!("<{}> Joining channel {}", guild_id.0, channel_id);
|
||||
let manager = songbird::get(ctx).await.expect("Songbird Voice client placed in at initialization").clone();
|
||||
let (_handle_lock, success) = manager.join(guild_id.to_owned(), channel_id.to_owned()).await;
|
||||
match success {
|
||||
Ok(s) => Ok(s),
|
||||
Err(err) => Err(format!("{}", err))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn leave(ctx: &Context, guild_id_option: &Option<GuildId>) -> Result<(), String> {
|
||||
let guild_id = match guild_id_option {
|
||||
Some(g) => g,
|
||||
None => {
|
||||
return Err(format!("{}", "No guild ID set"));
|
||||
}
|
||||
};
|
||||
|
||||
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() {
|
||||
debug!("<{}> Disconnecting from channel", guild_id.0);
|
||||
if let Err(e) = manager.remove(songbird_guild_id).await {
|
||||
return Err(format!("{}", e))
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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,
|
||||
None => return Err(format!("Guild not found"))
|
||||
};
|
||||
|
||||
match guild.voice_states.get(&user.id).and_then(|voice_state| voice_state.channel_id) {
|
||||
Some(channel) => Ok(channel),
|
||||
None => return Err(format!("User is not in a voice channel"))
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,11 +1,91 @@
|
||||
use log::debug;
|
||||
use serenity::{model::prelude::{interaction::application_command::CommandDataOption, application_command::CommandDataOptionValue}, builder::CreateApplicationCommand};
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use log::{debug, warn, error};
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction) -> String {
|
||||
let track_option: &CommandDataOptionValue = command.data.options.get(0).expect("Expected track option").resolved.as_ref().expect("Expected track option to be resolved");
|
||||
debug!("Play command executed with track: {:?}", track_option);
|
||||
"Playing xyz".to_string()
|
||||
use serenity::prelude::*;
|
||||
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 super::{create_response, edit_response};
|
||||
|
||||
pub async fn run(ctx: &Context, command: &ApplicationCommandInteraction) {
|
||||
let track_option = 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
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing track option");
|
||||
return
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Missing track option");
|
||||
return
|
||||
}
|
||||
};
|
||||
|
||||
match create_response(&ctx, &command, format!("Playing track \"{}\"", track_option)).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);
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("{}", err);
|
||||
let _ = edit_response(&ctx, &command, "Unable to join voice channel".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
error!("Failed to create a response message: {}", why);
|
||||
let _ = edit_response(&ctx, &command, "Unable to play track".to_string());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
|
||||
Reference in New Issue
Block a user