diff --git a/BUILDS/net6.0/Music Commands.dll b/BUILDS/net6.0/Music Commands.dll index 0771eac..509f1db 100644 Binary files a/BUILDS/net6.0/Music Commands.dll and b/BUILDS/net6.0/Music Commands.dll differ diff --git a/BUILDS/net6.0/PluginManager.dll b/BUILDS/net6.0/PluginManager.dll index b57de9d..3442573 100644 Binary files a/BUILDS/net6.0/PluginManager.dll and b/BUILDS/net6.0/PluginManager.dll differ diff --git a/BUILDS/net6.0/Plugins/Commands/Music Commands.dll b/BUILDS/net6.0/Plugins/Commands/Music Commands.dll new file mode 100644 index 0000000..509f1db Binary files /dev/null and b/BUILDS/net6.0/Plugins/Commands/Music Commands.dll differ diff --git a/BUILDS/net6.0/Plugins/Commands/MusicCommands.dll b/BUILDS/net6.0/Plugins/Commands/MusicCommands.dll deleted file mode 100644 index 2349924..0000000 Binary files a/BUILDS/net6.0/Plugins/Commands/MusicCommands.dll and /dev/null differ diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 4f5802a..712828f 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; -using Discord; -using Discord.Net.Queue; using DiscordBot.Discord.Core; using PluginManager; using PluginManager.Items; @@ -45,7 +43,8 @@ public class Program Console.Write("Prefix = "); var prefix = Console.ReadLine()![0]; - if (prefix == ' ' || char.IsDigit(prefix)) return; + if (prefix == ' ' || char.IsDigit(prefix)) + return; Config.AddValueToVariables("prefix", prefix.ToString(), false); } @@ -93,20 +92,33 @@ public class Program Console.Clear(); Console.ForegroundColor = ConsoleColor.DarkYellow; - List startupMessageList = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/StartupMessage"); + List startupMessageList = await ServerCom.ReadTextFromURL("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/StartupMessage"); - foreach (var message in startupMessageList) Console.WriteLine(message); + foreach (var message in startupMessageList) + Console.WriteLine(message); Console.WriteLine($"Running on version: {Config.GetValue("Version") ?? System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}"); Console.WriteLine($"Git URL: {Config.GetValue("GitURL") ?? " Could not find Git URL"}"); - Console_Utilities.WriteColorText("&rRemember to close the bot using the ShutDown command &y(sd) &ror some settings won't be saved"); + Console_Utilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"============================ LOG ============================"); try { - var token = Config.GetValue("token"); + var token = Config.GetValue("token"); +#if DEBUG + Console.WriteLine("Starting in DEBUG MODE"); + if (!Directory.Exists("./Data/BetaTest")) + Console.WriteLine("Failed to start in debug mode because the folder ./Data/BetaTest does not exist"); + else + { + token = File.ReadAllText("./Data/BetaTest/token.txt"); + + //Debug mode code... + } +#endif + var prefix = Config.GetValue("prefix"); var discordbooter = new Boot(token, prefix); @@ -239,7 +251,7 @@ public class Program if (Config.GetValue("DeleteLogsAtStartup")) foreach (var file in Directory.GetFiles("./Output/Logs/")) File.Delete(file); - List OnlineDefaultKeys = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/SetupKeys"); + List OnlineDefaultKeys = await ServerCom.ReadTextFromURL("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/SetupKeys"); Config.PluginConfig.Load(); @@ -263,7 +275,7 @@ public class Program } } - List onlineSettingsList = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/OnlineData"); + List onlineSettingsList = await ServerCom.ReadTextFromURL("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/OnlineData"); foreach (var key in onlineSettingsList) { if (key.Length <= 3 || !key.Contains(' ')) continue; diff --git a/MusicCommands/Leave.cs b/MusicCommands/Leave.cs index 98af079..3ee6975 100644 --- a/MusicCommands/Leave.cs +++ b/MusicCommands/Leave.cs @@ -22,10 +22,20 @@ internal class Leave : DBCommand { if (Data.audioClient is not null && Data.voiceChannel is not null) { - Data.Playlist.ClearQueue(); - Data.MusicPlayer.isPlaying = false; await Data.audioClient.StopAsync(); await Data.voiceChannel.DisconnectAsync(); } + + if (Data.Playlist is not null) + { + Data.Playlist.ClearQueue(); + Data.Playlist = new(); + } + + if (Data.MusicPlayer is not null) + { + Data.MusicPlayer.Stop(); + Data.MusicPlayer = null; + } } } diff --git a/MusicCommands/MusicPlayer.cs b/MusicCommands/MusicPlayer.cs index 536f6d7..5b5ae17 100644 --- a/MusicCommands/MusicPlayer.cs +++ b/MusicCommands/MusicPlayer.cs @@ -1,9 +1,5 @@ -using System; -using System.IO; -using System.Net.Http; -using System.Threading; +using System.IO; using System.Threading.Tasks; -using PluginManager.Others; namespace MusicCommands; @@ -48,54 +44,8 @@ internal class MusicPlayer isPlaying = false; } - - /* - public MusicPlayer(Stream input, Stream output) - { - inputStream = input; - outputStream = output; - } - - - - - - public Stream inputStream { get; } // from FFMPEG - public Stream outputStream { get; } // to Voice Channel - - public bool Paused { get; set; } - private bool _stop { get; set; } - - public void Stop() - { - _stop = true; - } - - public async Task StartSendAudio(int bsize) - { - Paused = false; - _stop = false; - while (!_stop) - { - if (Paused) continue; - var buffer = new byte[bsize]; - var bcount = await inputStream.ReadAsync(buffer, 0, bsize); - if (bcount <= 0) - { - Stop(); - Data.CurrentlyRunning = null; - break; - } - - try - { - await outputStream.WriteAsync(buffer, 0, bcount); - } - catch (Exception ex) - { - await outputStream.FlushAsync(); - Functions.WriteLogFile(ex.ToString()); - } - } - }*/ + public void Stop() + { + isPlaying = false; + } } diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 7848152..f20434a 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -19,7 +19,7 @@ namespace PluginManager { public static class PluginConfig { - public static List> InstalledPlugins = new(); + public static readonly List> InstalledPlugins = new(); public static void Load() { diff --git a/PluginManager/Items/Command.cs b/PluginManager/Items/Command.cs index ea8078b..6634b59 100644 --- a/PluginManager/Items/Command.cs +++ b/PluginManager/Items/Command.cs @@ -5,7 +5,7 @@ using PluginManager.Others; namespace PluginManager.Items; -internal class Command +public class Command { /// /// The author of the command @@ -20,10 +20,7 @@ internal class Command { Author = message.Author; var data = message.Content.Split(' '); - if (data.Length > 1) - Arguments = new List(data.MergeStrings(1).Split(' ')); - else - Arguments = new List(); + Arguments = data.Length > 1 ? new List(data.MergeStrings(1).Split(' ')) : new List(); CommandName = data[0].Substring(1); PrefixUsed = data[0][0]; } diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index 8f1e4ab..07e90f2 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; @@ -48,7 +47,7 @@ public class ConsoleCommandsHandler } items.Add(new[] { "-", "-", "-" }); - Console_Utilities.FormatAndAlignTable(items); + Console_Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); } else { @@ -147,7 +146,7 @@ public class ConsoleCommandsHandler { Console.WriteLine($"Downloading requirements for plugin : {name}"); - var lines = await ServerCom.ReadTextFromFile(info[2]); + var lines = await ServerCom.ReadTextFromURL(info[2]); foreach (var line in lines) { diff --git a/PluginManager/Online/Helpers/OnlineFunctions.cs b/PluginManager/Online/Helpers/OnlineFunctions.cs index 213a056..343ffe1 100644 --- a/PluginManager/Online/Helpers/OnlineFunctions.cs +++ b/PluginManager/Online/Helpers/OnlineFunctions.cs @@ -18,32 +18,32 @@ namespace PluginManager.Online.Helpers /// The that is used to track the download progress /// The cancellation token /// - internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, - IProgress? progress = null, IProgress? downloadedBytes = null, CancellationToken cancellation = default) + internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, IProgress? progress = null, IProgress? downloadedBytes = null, int bufferSize = 81920, CancellationToken cancellation = default) { - using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) + using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellation)) { var contentLength = response.Content.Headers.ContentLength; - using (var download = await response.Content.ReadAsStreamAsync()) + using (var download = await response.Content.ReadAsStreamAsync(cancellation)) { - // Ignore progress reporting when no progress reporter was // passed or when the content length is unknown if (progress == null || !contentLength.HasValue) { - await download.CopyToAsync(destination); + await download.CopyToAsync(destination, cancellation); return; } // Convert absolute progress (bytes downloaded) into relative progress (0% - 100%) var relativeProgress = new Progress(totalBytes => - { - progress.Report((float)totalBytes / contentLength.Value * 100); - downloadedBytes?.Report(totalBytes); - }); + { + progress.Report((float)totalBytes / contentLength.Value * 100); + downloadedBytes?.Report(totalBytes); + } + ); + // Use extension method to report progress while downloading - await download.CopyToOtherStreamAsync(destination, 81920, relativeProgress, cancellation); + await download.CopyToOtherStreamAsync(destination, bufferSize, relativeProgress, cancellation); progress.Report(1); } } @@ -57,10 +57,8 @@ namespace PluginManager.Online.Helpers /// internal static async Task DownloadStringAsync(string url, CancellationToken cancellation = default) { - using (var client = new HttpClient()) - { - return await client.GetStringAsync(url); - } + using var client = new HttpClient(); + return await client.GetStringAsync(url, cancellation); } diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index d3bf569..ddbe61c 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -30,7 +30,7 @@ public class PluginsManager { try { - var list = await ServerCom.ReadTextFromFile(PluginsLink); + var list = await ServerCom.ReadTextFromURL(PluginsLink); var lines = list.ToArray(); var data = new List(); @@ -43,7 +43,8 @@ public class PluginsManager data.Add(new[] { "-", "-", "-", "-", "-" }); for (var i = 0; i < len; i++) { - if (lines[i].Length <= 2) continue; + if (lines[i].Length <= 2) + continue; var content = lines[i].Split(','); var display = new string[titles.Length]; if (op == OperatingSystem.WINDOWS) @@ -54,7 +55,7 @@ public class PluginsManager display[1] = content[1]; display[2] = content[2]; if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) - display[3] = ((await ServerCom.ReadTextFromFile(content[5])).Count + 1).ToString(); + display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString(); else display[3] = "1"; @@ -72,7 +73,8 @@ public class PluginsManager display[0] = content[0]; display[1] = content[1]; display[2] = content[2]; - if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) display[3] = ((await ServerCom.ReadTextFromFile(content[5])).Count + 1).ToString(); + if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) + display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString(); if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0])) display[4] = "✓"; else @@ -88,7 +90,7 @@ public class PluginsManager } catch (Exception exception) { - Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message); + Console.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message); Functions.WriteErrFile(exception.ToString()); } } @@ -102,7 +104,7 @@ public class PluginsManager { try { - var list = await ServerCom.ReadTextFromFile(PluginsLink); + var list = await ServerCom.ReadTextFromURL(PluginsLink); var lines = list.ToArray(); var len = lines.Length; for (var i = 0; i < len; i++) @@ -110,8 +112,10 @@ public class PluginsManager var contents = lines[i].Split(','); if (contents[0] == name) { - if (contents.Length == 6) return new[] { contents[2], contents[3], contents[5] }; - if (contents.Length == 5) return new[] { contents[2], contents[3], string.Empty }; + if (contents.Length == 6) + return new[] { contents[2], contents[3], contents[5] }; + if (contents.Length == 5) + return new[] { contents[2], contents[3], string.Empty }; throw new Exception("Failed to download plugin. Invalid Argument Length"); } } diff --git a/PluginManager/Online/ServerCom.cs b/PluginManager/Online/ServerCom.cs index 67c2401..7ae5834 100644 --- a/PluginManager/Online/ServerCom.cs +++ b/PluginManager/Online/ServerCom.cs @@ -9,15 +9,14 @@ using PluginManager.Others; namespace PluginManager.Online { - public class ServerCom + public static class ServerCom { - /// /// Read all lines from a file async /// /// The link of the file /// - public static async Task> ReadTextFromFile(string link) + public static async Task> ReadTextFromURL(string link) { string response = await OnlineFunctions.DownloadStringAsync(link); string[] lines = response.Split('\n'); @@ -52,15 +51,12 @@ namespace PluginManager.Online /// public static async Task DownloadFileAsync(string URL, string location) { - bool isDownloading = true; - int c_progress = 0; + bool isDownloading = true; + float c_progress = 0; - Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100, NoColor = true }; + Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100f, NoColor = true }; - IProgress progress = new Progress(percent => - { - c_progress = (int)percent; - }); + IProgress progress = new Progress(percent => { c_progress = percent; }); Task updateProgressBarTask = new Task(() => @@ -68,7 +64,8 @@ namespace PluginManager.Online while (isDownloading) { pbar.Update(c_progress); - if (c_progress == 100) break; + if (c_progress == 100f) + break; Thread.Sleep(500); } } @@ -78,8 +75,8 @@ namespace PluginManager.Online await DownloadFileAsync(URL, location, progress); - c_progress = 100; - pbar.Update(100); + c_progress = pbar.Max; + pbar.Update(100f); isDownloading = false; } } diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index 42ff71e..15b902e 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -1,4 +1,5 @@ -using System; +using Discord; +using System; using System.Collections.Generic; using System.Linq; @@ -48,7 +49,7 @@ namespace PluginManager.Others if (progress.CanAproximateTo(Max)) Console.Write(progress + " % ✓"); else - Console.Write(progress + " % "); + Console.Write(MathF.Round(progress, 2) + " % "); } else Console.Write(progress + $"{speed} {unit}/s "); @@ -63,93 +64,183 @@ namespace PluginManager.Others /// A way to create a table based on input data /// /// The List of arrays of strings that represent the rows. - public static void FormatAndAlignTable(List data) + public static void FormatAndAlignTable(List data, TableFormat format = TableFormat.CENTER_EACH_COLUMN_BASED) { - char tableLine = '-'; - char tableCross = '+'; - char tableWall = '|'; - - int[] len = new int[data[0].Length]; - foreach (var line in data) + if (format == TableFormat.CENTER_EACH_COLUMN_BASED) { - for (int i = 0; i < line.Length; i++) - if (line[i].Length > len[i]) - len[i] = line[i].Length; - } + char tableLine = '-'; + char tableCross = '+'; + char tableWall = '|'; + + int[] len = new int[data[0].Length]; + foreach (var line in data) + for (int i = 0; i < line.Length; i++) + if (line[i].Length > len[i]) + len[i] = line[i].Length; - foreach (string[] row in data) - { - if (row[0][0] == tableLine) Console.Write(tableCross); - else Console.Write(tableWall); - for (int l = 0; l < row.Length; l++) + foreach (string[] row in data) { - if (row[l][0] == tableLine) - { - for (int i = 0; i < len[l] + 4; ++i) - Console.Write(tableLine); - } - else if (row[l].Length == len[l]) - { - Console.Write(" "); - Console.Write(row[l]); - Console.Write(" "); - } + if (row[0][0] == tableLine) + Console.Write(tableCross); else + Console.Write(tableWall); + for (int l = 0; l < row.Length; l++) { + if (row[l][0] == tableLine) + { + for (int i = 0; i < len[l] + 4; ++i) + Console.Write(tableLine); + } + else if (row[l].Length == len[l]) + { + Console.Write(" "); + Console.Write(row[l]); + Console.Write(" "); + } + else + { + int lenHalf = row[l].Length / 2; + for (int i = 0; i < ((len[l] + 4) / 2 - lenHalf); ++i) + Console.Write(" "); + Console.Write(row[l]); + for (int i = (len[l] + 4) / 2 + lenHalf + 1; i < len[l] + 4; ++i) + Console.Write(" "); + if (row[l].Length % 2 == 0) + Console.Write(" "); + } - int lenHalf = row[l].Length / 2; - for (int i = 0; i < ((len[l] + 4) / 2 - lenHalf); ++i) - Console.Write(" "); - Console.Write(row[l]); - for (int i = (len[l] + 4) / 2 + lenHalf + 1; i < len[l] + 4; ++i) - Console.Write(" "); - if (row[l].Length % 2 == 0) + Console.Write(row[l][0] == tableLine ? tableCross : tableWall); + } + + Console.WriteLine(); //end line + } + + return; + } + + if (format == TableFormat.CENTER_OVERALL_LENGTH) + { + int maxLen = 0; + foreach (string[] row in data) + foreach (string s in row) + if (s.Length > maxLen) + maxLen = s.Length; + + int div = (maxLen + 4) / 2; + + foreach (string[] row in data) + { + Console.Write("\t"); + if (row[0] == "-") + Console.Write("+"); + else + Console.Write("|"); + + foreach (string s in row) + { + if (s == "-") + { + for (int i = 0; i < maxLen + 4; ++i) + Console.Write("-"); + } + else if (s.Length == maxLen) + { + Console.Write(" "); + Console.Write(s); + Console.Write(" "); + } + else + { + int lenHalf = s.Length / 2; + for (int i = 0; i < div - lenHalf; ++i) + Console.Write(" "); + Console.Write(s); + for (int i = div + lenHalf + 1; i < maxLen + 4; ++i) + Console.Write(" "); + if (s.Length % 2 == 0) + Console.Write(" "); + } + + if (s == "-") + Console.Write("+"); + else + Console.Write("|"); + } + + Console.WriteLine(); //end line + } + + return; + } + + if (format == TableFormat.DEFAULT) + { + int[] widths = new int[data[0].Length]; + int space_between_columns = 5; + for (int i = 0; i < data.Count; i++) + { + for (int j = 0; j < data[i].Length; j++) + { + if (data[i][j].Length > widths[j]) + widths[j] = data[i][j].Length; + } + } + + for (int i = 0; i < data.Count; i++) + { + for (int j = 0; j < data[i].Length; j++) + { + if (data[i][j] == "-") + data[i][j] = " "; + Console.Write(data[i][j]); + for (int k = 0; k < widths[j] - data[i][j].Length + 1 + space_between_columns; k++) Console.Write(" "); } - Console.Write(row[l][0] == tableLine ? tableCross : tableWall); + Console.WriteLine(); } - Console.WriteLine(); //end line + return; } + + throw new Exception("Unknown type of table"); } - public static void WriteColorText(string text, bool appendNewLine = true) + public static void WriteColorText(string text, bool appendNewLineAtEnd = true) { - - string[] words = text.Split(' '); - ConsoleColor fg = Console.ForegroundColor; - Dictionary colors = new Dictionary() + ConsoleColor initialForeGround = Console.ForegroundColor; + Dictionary colors = new() { - { "&g", ConsoleColor.Green }, - { "&b", ConsoleColor.Blue }, - { "&r", ConsoleColor.Red }, - { "&m", ConsoleColor.Magenta }, - { "&y", ConsoleColor.Yellow }, - { "&c", fg } + { 'g', ConsoleColor.Green }, + { 'b', ConsoleColor.Blue }, + { 'r', ConsoleColor.Red }, + { 'm', ConsoleColor.Magenta }, + { 'y', ConsoleColor.Yellow }, + { 'c', initialForeGround } }; - foreach (string word in words) + + char[] input = text.ToCharArray(); + for (int i = 0; i < input.Length; i++) { - if (word.Length >= 2) + if (input[i] == '&') { - string prefix = word.Substring(0, 2); - if (colors.ContainsKey(prefix)) - Console.ForegroundColor = colors[prefix]; + if (i + 1 < input.Length) + { + if (colors.ContainsKey(input[i + 1])) + { + Console.ForegroundColor = colors[input[i + 1]]; + i++; + } + } } - - string m = colors.Keys.Aggregate(word, (current, key) => current.Replace(key, "")); - - Console.Write(m + " "); + else + Console.Write(input[i]); } - Console.CursorLeft--; - - if (appendNewLine) - Console.Write('\n'); - - Console.ForegroundColor = fg; + Console.ForegroundColor = initialForeGround; + if (appendNewLineAtEnd) + Console.WriteLine(); } - } } diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index 6ace192..322230d 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -28,4 +28,6 @@ public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL } /// public enum PluginType { Command, Event, Unknown } -public enum UnzipProgressType { PercentageFromNumberOfFiles, PercentageFromTotalSize } \ No newline at end of file +public enum UnzipProgressType { PercentageFromNumberOfFiles, PercentageFromTotalSize } + +public enum TableFormat { CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT } \ No newline at end of file diff --git a/PluginManager/Others/Functions.cs b/PluginManager/Others/Functions.cs index d88caf3..638b6f1 100644 --- a/PluginManager/Others/Functions.cs +++ b/PluginManager/Others/Functions.cs @@ -38,6 +38,11 @@ namespace PluginManager.Others /// public static readonly string pakFolder = @"./Data/Resources/PAK/"; + /// + /// Beta testing folder + /// + public static readonly string betaFolder = @"./Data/BetaTest/"; + /// /// Read data from a file that is inside an archive (ZIP format)