Reworked the Config system
This commit is contained in:
@@ -78,8 +78,7 @@ public class AddMelody: DBCommand
|
||||
await msg.ModifyAsync(a => a.Content = "Done");
|
||||
|
||||
|
||||
var info =
|
||||
MusicInfoExtensions.CreateMusicInfo(title, location, description ?? "Unknown", aliases.ToList(), bsize);
|
||||
var info = MusicInfoExtensions.CreateMusicInfo(title, location, description ?? "Unknown", aliases.ToList(), bsize);
|
||||
|
||||
Variables._MusicDatabase?.Add(title, info);
|
||||
|
||||
|
||||
@@ -21,6 +21,13 @@ public class AddMelodyYoutube: DBCommand
|
||||
|
||||
public async void ExecuteServer(DbCommandExecutingArguments args)
|
||||
{
|
||||
|
||||
if(Variables._MusicDatabase is null)
|
||||
{
|
||||
await args.Context.Channel.SendMessageAsync("Music Database is not loaded !");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Arguments is null)
|
||||
{
|
||||
await args.Context.Channel.SendMessageAsync("Invalid arguments given. Please use the following format:\nadd_melody_youtube [URL]");
|
||||
@@ -28,9 +35,9 @@ public class AddMelodyYoutube: DBCommand
|
||||
}
|
||||
|
||||
|
||||
var URL = args.Arguments[0];
|
||||
var url = args.Arguments[0];
|
||||
|
||||
if (!URL.StartsWith("https://www.youtube.com/watch?v=") && !URL.StartsWith("https://youtu.be/"))
|
||||
if (!url.StartsWith("https://www.youtube.com/watch?v=") && !url.StartsWith("https://youtu.be/"))
|
||||
{
|
||||
await args.Context.Channel.SendMessageAsync("Invalid URL given. Please use the following format:\nadd_melody_youtube [URL]");
|
||||
return;
|
||||
@@ -44,7 +51,7 @@ public class AddMelodyYoutube: DBCommand
|
||||
|
||||
var msg = await args.Context.Channel.SendMessageAsync("Saving melody ...");
|
||||
|
||||
var title = await YoutubeDLP.DownloadMelody(URL);
|
||||
var title = await YoutubeDLP.DownloadMelody(url);
|
||||
|
||||
if (title == null)
|
||||
{
|
||||
@@ -56,8 +63,11 @@ public class AddMelodyYoutube: DBCommand
|
||||
List<string> aliases = joinedAliases.Split('|').ToList();
|
||||
|
||||
|
||||
if (Variables._MusicDatabase.ContainsMelodyWithNameOrAlias(title))
|
||||
Variables._MusicDatabase.Remove(title);
|
||||
if (Variables._MusicDatabase.GetMusicInfoWithTitleOrAlias(title).Any())
|
||||
{
|
||||
await msg.ModifyAsync(x => x.Content = "Melody already exists !");
|
||||
return;
|
||||
}
|
||||
|
||||
Variables._MusicDatabase.Add(title, new MusicInfo()
|
||||
{
|
||||
|
||||
@@ -15,6 +15,19 @@ public class SearchMelody: DBCommand
|
||||
|
||||
public void ExecuteServer(DbCommandExecutingArguments args)
|
||||
{
|
||||
|
||||
if(Variables._MusicDatabase is null)
|
||||
{
|
||||
args.Context.Channel.SendMessageAsync("Music Database is not loaded !");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Arguments is null || args.Arguments.Length == 0)
|
||||
{
|
||||
args.Context.Channel.SendMessageAsync("You need to specify a melody name");
|
||||
return;
|
||||
}
|
||||
|
||||
var title = string.Join(" ", args.Arguments);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(title))
|
||||
@@ -23,16 +36,13 @@ public class SearchMelody: DBCommand
|
||||
return;
|
||||
}
|
||||
|
||||
List<MusicInfo>? info = Variables._MusicDatabase.GetMusicInfoList(title);
|
||||
if (info == null || info.Count == 0)
|
||||
List<MusicInfo> info = Variables._MusicDatabase.GetMusicInfoWithTitleOrAlias(title);
|
||||
if (!info.Any())
|
||||
{
|
||||
args.Context.Channel.SendMessageAsync("No melody with that name or alias was found");
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.Count > 1)
|
||||
args.Context.Channel.SendMessageAsync(embed: info.ToEmbed(Color.DarkOrange));
|
||||
else
|
||||
args.Context.Channel.SendMessageAsync(embed: info[0].ToEmbed(Color.DarkOrange));
|
||||
args.Context.Channel.SendMessageAsync(embed: info.Count > 1 ? info.ToEmbed(Color.DarkOrange) : info[0].ToEmbed(Color.DarkOrange));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,67 @@
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Others.Settings;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace MusicPlayer;
|
||||
|
||||
public class MusicDatabase: SettingsDictionary<string, MusicInfo>
|
||||
public class MusicDatabase: CustomSettingsDictionaryBase<string, MusicInfo>
|
||||
{
|
||||
public MusicDatabase(string file): base(file)
|
||||
public MusicDatabase(string diskLocation): base(diskLocation)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the database contains a melody with the specified name or alias
|
||||
/// </summary>
|
||||
/// <param name="melodyName">The name (alias) of the melody</param>
|
||||
/// <returns></returns>
|
||||
public bool ContainsMelodyWithNameOrAlias(string melodyName)
|
||||
public List<MusicInfo> GetMusicInfoWithTitleOrAlias(string searchQuery)
|
||||
{
|
||||
return ContainsKey(melodyName) || Values.Any(elem => elem.Aliases.Contains(melodyName, StringComparer.CurrentCultureIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the music info of a melody with the specified name or alias. Returns the first match or null if no match was found.
|
||||
/// </summary>
|
||||
/// <param name="searchQuery">The music name or one of its aliases.</param>
|
||||
/// <returns></returns>
|
||||
public MusicInfo? GetMusicInfo(string searchQuery)
|
||||
{
|
||||
return FirstOrDefault(kvp => kvp.Key.Contains(searchQuery, StringComparison.InvariantCultureIgnoreCase) ||
|
||||
kvp.Value.Aliases.Any(alias => alias.Contains(searchQuery, StringComparison.InvariantCultureIgnoreCase))
|
||||
).Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of music info that match the search query. Returns null if an error occurred, or empty list if no match was found.
|
||||
/// </summary>
|
||||
/// <param name="searchQuery">The search query</param>
|
||||
/// <returns>null if an error occured, otherwise a list with songs that match the search query. If no song match the list is empty</returns>
|
||||
public List<MusicInfo>? GetMusicInfoList(string searchQuery)
|
||||
{
|
||||
try
|
||||
List<MusicInfo> musicInfos = new List<MusicInfo>();
|
||||
foreach (var (title, musicInfo) in _InternalDictionary)
|
||||
{
|
||||
return this.Where(kvp =>
|
||||
kvp.Key.Contains(searchQuery, StringComparison.InvariantCultureIgnoreCase) ||
|
||||
kvp.Value.Aliases.Any(alias => alias.Contains(searchQuery, StringComparison.InvariantCultureIgnoreCase))
|
||||
)
|
||||
.Select(item => item.Value).ToList();
|
||||
|
||||
if(title.StartsWith(searchQuery))
|
||||
{
|
||||
musicInfos.Add(musicInfo);
|
||||
}
|
||||
|
||||
if (musicInfo.Aliases is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (musicInfo.Aliases.Contains(searchQuery) || musicInfo.Aliases.Any(x => x.StartsWith(searchQuery)))
|
||||
{
|
||||
musicInfos.Add(musicInfo);
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
return null;
|
||||
}
|
||||
return musicInfos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new entry to the database based on the music info. It uses the title as the key.
|
||||
/// </summary>
|
||||
/// <param name="musicInfo">The music to add to</param>
|
||||
public void AddNewEntryBasedOnMusicInfo(MusicInfo musicInfo)
|
||||
public override async Task SaveToFile()
|
||||
{
|
||||
Add(musicInfo.Title, musicInfo);
|
||||
var json = JsonConvert.SerializeObject(_InternalDictionary, Formatting.Indented);
|
||||
await File.WriteAllTextAsync(_DiskLocation, json);
|
||||
}
|
||||
|
||||
public override async Task LoadFromFile()
|
||||
{
|
||||
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)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
MusicInfo? info = value.ToObject<MusicInfo>();
|
||||
if (info is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_InternalDictionary[key] = info;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,10 +137,13 @@ public class MusicPlayer
|
||||
|
||||
public bool Enqueue(string musicName)
|
||||
{
|
||||
var minfo = Variables._MusicDatabase.GetMusicInfo(musicName);
|
||||
if (minfo is null) return false;
|
||||
var musicInfo = Variables._MusicDatabase?.Get(musicName);
|
||||
if (musicInfo is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
MusicQueue.Enqueue(minfo);
|
||||
MusicQueue.Enqueue(musicInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,11 +152,6 @@ public class MusicPlayer
|
||||
isPlaying = false;
|
||||
}
|
||||
|
||||
public void SetVolume(float volume)
|
||||
{
|
||||
// set volume
|
||||
}
|
||||
|
||||
private static Process? CreateStream(string? fileName, string path)
|
||||
{
|
||||
return Process.Start(new ProcessStartInfo
|
||||
|
||||
@@ -29,6 +29,13 @@ public class Play: DBSlashCommand
|
||||
|
||||
public async void ExecuteServer(SocketSlashCommand context)
|
||||
{
|
||||
|
||||
if(Variables._MusicDatabase is null)
|
||||
{
|
||||
await context.RespondAsync("Music Database is not loaded !");
|
||||
return;
|
||||
}
|
||||
|
||||
var melodyName = context.Data.Options.First().Value as string;
|
||||
|
||||
if (melodyName is null)
|
||||
@@ -37,8 +44,8 @@ public class Play: DBSlashCommand
|
||||
return;
|
||||
}
|
||||
|
||||
var melody = Variables._MusicDatabase.GetMusicInfo(melodyName);
|
||||
if (melody is null)
|
||||
var melody = Variables._MusicDatabase.GetMusicInfoWithTitleOrAlias(melodyName);
|
||||
if (!melody.Any())
|
||||
{
|
||||
await context.RespondAsync("The searched melody does not exists in the database. Sorry :(");
|
||||
return;
|
||||
@@ -71,6 +78,7 @@ public class Play: DBSlashCommand
|
||||
await context.RespondAsync("Failed to enqueue your request. Something went wrong !");
|
||||
return;
|
||||
}
|
||||
|
||||
await context.RespondAsync("Enqueued your request");
|
||||
|
||||
await Variables._MusicPlayer.PlayQueue(); //start queue
|
||||
|
||||
Reference in New Issue
Block a user