diff --git a/BUILDS/net6.0/MusicCommands.dll b/BUILDS/net6.0/MusicCommands.dll index 80586f4..f7f0d7f 100644 Binary files a/BUILDS/net6.0/MusicCommands.dll and b/BUILDS/net6.0/MusicCommands.dll differ diff --git a/BUILDS/net6.0/PluginManager.dll b/BUILDS/net6.0/PluginManager.dll index 57222cf..2241ba3 100644 Binary files a/BUILDS/net6.0/PluginManager.dll and b/BUILDS/net6.0/PluginManager.dll differ diff --git a/MusicCommands/LinkMusic.cs b/MusicCommands/LinkMusic.cs deleted file mode 100644 index 32fc6bf..0000000 --- a/MusicCommands/LinkMusic.cs +++ /dev/null @@ -1,73 +0,0 @@ -using PluginManager.Others.Exceptions; - -using System; -using System.IO; -using System.Net; -using System.Threading.Tasks; -namespace MusicCommands -{ - public class LinkMusic - { - public string URL { get; private set; } - public LinkType type { get; private set; } - - - public LinkMusic(string URL) - { - this.URL = URL; - if (URL.StartsWith("https://www.youtube.com/watch?v=")) - type = LinkType.YOUTUBE; - else if (URL.StartsWith("https://open.spotify.com/track/")) - type = LinkType.SPOTIFY; - else type = LinkType.RAW; - } - - private async Task GetYoutubeVideoID() - { - //https://www.youtube.com/watch?v=i-p--m7qaCM&ab_channel={channel}/ - return URL.Split('=')[1].Split('&')[0]; - } - - public async Task GetStream() - { - Stream s; - if (type == LinkType.SPOTIFY) s = await GetSpotifyMusicStreamAsync(); - else if (type == LinkType.YOUTUBE) s = await GetYoutubeMusicStreamAsync(); - else s = await GetRAWMusicStreamAsync(); - if (s == null) - { - Console.WriteLine("Failed to get the stream for url: " + this.URL); - throw new APIException("URL [" + this.URL + "] is invalid", "async Task GetStream()", "Work in progress", PluginManager.Others.Error.STREAM_NOT_FOUND); - } - return s; - } - - private async Task GetSpotifyMusicStreamAsync() - { - throw new APIException("Not implemented", "async Task GetSpotifyMusicStream()", - "Work in progress", PluginManager.Others.Error.STREAM_NOT_FOUND); - } - - private async Task GetYoutubeMusicStreamAsync() - { - //https://www.youtube.com/get_video_info?video_id={id}&el=detailpage - string ID = await GetYoutubeVideoID(); - - - using (var webc = new WebClient()) - { - Stream s = await webc.OpenReadTaskAsync($"https://www.youtube.com/get_video_info?video_id={ID}&el=detailpage"); - string str = await new StreamReader(s).ReadToEndAsync(); - Console.WriteLine(str); - - await Task.Delay(-1); - } - return null; - } - - private async Task GetRAWMusicStreamAsync() - { - return await new WebClient().OpenReadTaskAsync(URL); - } - } -} diff --git a/MusicCommands/LinkType.cs b/MusicCommands/LinkType.cs deleted file mode 100644 index 3a65356..0000000 --- a/MusicCommands/LinkType.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MusicCommands -{ - public enum LinkType - { - YOUTUBE, RAW, SPOTIFY - } -} diff --git a/MusicCommands/MusicPlayer.cs b/MusicCommands/MusicPlayer.cs index 293feac..ae58026 100644 --- a/MusicCommands/MusicPlayer.cs +++ b/MusicCommands/MusicPlayer.cs @@ -4,6 +4,10 @@ using PluginManager.Others; using System; using System.IO; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; using System.Threading.Tasks; namespace MusicCommands @@ -18,6 +22,12 @@ namespace MusicCommands outputStream = output; } + public MusicPlayer(Stream output) + { + inputStream = null; + outputStream = output; + } + public bool Paused { get; set; } private bool _stop { get; set; } public void Stop() @@ -25,6 +35,57 @@ namespace MusicCommands _stop = true; } + public async Task StartSendAudioFromLink(string URL) + { + Stream ms = new MemoryStream(); + int bsize = 512; + new Thread(async delegate (object o) + { + var response = await new HttpClient().GetAsync(URL); + using (var stream = await response.Content.ReadAsStreamAsync()) + { + byte[] buffer = new byte[bsize]; + int read; + while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) + { + var pos = ms.Position; + ms.Position = ms.Length; + ms.Write(buffer, 0, read); + ms.Position = pos; + } + } + }).Start(); + Console.Write("Reading data: "); + while (ms.Length < bsize * 10) + { + await Task.Delay(1000); + Console.Title = "Reading data: " + ms.Length + " bytes read of " + bsize * 10; + Console.Write("."); + } + Console.WriteLine("\nDone"); + ms.Position = 0; + + _stop = false; + Paused = false; + + while (!_stop) + { + if (Paused) continue; + byte[] buffer = new byte[bsize]; + int read = await ms.ReadAsync(buffer, 0, buffer.Length); + if (read > 0) + { + await outputStream.WriteAsync(buffer, 0, read); + } + else + { + break; + } + } + + + } + public async Task StartSendAudio() { Paused = false; diff --git a/MusicCommands/lplay.cs b/MusicCommands/lplay.cs index fe06e8d..dcaafde 100644 --- a/MusicCommands/lplay.cs +++ b/MusicCommands/lplay.cs @@ -15,16 +15,22 @@ namespace MusicCommands public string Description => "Play music from a link"; - public string Usage => "lplay [name]"; + public string Usage => "lplay [url]"; public bool canUseDM => false; - public bool canUseServer => false; + public bool canUseServer => true; public bool requireAdmin => false; public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { + string URL = message.Content.Split(' ')[1]; + if (!URL.EndsWith(".mp3") && !URL.EndsWith(".wav") && !URL.EndsWith(".flac") && !URL.EndsWith(".ogg")) + { + await message.Channel.SendMessageAsync("Invalid URL"); + return; + } Data.voiceChannel = (context.User as IGuildUser)?.VoiceChannel; if (Data.voiceChannel == null) { await context.Channel.SendMessageAsync("User must be in a voice channel, or a voice channel must be passed as an argument."); return; } @@ -33,11 +39,13 @@ namespace MusicCommands using (var discord = Data.audioClient.CreatePCMStream(AudioApplication.Mixed)) { - if (Data.CurrentlyRunning != null) - Data.CurrentlyRunning.Stop(); - LinkMusic music = new LinkMusic(Functions.GetArguments(message)[0]); - Data.CurrentlyRunning = new MusicPlayer(await music.GetStream(), discord); - await Data.CurrentlyRunning.StartSendAudio(); + + + await message.Channel.SendMessageAsync("Loading..."); + + Data.CurrentlyRunning = new MusicPlayer(discord); + await Data.CurrentlyRunning.StartSendAudioFromLink(URL); + } }