Finish upgrading pose and songbird

This commit is contained in:
Alex Page 2024-01-25 17:50:05 -05:00
parent 4244469610
commit ec66822602
6 changed files with 3221 additions and 125 deletions

View file

@ -1,21 +1,20 @@
use anyhow::{Context, Result};
use poise::serenity_prelude::{CreateEmbed, EmbedMessageBuilding, MessageBuilder};
use poise::{
serenity_prelude::{CreateEmbed, EmbedMessageBuilding, MessageBuilder},
CreateReply,
};
use songbird::input::{Input, YoutubeDl};
use tracing::{debug, log::warn};
use crate::{personality, CommandContext, Error};
#[poise::command(slash_command)]
pub async fn join(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
let Some(Some(channel_id)) = guild
.voice_states
.get(&ctx.author().id)
.map(|vs| vs.channel_id)
else {
let Some((guild_id, Some(channel_id))) = ctx.guild().and_then(|g| {
g.voice_states
.get(&ctx.author().id)
.map(|vs| (g.id, vs.channel_id))
}) else {
ctx.say("You're not in a voice channel, silly.").await?;
return Ok(());
};
@ -24,7 +23,7 @@ pub async fn join(ctx: CommandContext<'_>) -> Result<(), Error> {
.await
.context("Expected a songbird manager")?
.clone();
let _handler = manager.join(guild.id, channel_id).await;
let _handler = manager.join(guild_id, channel_id).await;
ctx.say("Joining your channel!").await?;
@ -33,7 +32,7 @@ pub async fn join(ctx: CommandContext<'_>) -> Result<(), Error> {
#[poise::command(slash_command)]
pub async fn leave(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -43,12 +42,12 @@ pub async fn leave(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_none() {
if manager.get(guild_id).is_none() {
ctx.say("I'm not even in a voice channel!").await?;
return Ok(());
}
let _handler = manager.remove(guild.id).await;
let _handler = manager.remove(guild_id).await;
ctx.say("Okay bye!").await?;
@ -65,7 +64,7 @@ pub async fn play(
return Ok(());
};
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -75,20 +74,19 @@ pub async fn play(
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_none() {
let Some(Some(channel_id)) = guild
.voice_states
.get(&ctx.author().id)
.map(|vs| vs.channel_id)
else {
ctx.say("Neither of us are in a voice channel, silly.")
.await?;
if manager.get(guild_id).is_none() {
let Some((guild_id, Some(channel_id))) = ctx.guild().and_then(|g| {
g.voice_states
.get(&ctx.author().id)
.map(|vs| (g.id, vs.channel_id))
}) else {
ctx.say("You're not in a voice channel, silly.").await?;
return Ok(());
};
let _handler = manager.join(guild.id, channel_id).await;
let _handler = manager.join(guild_id, channel_id).await;
}
if let Some(handler_lock) = manager.get(guild.id) {
if let Some(handler_lock) = manager.get(guild_id) {
let reply = poise::CreateReply::default()
.content(personality::get_random_loading_message())
.embed(
@ -100,33 +98,11 @@ pub async fn play(
let mut handler = handler_lock.lock().await;
debug!("Trying to play: {}", url);
let source = songbird::ytdl(&url).await;
let mut source: Input = YoutubeDl::new(ctx.data().http_client.clone(), url.clone()).into();
let metadata = source.aux_metadata().await?;
let source = match source {
Ok(source) => source,
Err(e) => {
match e {
songbird::input::error::Error::Json {
ref error,
ref parsed_text,
} => {
debug!("Failed to play: {}", error);
debug!("Parsed text: {}", parsed_text);
}
_ => {
debug!("Failed to play: {}", e);
}
}
return Err(Box::new(e));
}
};
debug!("Playing: {:?}", source.metadata);
let title = source
.metadata
.title
.clone()
.unwrap_or(String::from("This video"));
debug!("Playing: {:?}", metadata);
let title = metadata.title.clone().unwrap_or(String::from("This video"));
let mut msg = MessageBuilder::new();
@ -142,13 +118,15 @@ pub async fn play(
msg.push_bold("Now playing: ").push_named_link(title, url);
response.edit(ctx, |r| r.content(msg.build())).await?;
response
.edit(ctx, CreateReply::default().content(msg.build()))
.await?;
let mut queue = ctx.data().queue.lock();
if !queue.is_empty() {
let _ = queue.stop();
}
queue.add_next(source, &mut handler);
queue.add_next(source, &mut handler)?;
queue.resume()?;
} else {
ctx.say("Neither of us are in a voice channel, silly.")
@ -168,7 +146,7 @@ pub async fn queue(
return Ok(());
};
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -178,20 +156,19 @@ pub async fn queue(
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_none() {
let Some(Some(channel_id)) = guild
.voice_states
.get(&ctx.author().id)
.map(|vs| vs.channel_id)
else {
ctx.say("Neither of us are in a voice channel, silly.")
.await?;
if manager.get(guild_id).is_none() {
let Some((guild_id, Some(channel_id))) = ctx.guild().and_then(|g| {
g.voice_states
.get(&ctx.author().id)
.map(|vs| (g.id, vs.channel_id))
}) else {
ctx.say("You're not in a voice channel, silly.").await?;
return Ok(());
};
let _handler = manager.join(guild.id, channel_id).await;
let _handler = manager.join(guild_id, channel_id).await;
}
if let Some(handler_lock) = manager.get(guild.id) {
if let Some(handler_lock) = manager.get(guild_id) {
let reply = poise::CreateReply::default()
.content(personality::get_random_loading_message())
.embed(
@ -203,13 +180,11 @@ pub async fn queue(
let mut handler = handler_lock.lock().await;
debug!("Trying to play: {}", url);
let source = songbird::ytdl(&url).await?;
debug!("Playing: {:?}", source.metadata);
let title = source
.metadata
.title
.clone()
.unwrap_or(String::from("This video"));
let mut source: Input = YoutubeDl::new(ctx.data().http_client.clone(), url.clone()).into();
let metadata = source.aux_metadata().await?;
debug!("Playing: {:?}", metadata);
let title = metadata.title.clone().unwrap_or(String::from("This video"));
let mut msg = MessageBuilder::new();
@ -225,10 +200,12 @@ pub async fn queue(
msg.push_bold("Queued: ").push_named_link(title, url);
response.edit(ctx, |r| r.content(msg.build())).await?;
response
.edit(ctx, CreateReply::default().content(msg.build()))
.await?;
let mut queue = ctx.data().queue.lock();
queue.add_to_end(source, &mut handler);
queue.add_to_end(source, &mut handler)?;
} else {
ctx.say("Neither of us are in a voice channel, silly.")
.await?;
@ -239,7 +216,7 @@ pub async fn queue(
#[poise::command(slash_command)]
pub async fn stop(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -249,7 +226,7 @@ pub async fn stop(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_some() {
if manager.get(guild_id).is_some() {
{
let mut queue = ctx.data().queue.lock();
queue.stop()?;
@ -265,7 +242,7 @@ pub async fn stop(ctx: CommandContext<'_>) -> Result<(), Error> {
#[poise::command(slash_command)]
pub async fn skip(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -275,7 +252,7 @@ pub async fn skip(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_some() {
if manager.get(guild_id).is_some() {
{
let mut queue = ctx.data().queue.lock();
let _ = queue.stop();
@ -292,7 +269,7 @@ pub async fn skip(ctx: CommandContext<'_>) -> Result<(), Error> {
#[poise::command(slash_command)]
pub async fn pause(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -302,7 +279,7 @@ pub async fn pause(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_some() {
if manager.get(guild_id).is_some() {
{
let mut queue = ctx.data().queue.lock();
queue.pause()?;
@ -318,7 +295,7 @@ pub async fn pause(ctx: CommandContext<'_>) -> Result<(), Error> {
#[poise::command(slash_command)]
pub async fn resume(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -328,7 +305,7 @@ pub async fn resume(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_some() {
if manager.get(guild_id).is_some() {
{
let mut queue = ctx.data().queue.lock();
queue.resume()?;
@ -344,7 +321,7 @@ pub async fn resume(ctx: CommandContext<'_>) -> Result<(), Error> {
#[poise::command(slash_command)]
pub async fn clear(ctx: CommandContext<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild() else {
let Some(guild_id) = ctx.guild().map(|g| g.id) else {
ctx.say("You're not in a server, silly.").await?;
return Ok(());
};
@ -354,7 +331,7 @@ pub async fn clear(ctx: CommandContext<'_>) -> Result<(), Error> {
.context("Expected a songbird manager")?
.clone();
if manager.get(guild.id).is_some() {
if manager.get(guild_id).is_some() {
{
let mut queue = ctx.data().queue.lock();
queue.clear();