From af4a83e9ed7c6ceb6e54a005c2bd6ec56680259d Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 20 Apr 2021 22:04:24 +0200 Subject: [PATCH] Add error messages to unsuccessful plays Signed-off-by: trivernis --- src/commands/music/join.rs | 2 +- src/commands/music/play.rs | 2 +- src/commands/music/play_next.rs | 2 +- src/providers/music/mod.rs | 2 ++ src/providers/music/player.rs | 45 ++++++++++++++++++++++++++++++--- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/commands/music/join.rs b/src/commands/music/join.rs index c643460..544e61f 100644 --- a/src/commands/music/join.rs +++ b/src/commands/music/join.rs @@ -42,7 +42,7 @@ async fn join(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { return Ok(()); } log::debug!("Joining channel {} for guild {}", channel_id, guild.id); - MusicPlayer::join(ctx, guild.id, channel_id).await?; + MusicPlayer::join(ctx, guild.id, channel_id, msg.channel_id).await?; EphemeralMessage::create(&ctx.http, msg.channel_id, SHORT_TIMEOUT, |m| { m.content("🎤 Joined the Voice Channel") }) diff --git a/src/commands/music/play.rs b/src/commands/music/play.rs index 5f2ec75..9396cdc 100644 --- a/src/commands/music/play.rs +++ b/src/commands/music/play.rs @@ -30,7 +30,7 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult { if player.is_none() { log::debug!("Not in a channel. Joining authors channel..."); let channel_id = get_channel_for_author(&msg.author.id, &guild)?; - let music_player = MusicPlayer::join(ctx, guild.id, channel_id).await?; + let music_player = MusicPlayer::join(ctx, guild.id, channel_id, msg.channel_id).await?; player = Some(music_player); } let player = player.unwrap(); diff --git a/src/commands/music/play_next.rs b/src/commands/music/play_next.rs index e738dd3..ab62242 100644 --- a/src/commands/music/play_next.rs +++ b/src/commands/music/play_next.rs @@ -30,7 +30,7 @@ async fn play_next(ctx: &Context, msg: &Message, args: Args) -> CommandResult { if player.is_none() { log::debug!("Not in a channel. Joining authors channel..."); let channel_id = get_channel_for_author(&msg.author.id, &guild)?; - let music_player = MusicPlayer::join(ctx, guild.id, channel_id).await?; + let music_player = MusicPlayer::join(ctx, guild.id, channel_id, msg.channel_id).await?; player = Some(music_player); } diff --git a/src/providers/music/mod.rs b/src/providers/music/mod.rs index 13d07d6..a9cf669 100644 --- a/src/providers/music/mod.rs +++ b/src/providers/music/mod.rs @@ -40,10 +40,12 @@ pub(crate) async fn song_to_youtube_video(song: &Song) -> BotResult>>, + msg_channel: ChannelId, leave_flag: bool, paused: bool, } impl MusicPlayer { /// Creates a new music player - pub fn new(client: Arc, http: Arc, guild_id: GuildId) -> Self { + pub fn new( + client: Arc, + http: Arc, + guild_id: GuildId, + msg_channel: ChannelId, + ) -> Self { Self { client, http, guild_id, queue: MusicQueue::new(), + msg_channel, now_playing_msg: None, leave_flag: false, paused: false, @@ -47,6 +55,7 @@ impl MusicPlayer { ctx: &Context, guild_id: GuildId, voice_channel_id: ChannelId, + msg_channel_id: ChannelId, ) -> BotResult>> { let manager = songbird::get(ctx).await.unwrap(); let (_, connection) = manager.join_gateway(guild_id, voice_channel_id).await; @@ -56,7 +65,12 @@ impl MusicPlayer { let mut data = ctx.data.write().await; let client = data.get::().unwrap(); client.create_session(&connection).await?; - let player = MusicPlayer::new(Arc::clone(client), Arc::clone(&ctx.http), guild_id); + let player = MusicPlayer::new( + Arc::clone(client), + Arc::clone(&ctx.http), + guild_id, + msg_channel_id, + ); let player = Arc::new(Mutex::new(player)); let players = data.get_mut::().unwrap(); players.insert(guild_id.0, Arc::clone(&player)); @@ -113,6 +127,7 @@ impl MusicPlayer { /// Tries to play the next song pub async fn try_play_next(&mut self) -> BotResult { let mut next = if let Some(n) = self.queue.next() { + log::trace!("Next is {:?}", n); n } else { return Ok(true); @@ -120,12 +135,26 @@ impl MusicPlayer { let url = if let Some(url) = next.url().await { url } else { + self.send_error_message(format!( + "‼️ Could not find a video to play for '{}' by '{}'", + next.title(), + next.author() + )) + .await?; + log::debug!("Could not find playable candidate for song."); return Ok(false); }; let query_information = match self.client.auto_search_tracks(url).await { Ok(i) => i, Err(e) => { log::error!("Failed to search for song: {}", e); + self.send_error_message(format!( + "‼️ Failed to retrieve information for song '{}' by '{}': {:?}", + next.title(), + next.author(), + e + )) + .await?; return Ok(false); } }; @@ -193,6 +222,16 @@ impl MusicPlayer { pub fn set_leave_flag(&mut self, flag: bool) { self.leave_flag = flag; } + + /// Sends a play error message to the players test channel + async fn send_error_message(&self, content: String) -> BotResult<()> { + EphemeralMessage::create(&self.http, self.msg_channel, SHORT_TIMEOUT, |m| { + m.content(content) + }) + .await?; + + Ok(()) + } } /// Stats a tokio coroutine to check for player disconnect conditions