This commit is contained in:
2022-05-03 20:14:13 +03:00
parent eb12c012a3
commit f3d0ea426e
6 changed files with 76 additions and 93 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -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<string> GetYoutubeVideoID()
{
//https://www.youtube.com/watch?v=i-p--m7qaCM&ab_channel={channel}/
return URL.Split('=')[1].Split('&')[0];
}
public async Task<Stream> 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 <Stream> GetStream()", "Work in progress", PluginManager.Others.Error.STREAM_NOT_FOUND);
}
return s;
}
private async Task<Stream> GetSpotifyMusicStreamAsync()
{
throw new APIException("Not implemented", "async Task<Stream> GetSpotifyMusicStream()",
"Work in progress", PluginManager.Others.Error.STREAM_NOT_FOUND);
}
private async Task<Stream> 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<Stream> GetRAWMusicStreamAsync()
{
return await new WebClient().OpenReadTaskAsync(URL);
}
}
}

View File

@@ -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
}
}

View File

@@ -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;

View File

@@ -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);
}
}