Updated bootstrap and merged gitlab project
This commit is contained in:
@@ -51,13 +51,16 @@ public class AddMelodyYoutube: IDbCommand
|
||||
|
||||
var msg = await args.Context.Channel.SendMessageAsync("Saving melody ...");
|
||||
|
||||
var title = await YoutubeDLP.DownloadMelody(url);
|
||||
|
||||
if (title == null)
|
||||
string musicId = Guid.NewGuid().ToString();
|
||||
string? title = await YoutubeDlp.GetMusicTitle(url);
|
||||
|
||||
if(title is null)
|
||||
{
|
||||
await msg.ModifyAsync(x => x.Content = "Failed to download melody !");
|
||||
await msg.ModifyAsync(x => x.Content = "Failed to retrieve music title from the provided URL.");
|
||||
return;
|
||||
}
|
||||
|
||||
await YoutubeDlp.DownloadMelody(url, musicId);
|
||||
|
||||
var joinedAliases = string.Join(" ", args.Arguments.Skip(1));
|
||||
List<string> aliases = joinedAliases.Split('|').ToList();
|
||||
@@ -74,13 +77,11 @@ public class AddMelodyYoutube: IDbCommand
|
||||
Aliases = aliases,
|
||||
ByteSize = 1024,
|
||||
Description = "Melody added from youtube link",
|
||||
Location = Application.GetResourceFullPath($"Music/Melodies/{title}.mp3"),
|
||||
Location = Application.GetResourceFullPath($"Music/Melodies/{musicId}.mp3"),
|
||||
Title = title
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
await Variables._MusicDatabase.SaveToFile();
|
||||
await msg.ModifyAsync(x => x.Content = "Melody saved !");
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class MusicDatabase: CustomSettingsDictionaryBase<string, MusicInfo>
|
||||
continue;
|
||||
}
|
||||
|
||||
if (musicInfo.Aliases.Contains(searchQuery) || musicInfo.Aliases.Any(x => x.StartsWith(searchQuery)))
|
||||
if (musicInfo.Aliases.Contains(searchQuery) || musicInfo.Aliases.Any(x => x.Contains(searchQuery)))
|
||||
{
|
||||
musicInfos.Add(musicInfo);
|
||||
}
|
||||
@@ -44,10 +44,15 @@ public class MusicDatabase: CustomSettingsDictionaryBase<string, MusicInfo>
|
||||
|
||||
public override async Task LoadFromFile()
|
||||
{
|
||||
if(!File.Exists(_DiskLocation))
|
||||
{
|
||||
await SaveToFile();
|
||||
return;
|
||||
}
|
||||
|
||||
string jsonContent = await File.ReadAllTextAsync(_DiskLocation);
|
||||
var jObject = JsonConvert.DeserializeObject<JObject>(jsonContent);
|
||||
_InternalDictionary.Clear();
|
||||
|
||||
foreach (var (key,value) in jObject)
|
||||
{
|
||||
if (value is null || value.Type == JTokenType.Null)
|
||||
|
||||
@@ -51,9 +51,6 @@ public class MusicPlayer
|
||||
|
||||
ffmpegPath = ffmpegPath.Replace("\\", "/");
|
||||
ffmpegPath = Path.GetFullPath(ffmpegPath);
|
||||
|
||||
Console.WriteLine("FFMPEG Path: " + ffmpegPath);
|
||||
|
||||
while (MusicQueue.TryDequeue(out var dequeuedMusic))
|
||||
{
|
||||
CurrentlyPlaying = dequeuedMusic;
|
||||
@@ -135,15 +132,9 @@ public class MusicPlayer
|
||||
isPaused = false;
|
||||
}
|
||||
|
||||
public bool Enqueue(string musicName)
|
||||
public bool Enqueue(MusicInfo music)
|
||||
{
|
||||
var musicInfo = Variables._MusicDatabase?.Get(musicName);
|
||||
if (musicInfo is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
MusicQueue.Enqueue(musicInfo);
|
||||
MusicQueue.Enqueue(music);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public class Play: IDbSlashCommand
|
||||
|
||||
Variables._MusicPlayer ??= new MusicPlayer();
|
||||
|
||||
if (!Variables._MusicPlayer.Enqueue(melodyName))
|
||||
if (!Variables._MusicPlayer.Enqueue(melody.First()))
|
||||
{
|
||||
await context.RespondAsync("Failed to enqueue your request. Something went wrong !");
|
||||
return;
|
||||
|
||||
@@ -1,42 +1,47 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MusicPlayer;
|
||||
|
||||
public class YoutubeDLP
|
||||
public class YoutubeDlp
|
||||
{
|
||||
public static async Task<string?> DownloadMelody(string url)
|
||||
public static async Task DownloadMelody(string url, string downloadId)
|
||||
{
|
||||
Console.WriteLine("Downloading melody: " + url);
|
||||
var process = new Process();
|
||||
var process = new Process();
|
||||
var baseMusicDirectory = DiscordBotCore.Application.GetResourceFullPath("Music/Melodies/");
|
||||
|
||||
process.StartInfo.FileName = await DiscordBotCore.Application.CurrentApplication.PluginManager.GetDependencyLocation("yt-dlp");
|
||||
process.StartInfo.Arguments = $"-x --force-overwrites -o \"{DiscordBotCore.Application.GetResourceFullPath("/Music/Melodies")}/%(title)s.%(ext)s\" --audio-format mp3 {url}";
|
||||
process.StartInfo.Arguments = $"-x --force-overwrites -o \"{baseMusicDirectory}/{downloadId}.%(ext)s\" --audio-quality 3 --audio-format mp3 {url}";
|
||||
|
||||
process.StartInfo.RedirectStandardOutput = true;
|
||||
var title = "";
|
||||
process.OutputDataReceived += (sender, args) =>
|
||||
process.OutputDataReceived += (_, args) =>
|
||||
{
|
||||
if (args.Data != null)
|
||||
{
|
||||
if (args.Data.StartsWith("[ExtractAudio] Destination: "))
|
||||
{
|
||||
title = args.Data.Replace("[ExtractAudio] Destination: ", "").Replace(".mp3", "");
|
||||
title = title.Replace("\\", "/");
|
||||
title = title.Split('/').Last().Replace(".mp3", "").TrimEnd();
|
||||
|
||||
Console.WriteLine("Output title: " + title);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(args.Data);
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
Console.WriteLine("Waiting for process to exit ...");
|
||||
process.BeginOutputReadLine();
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
|
||||
|
||||
public static async Task<string?> GetMusicTitle(string youtubeUrl)
|
||||
{
|
||||
Process p = new Process();
|
||||
p.StartInfo.FileName=await DiscordBotCore.Application.CurrentApplication.PluginManager.GetDependencyLocation("yt-dlp");
|
||||
p.StartInfo.Arguments = $"--print \"%(title)s\" {youtubeUrl}";
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
|
||||
p.Start();
|
||||
string output = await p.StandardOutput.ReadToEndAsync();
|
||||
await p.WaitForExitAsync();
|
||||
|
||||
output = Regex.Replace(output, @"[\p{L}-[a-zA-Z]]+", "");
|
||||
output = output.TrimEnd();
|
||||
|
||||
return title ?? null;
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user