add error context
All checks were successful
All checks were successful
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
use anyhow::Context;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use twilight_model::{
|
use twilight_model::{
|
||||||
@@ -22,7 +23,7 @@ pub(crate) async fn join_channel(
|
|||||||
let channel_id = state
|
let channel_id = state
|
||||||
.cache
|
.cache
|
||||||
.voice_state(user_id, guild_id)
|
.voice_state(user_id, guild_id)
|
||||||
.ok_or("Cannot get voice state for user")?
|
.context("Could not get voice state for user")?
|
||||||
.channel_id();
|
.channel_id();
|
||||||
|
|
||||||
// join the voice channel
|
// join the voice channel
|
||||||
@@ -30,12 +31,12 @@ pub(crate) async fn join_channel(
|
|||||||
.songbird
|
.songbird
|
||||||
.join(guild_id.cast(), channel_id)
|
.join(guild_id.cast(), channel_id)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Could not join voice channel: {:?}", e))?;
|
.context("Could not join voice channel")?;
|
||||||
|
|
||||||
// signal that we are not listening
|
// signal that we are not listening
|
||||||
if let Some(call_lock) = state.songbird.get(guild_id.cast()) {
|
if let Some(call_lock) = state.songbird.get(guild_id.cast()) {
|
||||||
let mut call = call_lock.lock().await;
|
let mut call = call_lock.lock().await;
|
||||||
call.deafen(true).await?;
|
call.deafen(true).await.context("Could not deafen")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create guild config
|
// create guild config
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ use twilight_util::builder::embed::EmbedBuilder;
|
|||||||
use twilight_util::builder::InteractionResponseDataBuilder;
|
use twilight_util::builder::InteractionResponseDataBuilder;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct TrackType {
|
struct TrackType {
|
||||||
url: String,
|
url: String,
|
||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
@@ -245,6 +246,7 @@ pub(crate) async fn play(
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::debug!("Search did not result in any tracks: {}", e);
|
tracing::debug!("Search did not result in any tracks: {}", e);
|
||||||
let content = "Search did not result in any tracks.".to_string();
|
let content = "Search did not result in any tracks.".to_string();
|
||||||
|
|
||||||
let embeds = vec![EmbedBuilder::new()
|
let embeds = vec![EmbedBuilder::new()
|
||||||
.description(content)
|
.description(content)
|
||||||
.color(colors::RED)
|
.color(colors::RED)
|
||||||
@@ -334,12 +336,13 @@ pub(crate) async fn play_inner(
|
|||||||
.interaction(interaction.application_id)
|
.interaction(interaction.application_id)
|
||||||
.update_response(&interaction.token)
|
.update_response(&interaction.token)
|
||||||
.embeds(Some(&embeds))?
|
.embeds(Some(&embeds))?
|
||||||
.await?;
|
.await
|
||||||
|
.context("Could not send playlist loading message")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
if let Some(call_lock) = state.songbird.get(guild_id) {
|
||||||
let call = call_lock.lock().await;
|
let call = call_lock.lock().await;
|
||||||
call.queue().resume()?;
|
call.queue().resume().context("Could not resume playing")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tracks_added = vec![];
|
let mut tracks_added = vec![];
|
||||||
@@ -349,58 +352,84 @@ pub(crate) async fn play_inner(
|
|||||||
.original_url
|
.original_url
|
||||||
.clone()
|
.clone()
|
||||||
.or(yttrack.url.clone())
|
.or(yttrack.url.clone())
|
||||||
.ok_or("Could not find url")?;
|
.context("Could not find url")?;
|
||||||
|
|
||||||
let mut src = YoutubeDl::new(state.client.clone(), url.clone());
|
let mut src = YoutubeDl::new(state.client.clone(), url.clone());
|
||||||
let track: Track = src.clone().into();
|
let track: Track = src.clone().into();
|
||||||
|
|
||||||
if let Ok(metadata) = src.aux_metadata().await {
|
match src.aux_metadata().await {
|
||||||
debug!("metadata: {:?}", metadata);
|
Ok(metadata) => {
|
||||||
|
debug!("metadata: {:?}", metadata);
|
||||||
|
|
||||||
persistence(interaction, yttrack, Arc::clone(&state))
|
persistence(interaction, yttrack, Arc::clone(&state))
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
tracing::error!("could not persist track: {:?}", e);
|
tracing::error!("could not persist track: {:?}", e);
|
||||||
|
});
|
||||||
|
|
||||||
|
tracks_added.push(TrackType {
|
||||||
|
url: url.clone(),
|
||||||
|
title: metadata.title.clone(),
|
||||||
|
duration_string: yttrack.duration_string.clone(),
|
||||||
|
channel: yttrack.channel.clone(),
|
||||||
|
thumbnail: metadata.thumbnail.clone(),
|
||||||
});
|
});
|
||||||
|
|
||||||
tracks_added.push(TrackType {
|
match state.songbird.get(guild_id) {
|
||||||
url: url.clone(),
|
Some(call_lock) => {
|
||||||
title: metadata.title.clone(),
|
let mut call = call_lock.lock().await;
|
||||||
duration_string: yttrack.duration_string.clone(),
|
let handle = call.enqueue_with_preload(
|
||||||
channel: yttrack.channel.clone(),
|
track,
|
||||||
thumbnail: metadata.thumbnail.clone(),
|
metadata.duration.map(|duration| -> Duration {
|
||||||
});
|
if duration.as_secs() > 5 {
|
||||||
|
duration.sub(Duration::from_secs(5))
|
||||||
if let Some(call_lock) = state.songbird.get(guild_id) {
|
} else {
|
||||||
let mut call = call_lock.lock().await;
|
duration
|
||||||
let handle = call.enqueue_with_preload(
|
}
|
||||||
track,
|
}),
|
||||||
metadata.duration.map(|duration| -> Duration {
|
);
|
||||||
if duration.as_secs() > 5 {
|
let mut x = handle.typemap().write().await;
|
||||||
duration.sub(Duration::from_secs(5))
|
x.insert::<MetadataMap>(Metadata {
|
||||||
} else {
|
title: metadata.title,
|
||||||
duration
|
duration: metadata.duration,
|
||||||
}
|
url,
|
||||||
}),
|
src,
|
||||||
);
|
});
|
||||||
let mut x = handle.typemap().write().await;
|
}
|
||||||
x.insert::<MetadataMap>(Metadata {
|
None => tracing::error!("could not get call lock"),
|
||||||
title: metadata.title,
|
}
|
||||||
duration: metadata.duration,
|
}
|
||||||
url,
|
Err(e) => {
|
||||||
src,
|
tracing::error!("could not get metadata: {:?}", e);
|
||||||
});
|
if e.to_string()
|
||||||
|
.contains("Sign in to confirm you’re not a bot.")
|
||||||
|
{
|
||||||
|
let content =
|
||||||
|
"I seem to have been flagged by YouTube as a bot. :-(".to_string();
|
||||||
|
let embeds = vec![EmbedBuilder::new()
|
||||||
|
.description(content)
|
||||||
|
.color(colors::RED)
|
||||||
|
.build()];
|
||||||
|
state
|
||||||
|
.http
|
||||||
|
.interaction(interaction.application_id)
|
||||||
|
.update_response(&interaction.token)
|
||||||
|
.embeds(Some(&embeds))?
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let embeds = build_embeds(&tracks, &tracks_added);
|
|
||||||
|
|
||||||
|
let embeds = build_embeds(&tracks, &tracks_added);
|
||||||
state
|
state
|
||||||
.http
|
.http
|
||||||
.interaction(interaction.application_id)
|
.interaction(interaction.application_id)
|
||||||
.update_response(&interaction.token)
|
.update_response(&interaction.token)
|
||||||
.embeds(Some(&embeds))?
|
.embeds(Some(&embeds))?
|
||||||
.await?;
|
.await
|
||||||
|
.context("Could not send final play message")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user