Пример #1
0
        private async Task InternalQueue(CoreMusicPlayer mp, MusicInfo songInfo, bool silent, bool queueFirst = false)
        {
            if (songInfo == null)
            {
                if (!silent)
                {
                    await mp.TextChannel.SendMessageAsync("Song was not found").ConfigureAwait(false);
                }
                return;
            }

            int index;

            try
            {
                index = queueFirst
                    ? mp.EnqueueNext(songInfo)
                    : mp.Enqueue(songInfo);
            }
            catch (QueueFullException)
            {
                await mp.TextChannel.SendMessageAsync("Queue is currently full").ConfigureAwait(false);

                throw;
            }
            if (index != -1)
            {
                if (!silent)
                {
                    try
                    {
                        var embed = new DiscordEmbedBuilder()
                                    .WithDescription($"{songInfo.FormattedName}")
                                    .WithFooter(songInfo.FormattedProvider);

                        if (Uri.IsWellFormedUriString(songInfo.Thumbnail, UriKind.Absolute))
                        {
                            embed.WithThumbnailUrl(songInfo.Thumbnail);
                        }
                        int toDisplay = mp.QueueArray().Songs.Length > 10 ? 10 : mp.QueueArray().Songs.Length;
                        for (int i = mp.Current.Index; i < toDisplay; i++)
                        {
                            embed.AddField(i.ToString(), mp.QueueArray().Songs[i].FormattedName);
                        }
                        var queuedMessage = await mp.TextChannel.SendMessageAsync(embed : embed);

                        if (mp.Stopped)
                        {
                            await mp.TextChannel.SendMessageAsync($"Queue Stopped - Use play to add a song to queue").ConfigureAwait(false);
                        }
                        //await queuedMessage?.DeleteAsync();
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }
Пример #2
0
        public async Task <CoreMusicPlayer> GetOrCreatePlayer(ulong guild, DiscordChannel textChan, DiscordChannel voiceChan)
        {
            if (voiceChan == null || voiceChan.Guild != textChan.Guild)
            {
                if (textChan != null)
                {
                    await textChan.SendMessageAsync("Not in voice").ConfigureAwait(false);
                }
                throw new NotInVoiceChannelException();
            }
            return(MusicPlayers.GetOrAdd((long)guild, _ =>
            {
                float vol = 1.0f;
                var mp = new CoreMusicPlayer(voiceChan, textChan, _google, vol, this);

                DiscordMessage playingMessage = null;
                DiscordMessage lastFinishedMessage = null;
                //add implementation for event trigger
                mp.OnCompleted += async(s, song) =>
                {
                    try
                    {
                        await lastFinishedMessage?.DeleteAsync();
                        try
                        {
                            lastFinishedMessage = await mp.TextChannel.SendMessageAsync(embed: new DiscordEmbedBuilder()
                                                                                        .WithColor(DiscordColor.Blue)
                                                                                        .WithAuthor("Finished Song", song.FormattedFullName)
                                                                                        .WithDescription(song.FormattedName)
                                                                                        .WithFooter(song.FormattedInfo))
                                                  .ConfigureAwait(false);
                        }
                        catch
                        {
                            var cur = mp.Current;
                            if (cur.Current == null &&
                                !mp.RepeatCurrentSong &&
                                !mp.RepeatPlaylist)
                            {
                                await DestroyPlayer((long)guild).ConfigureAwait(false);
                            }
                        }
                    }
                    catch
                    {
                    }
                };

                mp.OnStarted += async(player, song) =>
                {
                    var sender = player;
                    if (sender == null)
                    {
                        return;
                    }
                    try
                    {
                        await playingMessage?.DeleteAsync();

                        playingMessage = await mp.TextChannel.SendMessageAsync(embed: new DiscordEmbedBuilder().WithColor(DiscordColor.Blue)
                                                                               .WithAuthor($"Playing - {song.Index + 1}", song.Song.SongUrl)
                                                                               .WithDescription(song.Song.FormattedName)
                                                                               .WithFooter($"{mp.Volume} | {song.Song.FormattedInfo}"))
                                         .ConfigureAwait(false);
                    }
                    catch
                    {
                        // ignored
                    }
                };
                mp.OnPauseChanged += async(player, paused) =>
                {
                    try
                    {
                        DiscordMessage msg;
                        if (paused)
                        {
                            msg = await mp.TextChannel.SendMessageAsync("paused").ConfigureAwait(false);
                        }
                        else
                        {
                            msg = await mp.TextChannel.SendMessageAsync("resumed").ConfigureAwait(false);
                        }
                        msg?.DeleteAsync();
                    }
                    catch
                    {
                        // ignored
                    }
                };
                _log.Info("Done creating");
                return mp;
            }));
        }