Пример #1
0
        public static async Task ShuffleAsync(
            this LavaPlayer player,
            AudioHelper audioHelper,
            EmbedHelper embedHelper,
            IMessageChannel channel      = null,
            SocketCommandContext context = null)
        {
            if (player.Queue.Count < 2)
            {
                return;
            }

            player.Queue.Shuffle();

            if (context != null && !context.Guild.TextChannels.Where(x => x.Id == Program.BotConfig.ChannelId).Any())
            {
                return;
            }

            channel ??= context?.Channel;
            string newQueue = await AudioHelper.GetNewEmbedQueueString(player);

            await Program.BotConfig.BotEmbedMessage.ModifyAsync(x => x.Content = string.Format(AudioHelper.QueueMayHaveSongs, newQueue));

            if (channel != null)
            {
                var msg = await embedHelper.BuildMessageEmbed("Queue shuffled");

                await channel.SendAndRemove(embed : msg);
            }
        }
Пример #2
0
        public static async Task EjectAsync(
            this LavaNode node,
            EmbedHelper embedHelper,
            IGuild guild,
            SocketCommandContext context = null)
        {
            if (context == null)
            {
                return;
            }

            if (!node.TryGetPlayer(guild, out var player))
            {
                return;
            }

            player.Queue.Clear();
            await node.LeaveAsync(player.VoiceChannel);

            if (context != null && !context.Guild.TextChannels.Where(x => x.Id == Program.BotConfig.ChannelId).Any())
            {
                return;
            }
            var embed = await EmbedHelper.BuildDefaultEmbed();

            await Program.BotConfig.BotEmbedMessage.ModifyAsync(x => { x.Content = AudioHelper.NoSongsInQueue; x.Embed = embed; });
        }
Пример #3
0
 public ReactionsHelper(DiscordSocketClient client, LavaNode lavaNode, AudioHelper ah, EmbedHelper eh)
 {
     //Uiharu was here. :)
     discord                = client;
     node                   = lavaNode;
     audioHelper            = ah;
     embedHelper            = eh;
     discord.ReactionAdded += OnReactionAdded;
 }
Пример #4
0
        public static async Task PauseResumeAsync(
            this LavaPlayer player,
            EmbedHelper embedHelper,
            bool paused,
            SocketCommandContext context = null)
        {
            await((paused) ? player.ResumeAsync() : player.PauseAsync());

            if (context != null && context.Guild.TextChannels.Any(x => x.Id == Program.BotConfig.ChannelId) == false)
            {
                return;
            }

            var embed = await embedHelper.BuildMusicEmbed(player, Color.DarkTeal, !paused);

            await Program.BotConfig.BotEmbedMessage.ModifyAsync(x => x.Embed = embed);
        }
Пример #5
0
        public static async Task NextTrackAsync(
            this LavaPlayer player,
            EmbedHelper embedHelper,
            SocketCommandContext context = null)
        {
            if (player.Queue.Count >= 1)
            {
                await player.SkipAsync();
            }
            else
            {
                await player.StopAsync();
            }

            if (context != null && context.Guild.TextChannels.Any(x => x.Id == Program.BotConfig.ChannelId) == false)
            {
                return;
            }
            var embed = await EmbedHelper.BuildDefaultEmbed();

            await Program.BotConfig.BotEmbedMessage.ModifyAsync(x => { x.Content = AudioHelper.NoSongsInQueue; x.Embed = embed; });
        }
Пример #6
0
        public AudioHelper(LavaNode lavanode, EmbedHelper eh)
        {
            Node        = lavanode;
            embedHelper = eh;

            // TODO: Make SpotifyClient own class
            if (Program.BotConfig.SpotifyClientId != "" && Program.BotConfig.SpotifySecret != "")
            {
                var config   = SpotifyClientConfig.CreateDefault();
                var request  = new ClientCredentialsRequest(Program.BotConfig.SpotifyClientId, Program.BotConfig.SpotifySecret);
                var response = new OAuthClient(config).RequestToken(request);
                var spotify  = new SpotifyClient(config.WithToken(response.Result.AccessToken));
                Spotify           = spotify;
                _disconnectTokens = new ConcurrentDictionary <ulong, CancellationTokenSource>();
                SpotifyLogin      = true;
            }


            // Handler for when a LavaTrack is started
            // args: TrackStartEventArgs
            Node.OnTrackStarted += async(args) =>
            {
                var player = args.Player;
                var queue  = await GetNewEmbedQueueString(player);

                var embed = await embedHelper.BuildMusicEmbed(player, Color.DarkTeal);

                //If for some reason Volume is set to 0 (100%) it will set to default volume
                if (player.Volume == 0)
                {
                    await player.UpdateVolumeAsync(Program.BotConfig.Volume);
                }

                var content = queue switch
                {
                    "" => NoSongsInQueue,
                    _ => string.Format(QueueMayHaveSongs, queue)
                };

                await Program.BotConfig.BotEmbedMessage.ModifyAsync(x =>
                {
                    x.Embed   = embed;
                    x.Content = content;
                });

                if (!_disconnectTokens.TryGetValue(args.Player.VoiceChannel.Id, out var value))
                {
                    return;
                }
                if (value.IsCancellationRequested)
                {
                    return;
                }

                value.Cancel(true);
            };

            // Handler for when a LavaTrack is ended, stopped, or skipped
            // args: TrackEndedEventArgs
            Node.OnTrackEnded += async(args) =>
            {
                var player = args.Player;

                if (RepeatFlag)
                {
                    await player.PlayAsync(RepeatTrack);

                    return;
                }

                RepeatFlag = false;

                if (!args.Reason.ShouldPlayNext())
                {
                    return;
                }

                if (!player.Queue.TryDequeue(out var track) && player.Queue.Count == 0)
                {
                    var embed = await EmbedHelper.BuildDefaultEmbed();

                    await Program.BotConfig.BotEmbedMessage.ModifyAsync(x =>
                    {
                        x.Content = NoSongsInQueue;
                        x.Embed   = embed;
                    });

                    if (!StayFlag)
                    {
                        _ = InitiateDisconnectAsync(args.Player, TimeSpan.FromMinutes(15));
                    }
                    return;
                }

                await args.Player.PlayAsync(track);
            };

            // Handler for when a LavaTrack throws an exception
            // args: TrackExceptionEventArgs
            Node.OnTrackException += async(args) =>
            {
                var player       = args.Player;
                var errorMessage = args.ErrorMessage switch
                {
                    "This video cannot be viewed anonymously." => "This video most likely hasn't premiered yet.",
                    "Received unexpected response from YouTube." => "YouTube is most likely down.",
                    _ => "Video might still be processing, try again later."
                };
                var msg = await embedHelper.BuildTrackErrorEmbed($"[{player.Track.Title}]({player.Track.Url})\n{errorMessage}");

                await player.TextChannel.SendAndRemove(embed : msg);

                if (player.Queue.Count == 0)
                {
                    //If no songs in queue it will stop playback to reset the embed
                    await player.StopAsync();

                    return;
                }
                //If queue has any songs it will skip to the next track
                await player.SkipAsync();

                return;
            };

            // Handler for when a LavaTrack gets stuck
            // args: TrackStuckEventArgs
            Node.OnTrackStuck += async(args) =>
            {
                var player = args.Player;
                var msg    = await embedHelper.BuildTrackErrorEmbed($"[{player.Track.Title}]({player.Track.Url})\nTrack got stuck, moving on...");

                await player.TextChannel.SendAndRemove(embed : msg);

                if (player.Queue.Count == 0)
                {
                    //If no songs in queue it will stop playback to reset the embed
                    await player.StopAsync();

                    return;
                }
                //If queue has any songs it will skip to the next track
                await player.SkipAsync();

                return;
            };
        }

        /// <summary>
        /// Initiates a disconnect from the voice channel
        /// </summary>
        /// <param name="player">Instance of LavaPlayer for the guild</param>
        /// <param name="timeSpan">TimeSpan for length before disconnecting</param>
        /// <returns></returns>
        private async Task InitiateDisconnectAsync(LavaPlayer player, TimeSpan timeSpan)
        {
            if (!_disconnectTokens.TryGetValue(player.VoiceChannel.Id, out var value))
            {
                value = new CancellationTokenSource();
                _disconnectTokens.TryAdd(player.VoiceChannel.Id, value);
            }
            else if (value.IsCancellationRequested)
            {
                _disconnectTokens.TryUpdate(player.VoiceChannel.Id, new CancellationTokenSource(), value);
                value = _disconnectTokens[player.VoiceChannel.Id];
            }

            var isCancelled = SpinWait.SpinUntil(() => value.IsCancellationRequested, timeSpan);

            if (isCancelled)
            {
                return;
            }

            await Node.LeaveAsync(player.VoiceChannel);

            var msg = await embedHelper.BuildMessageEmbed("Muse has disconnected due to inactivity.");

            await player.TextChannel.SendAndRemove(embed : msg);
        }
Пример #7
0
        public static async Task LoopAsync(this LavaPlayer player, AudioHelper audioHelper, EmbedHelper embedHelper, IMessageChannel channel)
        {
            audioHelper.RepeatFlag  = !audioHelper.RepeatFlag;
            audioHelper.RepeatTrack = audioHelper.RepeatFlag switch
            {
                true => player.Track,
                false => null
            };

            var embed = await embedHelper.BuildMessageEmbed($"Loop set to `{(audioHelper.RepeatFlag ? "enabled" : "disabled")}`");

            await channel.SendAndRemove(embed : embed, timeout : 5000);
        }