Beispiel #1
0
        public async Task <IUserMessage> SendPlayerAsync(IEnergizePlayer ply, IQueueObject obj = null, IChannel chan = null)
        {
            obj = obj ?? ply.CurrentTrack;

            if (ply.TrackPlayer == null)
            {
                ply.TrackPlayer = new TrackPlayer(ply.VoiceChannel.GuildId);
                await ply.TrackPlayer.Update(obj, ply.Volume, ply.IsPaused, ply.IsLooping, false);
            }
            else
            {
                await ply.TrackPlayer.Update(obj, ply.Volume, ply.IsPaused, ply.IsLooping, false);

                await ply.TrackPlayer.DeleteMessage();
            }

            if (obj == null)
            {
                return(null);
            }

            bool isRadio = obj is RadioTrack;

            ply.TrackPlayer.Message = await this.MessageSender.SendAsync(chan ?? ply.TextChannel, ply.TrackPlayer.Embed);

            this.AddPlayerReactions(ply.TrackPlayer.Message, isRadio);
            return(ply.TrackPlayer.Message);
        }
Beispiel #2
0
        public async Task <List <IUserMessage> > AddPlaylistAsync(IVoiceChannel vc, ITextChannel chan, string name, IEnumerable <ILavaTrack> trs)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return(null);
            }

            List <ILavaTrack> tracks = trs.ToList();

            if (tracks.Count < 1)
            {
                return(new List <IUserMessage>
                {
                    await this.MessageSender.SendWarningAsync(chan, "music player", "The loaded playlist does not contain any tracks")
                });
            }

            if (ply.IsPlaying)
            {
                foreach (ILavaTrack track in tracks)
                {
                    ply.Queue.Enqueue(track);
                }

                return(new List <IUserMessage>
                {
                    await this.MessageSender.SendGoodAsync(chan, "music player", $"🎶 Added `{tracks.Count}` tracks from `{name}`")
                });
            }
            ILavaTrack lavaTrack = tracks[0];

            tracks.RemoveAt(0);

            if (tracks.Count > 0)
            {
                foreach (ILavaTrack tr in tracks)
                {
                    ply.Queue.Enqueue(tr);
                }
            }

            await ply.Lavalink.PlayAsync(lavaTrack);

            return(new List <IUserMessage>
            {
                await this.MessageSender.SendGoodAsync(chan, "music player", $"🎶 Added `{tracks.Count}` tracks from `{name}`"),
                await this.SendPlayerAsync(ply, lavaTrack, chan)
            });
        }
Beispiel #3
0
        public async Task ResumeTrackAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return;
            }

            if (ply.IsPlaying && ply.IsPaused)
            {
                await ply.Lavalink.ResumeAsync();
            }
        }
Beispiel #4
0
        public async Task <bool> LoopTrackAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return(false);
            }

            bool isLooping = ply.IsLooping;

            ply.IsLooping = !isLooping;
            return(!isLooping);
        }
Beispiel #5
0
        public async Task SetTrackVolumeAsync(IVoiceChannel vc, ITextChannel chan, int vol)
        {
            vol = Math.Clamp(vol, 0, 200);
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return;
            }

            if (ply.IsPlaying)
            {
                await ply.Lavalink.SetVolumeAsync(vol);
            }
        }
Beispiel #6
0
        public async Task <bool> AutoplayTrackAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return(false);
            }

            bool autoplay = ply.Autoplay;

            ply.Autoplay = !autoplay;

            return(!autoplay);
        }
Beispiel #7
0
        public async Task StopTrackAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return;
            }

            ply.Queue.Clear();
            if (ply.IsPlaying)
            {
                await ply.Lavalink.StopAsync();
            }
        }
Beispiel #8
0
        public async Task <IUserMessage> PlayRadioAsync(IVoiceChannel vc, ITextChannel chan, ILavaTrack lavaTrack)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return(null);
            }

            RadioTrack radio = new RadioTrack(lavaTrack);

            ply.Queue.Clear();
            ply.CurrentRadio = radio;
            await ply.Lavalink.PlayAsync(lavaTrack);

            return(await this.SendPlayerAsync(ply, radio, chan));
        }
Beispiel #9
0
        public async Task <IUserMessage> AddTrackAsync(IVoiceChannel vc, ITextChannel chan, ILavaTrack lavaTrack)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return(null);
            }

            if (ply.IsPlaying)
            {
                ply.Queue.Enqueue(lavaTrack);
                return(await this.SendNewTrackAsync(chan, lavaTrack));
            }
            await ply.Lavalink.PlayAsync(lavaTrack);

            return(await this.SendPlayerAsync(ply, lavaTrack, chan));
        }
Beispiel #10
0
        private async Task OnTrackFinished(LavaPlayer lavalink, ILavaTrack lavaTrack, TrackEndReason reason)
        {
            if (!reason.ShouldPlayNext())
            {
                return;
            }

            IEnergizePlayer ply = this.Players[lavalink.VoiceChannel.GuildId];

            if (ply.IsLooping)
            {
                lavaTrack.ResetPosition();
                await ply.Lavalink.PlayAsync(lavaTrack);
            }
            else
            {
                if (ply.Queue.TryDequeue(out IQueueObject obj))
                {
                    if (obj is ILavaTrack newTrack)
                    {
                        await ply.Lavalink.PlayAsync(newTrack);

                        await this.SendPlayerAsync(ply, newTrack);
                    }
                }
                else
                {
                    if (ply.Autoplay && ply.Queue.Count == 0)
                    {
                        await this.AddRelatedYtContentAsync(ply.VoiceChannel, ply.TextChannel, lavaTrack);
                    }
                    else
                    {
                        if (ply.TrackPlayer != null)
                        {
                            await ply.TrackPlayer.DeleteMessage();
                        }
                    }
                }
            }
        }
Beispiel #11
0
        public async Task SeekTrackAsync(IVoiceChannel vc, ITextChannel chan, int amount)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return;
            }
            if (!ply.IsPlaying)
            {
                return;
            }

            ILavaTrack lavaTrack = ply.CurrentTrack;
            TimeSpan   total     = lavaTrack.Position.Add(TimeSpan.FromSeconds(amount));

            if (total < lavaTrack.Length && total >= TimeSpan.Zero)
            {
                await ply.Lavalink.SeekAsync(total);
            }
        }
Beispiel #12
0
        public async Task <IUserMessage> SendQueueAsync(IVoiceChannel vc, IMessage msg)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, msg.Channel as ITextChannel);

            IPaginatorSenderService paginator = this.ServiceManager.GetService <IPaginatorSenderService>("Paginator");
            List <IQueueObject>     objs      = ply.Queue.Items.ToList();

            if (objs.Count > 0)
            {
                return(await paginator.SendPaginatorAsync(msg, "track queue", objs, async (obj, builder) =>
                {
                    int i = objs.IndexOf(obj);
                    builder.WithDescription($"🎶 Track `#{i + 1}` out of `{objs.Count}` in the queue");

                    if (obj is ILavaTrack track)
                    {
                        builder
                        .WithField("Title", track.Title)
                        .WithField("Author", track.Author)
                        .WithField("Length", track.IsStream ? " - " : track.Length.ToString(@"hh\:mm\:ss"))
                        .WithField("Stream", track.IsStream);

                        string thumbnailurl = await GetThumbnailAsync(track);
                        if (!string.IsNullOrWhiteSpace(thumbnailurl))
                        {
                            builder.WithThumbnailUrl(thumbnailurl);
                        }
                    }
                    else
                    {
                        builder
                        .WithField("Title", "?")
                        .WithField("Author", "?")
                        .WithField("Length", "?")
                        .WithField("Stream", "?");
                    }
                }));
            }
            return(await this.MessageSender.SendGoodAsync(msg, "track queue", "The track queue is empty"));
        }
Beispiel #13
0
        public async Task SkipTrackAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            if (ply == null)
            {
                return;
            }

            if (ply.CurrentRadio != null)
            {
                ply.CurrentRadio = null;
            }

            if (!ply.IsPlaying)
            {
                return;
            }

            if (ply.Queue.Count > 0)
            {
                await ply.Lavalink.SkipAsync();

                await this.SendPlayerAsync(ply, ply.CurrentTrack);
            }
            else
            {
                ILavaTrack oldTrack = ply.CurrentTrack;
                await ply.Lavalink.StopAsync();

                if (ply.Autoplay)
                {
                    await this.AddRelatedYtContentAsync(vc, chan, oldTrack);
                }
            }
        }
Beispiel #14
0
        public async Task ClearTracksAsync(IVoiceChannel vc, ITextChannel chan)
        {
            IEnergizePlayer ply = await this.ConnectAsync(vc, chan);

            ply?.Queue.Clear();
        }