Rustendo/src/commands/play.rs

104 lines
3.1 KiB
Rust

use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, Context};
use serenity::builder::{CreateCommand, CreateCommandOption, CreateEmbed};
use serenity::model::application::CommandOptionType;
use crate::util::embed::Embed;
use crate::util::user_util::{self, get_guild};
pub async fn run(ctx: &Context, command: &CommandInteraction) -> CreateEmbed {
let username = command.user.name.as_str();
let options = &command.data.options;
let query = command.data.options.first().and_then(|option| {
if let CommandDataOptionValue::String(query) = &option.value {
Some(query)
} else {
None
}
});
if query.is_none() {
return Embed::create(username, "Error 400", "There is no query provided");
}
let guild_id = match &command.guild_id {
Some(guild_id) => guild_id,
None => {
return Embed::create(username, "GuildId not found", "Could not find guild id.");
}
};
let (guild_id, channel_id) = {
let guild = get_guild(ctx, guild_id);
match guild {
Some(guild) => {
let channel_id = guild
.voice_states
.get(&command.user.id)
.and_then(|voice_state| voice_state.channel_id);
(guild.id, channel_id)
}
None => {
return Embed::create(username, "Guild not found", "Could not find guild.");
}
}
};
let connect_to = match channel_id {
Some(channel) => channel,
None => {
return Embed::create(
username,
"You are not in a VC.",
"Join one to start playing music.",
);
}
};
let manager = &songbird::get(ctx)
.await
.expect("Cannot get Songbird.")
.clone();
if !user_util::is_self_connected_to_vc(ctx, &guild_id) {
// self is connected to vc, check if user is in same vc
let self_channel = user_util::get_self_vc_id(ctx, &guild_id);
if self_channel.is_none() {
return Embed::create(
username,
"I am not in a VC.",
"Connect me to a VC to start playing music.",
);
}
let self_channel = self_channel.unwrap();
// Check if user is in the same VC as the bot
if self_channel != connect_to {
return Embed::create(
username,
"You are not in my VC.",
"Connect to my VC to control the music.",
);
}
// Connect to VC
manager
.join(guild_id, connect_to)
.await
.expect("Cannot connect>...");
}
Embed::create(username, "Searching...", format!("Looking for {}", query))
}
pub fn register() -> CreateCommand {
CreateCommand::new("play")
.description("Play music")
.add_option(
CreateCommandOption::new(CommandOptionType::String, "query", "Link or search term")
.required(true),
)
}