This commit is contained in:
2022-07-09 09:48:58 +03:00
parent a66ebc43d9
commit 3839e4d838
17 changed files with 215 additions and 177 deletions

View File

@@ -17,4 +17,8 @@
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="YoutubeExplode" Version="6.2.0" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Discord;
using Discord.Audio;
using Discord.Commands;
@@ -12,11 +13,11 @@ namespace MusicCommands;
internal class Play : DBCommand
{
public string Command => "fplay";
public string Command => "play";
public string Description => "Play music from a file";
public string Usage => "fplay [name]";
public string Usage => "fplay [name/url]";
public bool canUseDM => false;
@@ -26,14 +27,42 @@ internal class Play : DBCommand
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
var path = "./Music";
var FileName = Functions.GetArguments(message).ToArray().MergeStrings(0);
path += "/" + FileName + ".ogg";
if (!File.Exists(path))
{
Console.WriteLine("Unknown path " + path);
Directory.CreateDirectory("Music");
var path = "./Music/";
string[] splitted = message.Content.Split(' ');
if (splitted.Length < 2)
return;
do
{
if (splitted.Length == 2)
{
if (!splitted[1].Contains("youtube.com"))
{
await context.Channel.SendMessageAsync("Invalid link");
return;
}
string url = splitted[1];
path += $"{Functions.CreateMD5(url)}";
if (File.Exists(path))
break;
//await context.Channel.SendMessageAsync("Searching for " + url);
await GetMusicAudio(url, path);
//await context.Channel.SendMessageAsync("Playing: " + url);
}
else
{
string searchString = Functions.MergeStrings(splitted, 1);
path += $"{Functions.CreateMD5(searchString)}";
if (File.Exists(path))
break;
await context.Channel.SendMessageAsync("Searching for " + searchString);
await GetMusicAudio(searchString, path);
await context.Channel.SendMessageAsync("Playing: " + searchString);
}
}
while (false);
Data.voiceChannel = (context.User as IGuildUser)?.VoiceChannel;
@@ -59,4 +88,16 @@ internal class Play : DBCommand
{
return Process.Start(new ProcessStartInfo { FileName = "ffmpeg", Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1", UseShellExecute = false, RedirectStandardOutput = true });
}
private async Task GetMusicAudio(string url, string location)
{
Process proc = new Process();
proc.StartInfo.FileName = "MusicDownloader.exe";
proc.StartInfo.Arguments = $"{url},{location}";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
await proc.WaitForExitAsync();
}
}

View File

@@ -1,49 +0,0 @@
using Discord;
using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
namespace MusicCommands;
internal class lplay : DBCommand
{
public string Command => "lplay";
public string Description => "Play music from a link";
public string Usage => "lplay [url]";
public bool canUseDM => false;
public bool canUseServer => false;
public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
{
var 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;
}
Data.audioClient = await Data.voiceChannel.ConnectAsync();
using (var discord = Data.audioClient.CreatePCMStream(AudioApplication.Mixed))
{
await message.Channel.SendMessageAsync("Loading...");
Data.CurrentlyRunning = new MusicPlayer(discord);
await Data.CurrentlyRunning.StartSendAudioFromLink(URL);
}
}
}