moover_rust/src/commands/voice/spotify/player.rs

159 lines
4.2 KiB
Rust

use poise;
use serenity::{all::ChannelId, async_trait};
use songbird::events::{Event, EventContext, EventHandler as VoiceEventHandler};
use songbird::TrackEvent;
use crate::util::debug::send_error;
use crate::{types::{Context, Error}, util::utilities::get_channel_by_name};
use serenity::all::User;
#[poise::command(
slash_command,
description_localized("en-US", "Play song")
)]
pub async fn play(ctx: Context<'_>,
#[description = "Song: "]
_url: String
) -> Result<(), Error> {
ctx.reply("Done!").await?;
Ok(())
}
#[poise::command(
slash_command,
description_localized("en-US", "Headpat all your friends!")
)]
pub async fn headpat(ctx: Context<'_>,
#[description = "Who is the lucky one?"]
user: User
) -> Result<(), Error> {
let _title = "HEADPATS!";
let _desc = format!("{} headpats {}", ctx.author(), user);
// send_with_embed(ctx, "headpat", &title, &desc).await?;
ctx.reply("Done!").await?;
Ok(())
}
#[poise::command(
slash_command,
description_localized("en-US", "Connect to channel")
)]
pub async fn connect(ctx: Context<'_>,
#[autocomplete = "autocomplete_channel"]
#[description = "Voice channel name: "]
name: Option<String>
) -> Result<(), Error> {
if ctx.guild().is_none() {
ctx.reply("Can't use this outside of guild").await?;
return Ok(())
}
let channel: ChannelId;
if name.is_none() || name.clone().unwrap() == "" {
// Ugly one liner since I don't know how to do this another way yet
// TODO fix please
let Some(voice_channel) = ctx.guild().unwrap().voice_states.get(&ctx.author().id).and_then(|voice_state| voice_state.channel_id) else {
ctx.reply("You must be in a voice channel or specify explicit voice channel by name").await?;
return Ok(())
};
channel = voice_channel;
}
else {
channel = match get_channel_by_name(ctx.guild().unwrap(), name.unwrap()) {
Some(channel) => channel,
None => {
ctx.reply("Channel with this name does not exist").await?;
return Ok(());
}
};
}
let manager = songbird::get(ctx.serenity_context())
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
if let Ok(handler_lock) = manager.join(ctx.guild_id().unwrap(), channel).await {
let mut handler = handler_lock.lock().await;
handler.add_global_event(TrackEvent::Error.into(), TrackErrorNotifier);
}
ctx.reply("Done!").await?;
Ok(())
}
struct TrackErrorNotifier;
#[async_trait]
impl VoiceEventHandler for TrackErrorNotifier {
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
if let EventContext::Track(track_list) = ctx {
for (state, handle) in *track_list {
println!(
"Track {:?} encountered an error: {:?}",
handle.uuid(),
state.playing
);
}
}
None
}
}
async fn autocomplete_channel(
ctx: Context<'_>,
_partial: &str,
) -> Vec<String> {
use crate::util::utilities::get_vc_names;
match ctx.guild() {
Some(guild) => get_vc_names(guild),
None => [].to_vec()
}
}
#[poise::command(
slash_command,
description_localized("en-US", "Disconnect from voice channel")
)]
async fn disconnect(
ctx: Context<'_>
) -> Result<(), Error> {
let Some(guild_id) = ctx.guild_id() else {
ctx.reply("Can't use this outside of guild").await?;
return Ok(());
};
let manager = songbird::get(ctx.serenity_context())
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();
let has_handler = manager.get(guild_id).is_some();
if ! has_handler {
ctx.reply("I am not connected to a channel!").await?;
return Ok(())
}
match manager.remove(guild_id).await {
Ok(()) => {
ctx.reply("Disconnected").await?;
return Ok(())
}
Err(e) => {
let _ = send_error(ctx.serenity_context().http.clone(), e.to_string()).await;
return Ok(())
}
}
}