<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Microsoft.Extensions.Logging;
using SoraBot.Common.Extensions.Modules;
using Victoria;
using Victoria.EventArgs;

namespace SoraBot.Bot.Modules.AudioModule
{
    public class AudioEventHandler : IDisposable
    {
        private readonly ILogger<AudioEventHandler> _log;
        private readonly LavaNode _node;

        public AudioEventHandler(ILogger<AudioEventHandler> log, LavaNode node)
        {
            log.LogInformation("Initialized Audio Event Handlers");
            _log = log;
            _node = node;
            
            _node.OnLog += OnLog;
            _node.OnTrackEnded += OnTrackEnded;
            _node.OnTrackStuck += OnTrackStuck;
            _node.OnTrackException += OnTrackException;
            _node.OnWebSocketClosed += OnWebSocketClosed;
            _node.OnStatsReceived += OnStatsReceived;
        }

        private Task OnStatsReceived(StatsEventArgs arg)
        {
            throw new NotImplementedException();
        }

        private Task OnWebSocketClosed(WebSocketClosedEventArgs e)
        {
            _log.LogWarning("Websocket connection lost from LavaNode");
            return Task.CompletedTask;
        }

        private async Task OnTrackException(TrackExceptionEventArgs e)
        {
            if (e.Player == null || e.Track == null) 
                return;   
            
            e.Player.Queue.Remove(e.Track);

            var eb = this.GetSimpleMusicEmbed("Track threw and exception. Attempting to play next track");
            if (!string.IsNullOrWhiteSpace(e.ErrorMessage))
                eb.WithDescription(e.ErrorMessage);
            
            await e.Player.TextChannel.SendMessageAsync(
                embed: eb.Build());
            
            await e.Player.SkipAsync();
        }

        private async Task OnTrackStuck(TrackStuckEventArgs e)
        {
            if (e.Player == null || e.Track == null) 
                return;   
            
            e.Player.Queue.Remove(e.Track);

            await e.Player.TextChannel.SendMessageAsync(
                embed: this.GetSimpleMusicEmbed("Track got stuck. Attempting to play next track").Build());
            
            await e.Player.SkipAsync();
        }

        private EmbedBuilder GetSimpleMusicEmbed(string message)
            => new EmbedBuilder()
            {
                Color = SoraSocketCommandModule.Blue,
                Title = $"{SoraSocketCommandModule.MusicalNote} {message}"
            };

        private async Task<EmbedBuilder> GetExtendedMusicEmbed(LavaTrack track)
        {
            var eb = new EmbedBuilder()
            {
                Color = SoraSocketCommandModule.Blue,
                Title = $"{SoraSocketCommandModule.MusicalNote} Next: [{track.Duration.ToString(@"mm\:ss")}] - **{track.Title}**",
                Footer = new EmbedFooterBuilder()
                {
                    Text = $"Video by {track.Author}"<commit_msg>#115 - Added additional info to on stuck and exception so you know which track got skipped<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Microsoft.Extensions.Logging;
using SoraBot.Common.Extensions.Modules;
using Victoria;
using Victoria.EventArgs;

namespace SoraBot.Bot.Modules.AudioModule
{
    public class AudioEventHandler : IDisposable
    {
        private readonly ILogger<AudioEventHandler> _log;
        private readonly LavaNode _node;

        public AudioEventHandler(ILogger<AudioEventHandler> log, LavaNode node)
        {
            log.LogInformation("Initialized Audio Event Handlers");
            _log = log;
            _node = node;
            
            _node.OnLog += OnLog;
            _node.OnTrackEnded += OnTrackEnded;
            _node.OnTrackStuck += OnTrackStuck;
            _node.OnTrackException += OnTrackException;
            _node.OnWebSocketClosed += OnWebSocketClosed;
            _node.OnStatsReceived += OnStatsReceived;
        }

        private Task OnStatsReceived(StatsEventArgs arg)
        {
            throw new NotImplementedException();
        }

        private Task OnWebSocketClosed(WebSocketClosedEventArgs e)
        {
            _log.LogWarning("Websocket connection lost from LavaNode");
            return Task.CompletedTask;
        }

        private async Task OnTrackException(TrackExceptionEventArgs e)
        {
            if (e.Player == null || e.Track == null) 
                return;   
            
            e.Player.Queue.Remove(e.Track);

            var eb = this.GetSimpleMusicEmbed("Track threw and exception. Attempting to play next track");

            string desc = "";
            if (!string.IsNullOrWhiteSpace(e.ErrorMessage))
                desc += $"{e.ErrorMessage}\n";
            desc += e.Track.Title;
            eb.WithDescription(desc);
            
            await e.Player.TextChannel.SendMessageAsync(
                embed: eb.Build());
            
            await e.Player.SkipAsync();
        }

        private async Task OnTrackStuck(TrackStuckEventArgs e)
        {
            if (e.Player == null || e.Track == null) 
                return;   
            
            e.Player.Queue.Remove(e.Track);

            await e.Player.TextChannel.SendMessageAsync(
                embed: this.GetSimpleMusicEmbed("Track got stuck. Attempting to play next track")
                    .WithDescription(e.Track.Title).Build());
            
            await e.Player.SkipAsync();
        }

        private EmbedBuilder GetSimpleMusicEmbed(string message)
            => new EmbedBuilder()
            {
                Color = SoraSocketCommandModule.Blue,
                Title = $"{SoraSocketCommandModule.MusicalNote} {message}"
            };

        private async Task<EmbedBuilder> GetExtendedMusicEmbed(LavaTrack track)
        {
            var eb = new EmbedBuilder()
            {
                Color = SoraSocketCommandModule.Blue,
                Title = $"{SoraSocketCommandModule.MusicalNote} Next: [{track.Duration.ToString(@"mm\:ss")}] - **{track.Title}**",
                Footer = new EmbedFooterBuilder()
                {
                    Text = $"Video by {track.Author}"
{'fraction_non_alphanumeric': 0.08801141769743102, 'fraction_numerical': 0.0004757373929590866, 'mean_word_length': 1.872040072859745, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415348', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1594, 'n_words': 349}