Exemplo n.º 1
0
        /// <summary>
        /// Fetches thumbnail of the specified track.
        /// </summary>
        /// <param name="track"><see cref="LavaTrack"/></param>
        public static async Task <string> FetchThumbnailAsync(this LavaTrack track)
        {
            var url = string.Empty;

            switch ($"{track.Uri}".ToLower())
            {
            case var yt when yt.Contains("youtube"):
                return($"https://img.youtube.com/vi/{track.Id}/maxresdefault.jpg");

            case var twich when twich.Contains("twitch"):
                url = $"https://api.twitch.tv/v4/oembed?url={track.Uri}";

                break;

            case var sc when sc.Contains("soundcloud"):
                url = $"https://soundcloud.com/oembed?url={track.Uri}&format=json";

                break;

            case var vim when vim.Contains("vimeo"):
                url = $"https://vimeo.com/api/oembed.json?url={track.Uri}";

                break;
            }

            var req = await HttpHelper.Instance.GetStringAsync(new Uri(url));

            var parse = JObject.Parse(req);

            return(!parse.TryGetValue("thumbnail_url", out var thumb)
                ? "https://i.imgur.com/YPCEUDK.gif"
                : $"{thumb}");
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Plays the specified track with a custom start and end time.
        /// </summary>
        /// <param name="track">An instance of <see cref="LavaTrack" />.</param>
        /// <param name="startTime">Custom start time for track. Must be greater than 0.</param>
        /// <param name="endTime">Custom end time for track. Must be less than <see cref="LavaTrack.Duration" />.</param>
        /// <param name="noReplace">If true, this operation will be ignored if a track is already playing or paused.</param>
        /// <exception cref="ArgumentOutOfRangeException">Throws when start or end time are out of range.</exception>
        /// <exception cref="InvalidOperationException">Throws when star time is bigger than end time.</exception>
        public async Task PlayAsync(LavaTrack track, TimeSpan startTime, TimeSpan endTime, bool noReplace = false)
        {
            if (track == null)
            {
                throw new ArgumentNullException(nameof(track));
            }

            if (startTime.TotalMilliseconds < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startTime), "Value must be greater than 0.");
            }

            if (endTime.TotalMilliseconds < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(endTime), "Value must be greater than 0.");
            }

            if (startTime <= endTime)
            {
                throw new InvalidOperationException($"{nameof(endTime)} must be greather than {nameof(startTime)}.");
            }

            var payload = new PlayPayload(VoiceChannel.GuildId, track.Hash, startTime, endTime, noReplace);
            await _lavaSocket.SendAsync(payload)
            .ConfigureAwait(false);

            Track       = track;
            PlayerState = PlayerState.Playing;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Plays the specified <paramref name="track"/>.
        /// </summary>
        /// <param name="track"><see cref="LavaTrack"/></param>
        /// <param name="noReplace">If set to true, this operation will be ignored if a track is already playing or paused.</param>
        public Task PlayAsync(LavaTrack track, bool noReplace = false)
        {
            IsPlaying    = true;
            CurrentTrack = track;
            var payload = new PlayPayload(VoiceChannel.GuildId, track.Hash, noReplace);

            return(_socketHelper.SendPayloadAsync(payload));
        }
Exemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lavaTrack"></param>
 /// <returns></returns>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 public async Task PlayAsync(LavaTrack lavaTrack)
 {
     Track       = lavaTrack ?? throw new NullReferenceException(nameof(lavaTrack));
     PlayerState = PlayerState.Playing;
     await _lavaSocket.SendAsync(new PlayPayload(VoiceChannel.GuildId, new PlayArgs {
         Track = lavaTrack,
         Volume = 100,
         ShouldPause = false
     }))
     .ConfigureAwait(false);
 }
Exemplo n.º 5
0
        /// <summary>
        ///     Plays the specified track.
        /// </summary>
        /// <param name="track">An instance of <see cref="LavaTrack" />.</param>
        public async Task PlayAsync(LavaTrack track)
        {
            if (track == null)
            {
                throw new ArgumentNullException(nameof(track));
            }

            var payload = new PlayPayload(VoiceChannel.GuildId, track, false);
            await _lavaSocket.SendAsync(payload)
            .ConfigureAwait(false);

            Track       = track;
            PlayerState = PlayerState.Playing;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Play a track with start and stop time.
        /// </summary>
        /// <param name="track"><see cref="LavaTrack"/></param>
        /// <param name="start">Start time for track.</param>
        /// <param name="stop">Stop time for track.</param>
        /// <exception cref="InvalidOperationException">Throws if player isn't connected.</exception>
        /// <exception cref="ArgumentException">Throws if start and stop logic isn't valid.</exception>
        public void PlayPartial(LavaTrack track, TimeSpan start, TimeSpan stop)
        {
            if (start.TotalMilliseconds < 0 || stop.TotalMilliseconds < 0)
            {
                throw new ArgumentException("Start & Stop Must Be Greater Than 0.");
            }

            if (stop <= start)
            {
                throw new ArgumentException("End Time Must Be Greater Than Start Time.");
            }

            CurrentTrack = track;
            Volatile.Write(ref _isDisposed, false);
            _lavaSocket.SendPayload(new PlayPartialPayload(Guild.Id, track, start, stop));
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lavaTrack"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public LavaTrack(LavaTrack lavaTrack)
        {
            if (lavaTrack == null)
            {
                throw new ArgumentNullException(nameof(lavaTrack));
            }

            Hash     = lavaTrack.Hash;
            Id       = lavaTrack.Id;
            Title    = lavaTrack.Title;
            Author   = lavaTrack.Author;
            Url      = lavaTrack.Url;
            Position = lavaTrack.Position;
            Duration = lavaTrack.Duration;
            CanSeek  = lavaTrack.CanSeek;
            IsStream = lavaTrack.IsStream;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Plays the specified <paramref name="track"/>.
        /// </summary>
        /// <param name="track"></param>
        /// <param name="startTime">Optional setting that determines the number of milliseconds to offset the track by.</param>
        /// <param name="stopTime">optional setting that determines at the number of milliseconds at which point the track should stop playing.</param>
        /// <param name="noReplace">If set to true, this operation will be ignored if a track is already playing or paused.</param>
        public Task PlayAsync(LavaTrack track, TimeSpan startTime, TimeSpan stopTime, bool noReplace = false)
        {
            if (startTime.TotalMilliseconds < 0 || stopTime.TotalMilliseconds < 0)
            {
                throw new InvalidOperationException("Start and stop must be greater than 0.");
            }

            if (startTime <= stopTime)
            {
                throw new InvalidOperationException("Stop time must be greater than start time.");
            }

            IsPlaying    = true;
            CurrentTrack = track;
            var payload = new PlayPayload(VoiceChannel.GuildId, track.Hash, startTime, stopTime, noReplace);

            return(_socketHelper.SendPayloadAsync(payload));
        }
Exemplo n.º 9
0
        internal static (string Author, string Title) GetAuthorAndTitle(this LavaTrack lavaTrack)
        {
            var split = lavaTrack.Title.Split('-');

            if (split.Length is 1)
            {
                return(lavaTrack.Author, lavaTrack.Title);
            }

            var author = split[0];
            var title  = split[1];
            var regex  = new Regex(@"(ft).\s+\w+|\(.*?\)|(lyrics)", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            while (regex.IsMatch(title))
            {
                title = regex.Replace(title, string.Empty);
            }

            return(author switch {
                "" => (lavaTrack.Author, title),
                null => (lavaTrack.Author, title),
                _ when string.Equals(author, lavaTrack.Author) => (lavaTrack.Author, title),
                _ => (author, title)
            });
Exemplo n.º 10
0
 /// <summary>
 /// Searches lyrics for the specified track.
 /// </summary>
 /// <param name="track"><see cref="LavaTrack"/></param>
 public static Task <string> FetchLyricsAsync(this LavaTrack track)
 {
     return(LyricsHelper.SearchAsync(track.Author, track.Title));
 }
Exemplo n.º 11
0
 /// <summary>
 ///     Fetches lyrics from OVH API.
 /// </summary>
 /// <param name="track"></param>
 /// <returns><see cref="string"/></returns>
 public static ValueTask <string> FetchLyricsFromOvhAsync(this LavaTrack track)
 {
     return(LyricsResolver.SearchOvhAsync(track));
 }
Exemplo n.º 12
0
 /// <summary>
 ///     Fetches artwork for Youtube, Twitch, SoundCloud and Vimeo.
 /// </summary>
 /// <param name="track"></param>
 /// <returns><see cref="string"/></returns>
 public static ValueTask <string> FetchArtworkAsync(this LavaTrack track)
 {
     return(ArtworkResolver.FetchAsync(track));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Plays the specified track.
 /// </summary>
 /// <param name="track"><see cref="LavaTrack"/></param>
 /// <exception cref="InvalidOperationException">Throws if player isn't connected.</exception>
 public void Play(LavaTrack track)
 {
     CurrentTrack = track;
     Volatile.Write(ref _isDisposed, false);
     _lavaSocket.SendPayload(new PlayPayload(Guild.Id, track));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Removes the first instance of given <see cref="LavaTrack"/>.
 /// </summary>
 /// <param name="track"><see cref="LavaTrack"/></param>
 public void Remove(LavaTrack track)
 {
     Queue.Remove(track);
 }
Exemplo n.º 15
0
 /// <summary>
 /// Adds a track to default queue.
 /// </summary>
 /// <param name="track"><see cref="LavaTrack"/></param>
 public void Enqueue(LavaTrack track)
 {
     Queue.Enqueue(track);
 }