diff --git a/BUILDS/net6.0/PluginManager.dll b/BUILDS/net6.0/PluginManager.dll index 2241ba3..9a440c2 100644 Binary files a/BUILDS/net6.0/PluginManager.dll and b/BUILDS/net6.0/PluginManager.dll differ diff --git a/DiscordBot/Discord/Commands/Help.cs b/DiscordBot/Discord/Commands/Help.cs index a7aa718..67840a5 100644 --- a/DiscordBot/Discord/Commands/Help.cs +++ b/DiscordBot/Discord/Commands/Help.cs @@ -11,19 +11,48 @@ using System.Collections.Generic; namespace DiscordBot.Discord.Commands { + /// + /// The help command + /// internal class Help : DBCommand { + /// + /// Command name + /// public string Command => "help"; + /// + /// Command Description + /// public string Description => "This command allows you to check all loadded commands"; + /// + /// Command usage + /// public string Usage => "help"; + /// + /// Check if the command can be used /> + /// public bool canUseDM => true; + + /// + /// Check if the command can be used in a server + /// public bool canUseServer => true; + /// + /// Check if the command require administrator to be executed + /// public bool requireAdmin => false; + /// + /// The main body of the command + /// + /// The command context + /// The command message + /// The discord bot client + /// True if the message was sent from a DM channel, false otherwise public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { List args = Functions.GetArguments(message); diff --git a/DiscordBot/Discord/Commands/Restart.cs b/DiscordBot/Discord/Commands/Restart.cs index 8132550..0d3a6b4 100644 --- a/DiscordBot/Discord/Commands/Restart.cs +++ b/DiscordBot/Discord/Commands/Restart.cs @@ -14,26 +14,49 @@ using ds = Discord; using PluginManager.Interfaces; using PluginManager.Others.Permissions; -using PluginManager.Online; using PluginManager.Others; namespace DiscordBot.Discord.Commands { - class Restart : DBCommand + internal class Restart : DBCommand { - string DBCommand.Command => "restart"; + /// + /// Command name + /// + public string Command => "restart"; - string DBCommand.Description => "Restart the bot"; + /// + /// Command Description + /// + public string Description => "Restart the bot"; - string DBCommand.Usage => "restart [-option]"; + /// + /// Command usage + /// + public string Usage => "restart [-p | -c | -args | -cmd] "; - bool DBCommand.canUseDM => false; + /// + /// Check if the command can be used /> + /// + public bool canUseDM => false; - bool DBCommand.canUseServer => true; + /// + /// Check if the command can be used in a server + /// + public bool canUseServer => true; - bool DBCommand.requireAdmin => true; - - void DBCommand.Execute(dsc.SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + /// + /// Check if the command require administrator to be executed + /// + public bool requireAdmin => false; + /// + /// The main body of the command + /// + /// The command context + /// The command message + /// The discord bot client + /// True if the message was sent from a DM channel, false otherwise + public async void Execute(dsc.SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { if (!DiscordPermissions.hasPermission(message.Author as SocketGuildUser, ds.GuildPermission.Administrator)) return; var args = Functions.GetArguments(message); @@ -79,6 +102,9 @@ namespace DiscordBot.Discord.Commands } Environment.Exit(0); break; + default: + await context.Channel.SendMessageAsync("Invalid argument. Use `help restart` to see the usage."); + break; } diff --git a/DiscordBot/Discord/Commands/Settings.cs b/DiscordBot/Discord/Commands/Settings.cs index 04ed19c..4b867f3 100644 --- a/DiscordBot/Discord/Commands/Settings.cs +++ b/DiscordBot/Discord/Commands/Settings.cs @@ -17,16 +17,44 @@ namespace DiscordBot.Discord.Commands { class Settings : DBCommand { + + /// + /// Command name + /// public string Command => "set"; + /// + /// Command Description + /// public string Description => "This command allows you change all settings. Use \"set help\" to show details"; + /// + /// Command usage + /// public string Usage => "set [keyword] [new Value]"; + /// + /// Check if the command can be used /> + /// public bool canUseDM => true; + + /// + /// Check if the command can be used in a server + /// public bool canUseServer => true; + + /// + /// Check if the command require administrator to be executed + /// public bool requireAdmin => true; + /// + /// The main body of the command + /// + /// The command context + /// The command message + /// The discord bot client + /// True if the message was sent from a DM channel, false otherwise public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { var channel = message.Channel; diff --git a/DiscordBot/Discord/Core/Boot.cs b/DiscordBot/Discord/Core/Boot.cs index 043e930..cc5194d 100644 --- a/DiscordBot/Discord/Core/Boot.cs +++ b/DiscordBot/Discord/Core/Boot.cs @@ -11,21 +11,53 @@ namespace PluginManager.Core { internal class Boot { + /// + /// The bot prefix + /// public readonly string botPrefix; + + /// + /// The bot token + /// public readonly string botToken; - private bool isReady = false; + /// + /// Checks if the bot is ready + /// + /// true if the bot is ready, othwerwise false + public bool isReady { get; private set; } = false; + + /// + /// The bot client + /// public DiscordSocketClient client; + + /// + /// The bot command handler + /// private CommandHandler commandServiceHandler; + + /// + /// The command service + /// private CommandService service; + /// + /// The main Boot constructor + /// + /// The bot token + /// The bot prefix public Boot(string botToken, string botPrefix) { this.botPrefix = botPrefix; this.botToken = botToken; } + /// + /// The start method for the bot. This method is used to load the bot + /// + /// Task public async Task Awake() { client = new DiscordSocketClient(); @@ -39,10 +71,15 @@ namespace PluginManager.Core commandServiceHandler = new CommandHandler(client, service, botPrefix); await commandServiceHandler.InstallCommandsAsync(); + //wait for isReady to become true while (!isReady) ; } + /// + /// The method that stops the bot from running + /// + /// public async Task ShutDown() { if (client == null) return; diff --git a/DiscordBot/Discord/Core/CommandHandler.cs b/DiscordBot/Discord/Core/CommandHandler.cs index 088a049..5ed266b 100644 --- a/DiscordBot/Discord/Core/CommandHandler.cs +++ b/DiscordBot/Discord/Core/CommandHandler.cs @@ -21,6 +21,12 @@ namespace PluginManager.Core private readonly CommandService commandService; private readonly string botPrefix; + /// + /// Command handler constructor + /// + /// The discord bot client + /// The discord bot command service + /// The prefix to watch for public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix) { this.client = client; @@ -28,12 +34,21 @@ namespace PluginManager.Core this.botPrefix = botPrefix; } + /// + /// The method to initialize all commands + /// + /// public async Task InstallCommandsAsync() { client.MessageReceived += MessageHandler; await commandService.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null); } + /// + /// The message handler for the bot + /// + /// The message got from the user in discord chat + /// private async Task MessageHandler(SocketMessage Message) { try @@ -45,6 +60,8 @@ namespace PluginManager.Core if (message == null) return; + if (!message.Content.StartsWith(botPrefix)) return; + int argPos = 0; if (message.HasMentionPrefix(client.CurrentUser, ref argPos)) diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 7962b1e..b276b1f 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -83,6 +83,10 @@ namespace DiscordBot await manager.ListAvailablePlugins(); if (listLanguagAtStartup) await languageManager.ListAllLanguages(); + + IProgress progress = null; + + while (true) { Console.ForegroundColor = ConsoleColor.White; @@ -131,7 +135,7 @@ namespace DiscordBot } string path = "./Data/Plugins/" + info[0] + "s/" + name + ".dll"; - IProgress progress = new Progress(percent => + progress = new Progress(percent => { Console.Title = $"Downloading {info[0]}: {name} ({MathF.Round(percent, 2)}%)"; }); @@ -145,11 +149,16 @@ namespace DiscordBot // List lines = await ServerCom.ReadTextFromFile(info[2]); int i = 1; + foreach (var line in lines) { string[] split = line.Split(','); Console.WriteLine($"Downloading item: {split[1]}"); - await ServerCom.DownloadFileAsync(split[0], "./" + split[1], i, lines.Count); + progress = new Progress(bytes => + { + Console.Title = $"Downloading {MathF.Round(bytes, 2)}% ({i}/{lines.Count})"; + }); + await ServerCom.DownloadFileAsync(split[0], "./" + split[1], progress); Console_Utilities.WriteColorText($"Downloaded item {split[1]}"); i++; } diff --git a/PluginManager/Interfaces/DBEvent.cs b/PluginManager/Interfaces/DBEvent.cs index 8741e81..77288fb 100644 --- a/PluginManager/Interfaces/DBEvent.cs +++ b/PluginManager/Interfaces/DBEvent.cs @@ -4,9 +4,20 @@ namespace PluginManager.Interfaces { public interface DBEvent { + /// + /// The name of the event + /// string name { get; } + + /// + /// The description of the event + /// string description { get; } + /// + /// The method that is invoked when the event is loaded into memory + /// + /// The discord bot client void Start(DiscordSocketClient client); } } diff --git a/PluginManager/Items/Command.cs b/PluginManager/Items/Command.cs index 1ac0ab1..0842f8a 100644 --- a/PluginManager/Items/Command.cs +++ b/PluginManager/Items/Command.cs @@ -13,10 +13,30 @@ namespace PluginManager.Items { internal class Command { + /// + /// The author of the command + /// public SocketUser Author; + + /// + /// The list of arguments + /// public List Arguments { get; private set; } + + /// + /// The command that is executed + /// public string CommandName { get; private set; } + + /// + /// The prefix that is used for the command + /// public char PrefixUsed { get; private set; } + + /// + /// The Command class contructor + /// + /// The message that was sent public Command(SocketMessage message) { this.Author = message.Author; @@ -28,6 +48,11 @@ namespace PluginManager.Items this.PrefixUsed = data[0][0]; } + /// + /// The Command class contructor + /// + /// The message string itself + /// True if the message has a prefix, false if not public Command(string message, bool hasPrefix) { string[] data = message.Split(' '); diff --git a/PluginManager/Items/CustomProgressBar.cs b/PluginManager/Items/CustomProgressBar.cs deleted file mode 100644 index 0efdccc..0000000 --- a/PluginManager/Items/CustomProgressBar.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -namespace PluginManager.Items -{ - public class CustomProgressBar - { - private const char _block = '#'; - private const char _emptyBlock = ' '; - private const char _leftMargin = '['; - private const char _rightMargin = ']'; - - const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; - public static void WriteProgressBar(int percent) - { - Console.Write(_back); - Console.Write(_leftMargin); - var p = (int)((percent / 10f) + .5f); - for (var i = 0; i < 10; ++i) - { - if (i >= p) - Console.Write(_emptyBlock); - else - Console.Write(_block); - } - Console.Write($"{_rightMargin} " + percent + " %"); - } - } -} diff --git a/PluginManager/Items/Spinner.cs b/PluginManager/Items/Spinner.cs index 5888eae..ceee3c0 100644 --- a/PluginManager/Items/Spinner.cs +++ b/PluginManager/Items/Spinner.cs @@ -5,13 +5,22 @@ namespace PluginManager.Items { public class Spinner { + /// + /// True if active, false otherwise + /// public bool isSpinning; + /// + /// The Spinner constructor + /// public Spinner() { isSpinning = false; } + /// + /// The method that is called to start spinning the spinner + /// public async void Start() { isSpinning = true; @@ -32,8 +41,13 @@ namespace PluginManager.Items } } + /// + /// The method that is called to stop the spinner from spinning + /// public void Stop() { + if (!isSpinning) + throw new Others.Exceptions.APIException("The spinner was not running", "Stop()"); isSpinning = false; } } diff --git a/PluginManager/Language System/Language.cs b/PluginManager/Language System/Language.cs index d9fe518..f3c5d08 100644 --- a/PluginManager/Language System/Language.cs +++ b/PluginManager/Language System/Language.cs @@ -8,10 +8,34 @@ namespace PluginManager.LanguageSystem { public class Language { + /// + /// The active language + /// public static Language? ActiveLanguage = null; private static readonly string LanguageFileExtension = ".lng"; + /// + /// The name of the language + /// + public string LanguageName { get; } + + /// + /// The file where the language is imported from + /// + public string fileName { get; } + + /// + /// The dictionary of the language + /// + public Dictionary LanguageWords { get; } + + /// + /// The Language constructor + /// + /// The file to import the language from + /// The dictionary of the language + /// The name of the language private Language(string fileName, Dictionary words, string LanguageName) { this.fileName = fileName; @@ -19,12 +43,11 @@ namespace PluginManager.LanguageSystem LanguageWords = words; } - public string LanguageName { get; } - - public string fileName { get; } - - public Dictionary LanguageWords { get; } - + /// + /// Load language from file + /// + /// The file path + /// public static Language? CreateLanguageFromFile(string LanguageFileLocation) { if (!LanguageFileLocation.EndsWith(LanguageFileExtension)) @@ -58,6 +81,12 @@ namespace PluginManager.LanguageSystem return new Language(LanguageFileLocation, words, languageName); } + /// + /// Format text by inserting parameters + /// + /// The raw text + /// The arguments + /// public string FormatText(string text, params string[] args) { if (ActiveLanguage == null) return text; diff --git a/PluginManager/Loaders/CommandsLoader.cs b/PluginManager/Loaders/CommandsLoader.cs index b1cea89..caec835 100644 --- a/PluginManager/Loaders/CommandsLoader.cs +++ b/PluginManager/Loaders/CommandsLoader.cs @@ -8,25 +8,41 @@ using PluginManager.Interfaces; namespace PluginManager.Loaders { - public class CommandsLoader + internal class CommandsLoader { private readonly string CMDPath; private readonly string CMDExtension; - public delegate void onCommandLoaded(string name, bool success, DBCommand? command = null, Exception? exception = null); - public delegate void onCommandFileLoaded(string path); + internal delegate void onCommandLoaded(string name, bool success, DBCommand? command = null, Exception? exception = null); + internal delegate void onCommandFileLoaded(string path); - public onCommandLoaded? OnCommandLoaded; - public onCommandFileLoaded? OnCommandFileLoaded; + /// + /// Event fired when a command is loaded + /// + internal onCommandLoaded? OnCommandLoaded; - public CommandsLoader(string CommandPath, string CommandExtension) + /// + /// Event fired when the file is loaded + /// + internal onCommandFileLoaded? OnCommandFileLoaded; + + /// + /// Command Loader contructor + /// + /// The path to the commands + /// The extension to search for in the + internal CommandsLoader(string CommandPath, string CommandExtension) { CMDPath = CommandPath; CMDExtension = CommandExtension; } - public List? LoadCommands() + /// + /// The method that loads all commands + /// + /// + internal List? LoadCommands() { if (!Directory.Exists(CMDPath)) { diff --git a/PluginManager/Loaders/EventsLoader.cs b/PluginManager/Loaders/EventsLoader.cs index bc1e1eb..28c1766 100644 --- a/PluginManager/Loaders/EventsLoader.cs +++ b/PluginManager/Loaders/EventsLoader.cs @@ -8,25 +8,41 @@ using PluginManager.Interfaces; namespace PluginManager.Loaders { - public class EventsLoader + internal class EventsLoader { private readonly string EVPath; private readonly string EVExtension; - public delegate void onEventLoad(string name, bool success, DBEvent? ev = null, Exception? e = null); - public delegate void onEventFileLoaded(string path); + internal delegate void onEventLoad(string name, bool success, DBEvent? ev = null, Exception? e = null); + internal delegate void onEventFileLoaded(string path); - public onEventLoad? EventLoad; - public onEventFileLoaded? EventFileLoaded; + /// + /// An event that is fired whenever a event is loaded in memory + /// + internal onEventLoad? EventLoad; - public EventsLoader(string path, string ext) + /// + /// An event that is fired whenever a event file is loaded + /// + internal onEventFileLoaded? EventFileLoaded; + + /// + /// The Event Loader constructor + /// + /// The path to all events + /// The extension for events + internal EventsLoader(string path, string ext) { EVPath = path; EVExtension = ext; } - public List? LoadEvents() + /// + /// The method that loads all events + /// + /// + internal List? LoadEvents() { if (!Directory.Exists(EVPath)) diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index ccff7aa..3f6ed97 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -10,6 +10,11 @@ namespace PluginManager.Loaders public class PluginLoader { private DiscordSocketClient client; + + /// + /// The Plugin Loader constructor + /// + /// The discord bot client where the plugins will pe attached to public PluginLoader(DiscordSocketClient discordSocketClient) { this.client = discordSocketClient; @@ -21,17 +26,33 @@ namespace PluginManager.Loaders private const string pluginCMDExtension = ".dll"; private const string pluginEVEExtension = ".dll"; - + /// + /// A list of commands + /// public static List? Plugins { get; set; } + + /// + /// A list of commands + /// public static List? Events { get; set; } - public delegate void CMDLoaded(string name, string typeName, bool success, Exception? e = null); + public delegate void CMDLoaded(string name, string typeName, bool success, Exception? e = null); public delegate void EVELoaded(string name, string typeName, bool success, Exception? e = null); + /// + /// Event that is fired when a is successfully loaded into commands list + /// public CMDLoaded? onCMDLoad; + + /// + /// Event that is fired when a is successfully loaded into events list + /// public EVELoaded? onEVELoad; + /// + /// The main mathod that is called to load all events + /// public void LoadPlugins() { diff --git a/PluginManager/Online/Helpers/OnlineFunctions.cs b/PluginManager/Online/Helpers/OnlineFunctions.cs index ba2bcbf..8754f15 100644 --- a/PluginManager/Online/Helpers/OnlineFunctions.cs +++ b/PluginManager/Online/Helpers/OnlineFunctions.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; using System.Net.Http; using System.Threading.Tasks; using System.IO; @@ -13,6 +9,15 @@ namespace PluginManager.Online.Helpers { internal static class OnlineFunctions { + /// + /// Downloads a and saves it to another . + /// + /// The that is used to download the file + /// The url to the file + /// The to save the downloaded data + /// 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, CancellationToken cancellation = default) { using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) @@ -39,6 +44,12 @@ namespace PluginManager.Online.Helpers } } + /// + /// Read contents of a file as string from specified URL + /// + /// The URL to read from + /// The cancellation token + /// internal static async Task DownloadStringAsync(string url, CancellationToken cancellation = default) { using (var client = new HttpClient()) diff --git a/PluginManager/Online/LanguageManager.cs b/PluginManager/Online/LanguageManager.cs index 0389922..71eacdd 100644 --- a/PluginManager/Online/LanguageManager.cs +++ b/PluginManager/Online/LanguageManager.cs @@ -11,8 +11,17 @@ namespace PluginManager.Online public class LanguageManager { private string link; + + /// + /// The Language Manager constructor + /// + /// The link to where all the languages for the bot are stored public LanguageManager(string link) => this.link = link; + /// + /// The method to list all languages + /// + /// public async Task ListAllLanguages() { @@ -49,6 +58,11 @@ namespace PluginManager.Online } + /// + /// A function that gets the download link for specified language + /// + /// The name of the language + /// public async Task GetDownloadLink(string langName) { try diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 581f6ca..1de837e 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -10,13 +10,24 @@ namespace PluginManager.Online { public class PluginsManager { + /// + /// The URL of the server + /// public string PluginsLink { get; private set; } + /// + /// The Plugin Manager constructor + /// + /// The link to the file where all plugins are stored public PluginsManager(string link) { PluginsLink = link; } + /// + /// The method to load all plugins + /// + /// public async Task ListAvailablePlugins() { try @@ -77,6 +88,11 @@ namespace PluginManager.Online } + /// + /// The method to get plugin information by its name + /// + /// The plugin name + /// public async Task GetPluginLinkByName(string name) { try diff --git a/PluginManager/Online/ServerCom.cs b/PluginManager/Online/ServerCom.cs index 264ee45..2dbc85e 100644 --- a/PluginManager/Online/ServerCom.cs +++ b/PluginManager/Online/ServerCom.cs @@ -13,27 +13,26 @@ namespace PluginManager.Online { public class ServerCom { + + /// + /// Read all lines from a file async + /// + /// The link of the file + /// public static async Task> ReadTextFromFile(string link) { string response = await OnlineFunctions.DownloadStringAsync(link); string[] lines = response.Split('\n'); return lines.ToList(); - - - //[Obsolete] - #region old code for reading text from link - /* - List s = new List(); - WebClient webClient = new WebClient(); - var data = await webClient.OpenReadTaskAsync(link); - var response = await new StreamReader(data).ReadToEndAsync(); - s.AddRange(from a in response.Split('\n') - where !a.StartsWith("$") - select a); - return s;*/ - #endregion } + /// + /// Download file from url + /// + /// The url to the file + /// The location where to store the downloaded data + /// The to track the download + /// public static async Task DownloadFileAsync(string URL, string location, IProgress progress) { using (var client = new System.Net.Http.HttpClient()) @@ -46,50 +45,5 @@ namespace PluginManager.Online } } } - - public static async Task DownloadFileAsync(string url, string location, int downloadNumber, int totalToDownload) - { - - IProgress progress = new Progress(bytes => - { - Console.Title = $"Downloading {MathF.Round(bytes, 2)}% ({downloadNumber}/{totalToDownload})"; - }); - - await DownloadFileAsync(url, location, progress); - Console.Title = "ONLINE"; - return; - - //[Obsolete] - #region old download code - /* - WebClient client = new WebClient(); - Spinner spinner = new Spinner(); - Console.Write("Downloading "); - spinner.Start(); - string oldTitle = Console.Title ?? ""; - client.DownloadProgressChanged += (sender, e) => - { - Console.Title = e.BytesReceived + "/" + e.TotalBytesToReceive + " (" + e.ProgressPercentage + "%) (" + downloadNumber + " / " + totalToDownload + ")"; - }; - client.DownloadFileCompleted += (sender, e) => - { - spinner.Stop(); Console.WriteLine(); - - }; - try - { - await client.DownloadFileTaskAsync(new Uri(url), location); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } - finally - { - Console.Title = oldTitle; - }*/ - - #endregion - } } } diff --git a/PluginManager/Others/Channels.cs b/PluginManager/Others/Channels.cs index 84682a7..89ea068 100644 --- a/PluginManager/Others/Channels.cs +++ b/PluginManager/Others/Channels.cs @@ -4,11 +4,38 @@ using System.Threading.Tasks; namespace PluginManager.Others { + /// + /// A class that handles the sending of messages to the user. + /// public static class ChannelManagement { + /// + /// Get the text channel by name from server + /// + /// The server + /// The channel name + /// public static IGuildChannel GetTextChannel(this IGuild server, string name) => server.GetTextChannel(name); + /// + /// Get the voice channel by name from server + /// + /// The server + /// The channel name + /// public static IGuildChannel GetVoiceChannel(this IGuild server, string name) => server.GetVoiceChannel(name); + + /// + /// Get the DM channel between and + /// + /// + /// public static async Task GetDMChannel(IGuildUser user) => await user.CreateDMChannelAsync(); + + /// + /// Get the channel where the message was sent + /// + /// The message + /// public static IChannel GetChannel(IMessage message) => message.Channel; } diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index a21d6b7..a57cbc4 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -8,33 +8,6 @@ namespace PluginManager.Others { public class Console_Utilities { - const char _block = '■'; - const string _back = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; - const string _twirl = "-\\|/"; - public static void WriteProgressBar(int percent, bool update = false) - { - if (update) - Console.Write(_back); - Console.Write("["); - var p = (int)((percent / 10f) + .5f); - for (var i = 0; i < 10; ++i) - { - if (i >= p) - Console.Write(' '); - else - Console.Write(_block); - } - Console.Write("] {0,3:##0}%", percent); - - if (percent == 100) - Console.WriteLine(); - } - public static void WriteProgress(int progress, bool update = false) - { - if (update) - Console.Write("\b"); - Console.Write(_twirl[progress % _twirl.Length]); - } /// /// A way to create a table based on input data diff --git a/PluginManager/Others/Cryptography.cs b/PluginManager/Others/Cryptography.cs index d9e53e4..e2c13de 100644 --- a/PluginManager/Others/Cryptography.cs +++ b/PluginManager/Others/Cryptography.cs @@ -1,7 +1,49 @@ +using System; + namespace PluginManager.Others { public class Cryptography { + + /// + /// Translate hex to string + /// + /// The encrypted string + /// + public static string FromHexToString(string hexString) + { + var bytes = new byte[hexString.Length / 2]; + for (var i = 0; i < bytes.Length; i++) + { + bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); + } + + return System.Text.Encoding.Unicode.GetString(bytes); + } + + /// + /// Translate string to hex + /// + /// The string to encrypt + /// + public static string ToHexString(string str) + { + var sb = new System.Text.StringBuilder(); + + var bytes = System.Text.Encoding.Unicode.GetBytes(str); + foreach (var t in bytes) + { + sb.Append(t.ToString("X2")); + } + + return sb.ToString(); + } + + /// + /// Create MD5 hash + /// + /// The text to encrypt + /// public static async System.Threading.Tasks.Task CreateMD5(string text) { string output = ""; @@ -17,6 +59,11 @@ namespace PluginManager.Others return output; } + /// + /// Create SHA256 hash + /// + /// The text to encrypt + /// public static async System.Threading.Tasks.Task CreateSHA256(string text) { string output = ""; @@ -31,7 +78,7 @@ namespace PluginManager.Others return output; } - public static System.IO.Stream GenerateStreamFromString(string s) + private static System.IO.Stream GenerateStreamFromString(string s) { var stream = new System.IO.MemoryStream(); var writer = new System.IO.StreamWriter(stream); diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index 43cb702..5413955 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -7,8 +7,14 @@ public enum OperatingSystem { WINDOWS, LINUX, MAC_OS, UNKNOWN } + /// + /// A list with all errors + /// public enum Error { UNKNOWN_ERROR, GUILD_NOT_FOUND, STREAM_NOT_FOUND } + /// + /// The output log type + /// public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL } } \ No newline at end of file diff --git a/PluginManager/Others/Exceptions/APIException.cs b/PluginManager/Others/Exceptions/APIException.cs index ac9c895..881de94 100644 --- a/PluginManager/Others/Exceptions/APIException.cs +++ b/PluginManager/Others/Exceptions/APIException.cs @@ -5,10 +5,28 @@ namespace PluginManager.Others.Exceptions [Serializable] public class APIException : Exception { + /// + /// The function where the error occurred + /// public string? Function { get; } = "not specified"; + + /// + /// The error code + /// public Error? ErrorCode { get; } = Error.UNKNOWN_ERROR; + + /// + /// The possible cause that determined the error + /// public string? PossibleCause { get; } = "not specified"; + /// + /// The APIException contructor + /// + /// The error message + /// The function where the message was triggered + /// The possible cause of the error + /// The error code public APIException(string message, string? function, string possible_cause, Error error) : base(message) { ErrorCode = error; @@ -16,22 +34,37 @@ namespace PluginManager.Others.Exceptions PossibleCause = possible_cause; } + /// + /// The APIException contructor + /// + /// The error message + /// The function where the message was triggered + /// The error code public APIException(string message, string? function, Error? errorCode) : base(message) { ErrorCode = errorCode; Function = function; } - + /// + /// The APIException contructor + /// + /// The error message + /// The function where the message was triggered public APIException(string message, string? function) : base(message) { Function = function; } - + /// + /// The APIException contructor + /// + /// The error message public APIException(string message) : base(message) { } - + /// + /// Method to print the error to + /// public void Print() { Console.WriteLine("Message Content: " + Message); diff --git a/PluginManager/Others/Functions.cs b/PluginManager/Others/Functions.cs index fb23cd5..02afbf5 100644 --- a/PluginManager/Others/Functions.cs +++ b/PluginManager/Others/Functions.cs @@ -199,30 +199,6 @@ namespace PluginManager.Others } - public static string FromHexToString(string hexString) - { - var bytes = new byte[hexString.Length / 2]; - for (var i = 0; i < bytes.Length; i++) - { - bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); - } - - return System.Text.Encoding.Unicode.GetString(bytes); - } - - public static string ToHexString(string str) - { - var sb = new System.Text.StringBuilder(); - - var bytes = System.Text.Encoding.Unicode.GetBytes(str); - foreach (var t in bytes) - { - sb.Append(t.ToString("X2")); - } - - return sb.ToString(); - } - /// /// Copy one Stream to another /// diff --git a/PluginManager/Others/Permissions/DiscordPermissions.cs b/PluginManager/Others/Permissions/DiscordPermissions.cs index 5343cf5..be55780 100644 --- a/PluginManager/Others/Permissions/DiscordPermissions.cs +++ b/PluginManager/Others/Permissions/DiscordPermissions.cs @@ -5,15 +5,47 @@ using System.Linq; namespace PluginManager.Others.Permissions { + /// + /// A class whith all discord permissions + /// public static class DiscordPermissions { + /// + /// Checks if the role has the specified permission + /// + /// The role + /// The permission + /// public static bool hasPermission(this IRole role, GuildPermission permission) => role.Permissions.Has(permission); + /// + /// Check if user has the specified role + /// + /// The user + /// The role + /// public static bool hasRole(this SocketGuildUser user, IRole role) => user.Roles.Contains(role); + /// + /// Check if user has the specified permission + /// + /// The user + /// The permission + /// public static bool hasPermission(this SocketGuildUser user, GuildPermission permission) => user.Roles.Where(role => role.hasPermission(permission)).Any() || user.Guild.Owner == user; + /// + /// Check if user is administrator of server + /// + /// The user + /// public static bool isAdmin(this SocketGuildUser user) => user.hasPermission(GuildPermission.Administrator); + + /// + /// Check if user is administrator of server + /// + /// The user + /// public static bool isAdmin(this SocketUser user) => isAdmin((SocketGuildUser)user); }