Updated documentation

This commit is contained in:
2022-05-03 21:58:44 +03:00
parent f3d0ea426e
commit f5f1827540
26 changed files with 520 additions and 180 deletions

View File

@@ -4,9 +4,20 @@ namespace PluginManager.Interfaces
{
public interface DBEvent
{
/// <summary>
/// The name of the event
/// </summary>
string name { get; }
/// <summary>
/// The description of the event
/// </summary>
string description { get; }
/// <summary>
/// The method that is invoked when the event is loaded into memory
/// </summary>
/// <param name="client">The discord bot client</param>
void Start(DiscordSocketClient client);
}
}

View File

@@ -13,10 +13,30 @@ namespace PluginManager.Items
{
internal class Command
{
/// <summary>
/// The author of the command
/// </summary>
public SocketUser Author;
/// <summary>
/// The list of arguments
/// </summary>
public List<string> Arguments { get; private set; }
/// <summary>
/// The command that is executed
/// </summary>
public string CommandName { get; private set; }
/// <summary>
/// The prefix that is used for the command
/// </summary>
public char PrefixUsed { get; private set; }
/// <summary>
/// The Command class contructor
/// </summary>
/// <param name="message">The message that was sent</param>
public Command(SocketMessage message)
{
this.Author = message.Author;
@@ -28,6 +48,11 @@ namespace PluginManager.Items
this.PrefixUsed = data[0][0];
}
/// <summary>
/// The Command class contructor
/// </summary>
/// <param name="message">The message string itself</param>
/// <param name="hasPrefix">True if the message has a prefix, false if not</param>
public Command(string message, bool hasPrefix)
{
string[] data = message.Split(' ');

View File

@@ -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 + " %");
}
}
}

View File

@@ -5,13 +5,22 @@ namespace PluginManager.Items
{
public class Spinner
{
/// <summary>
/// True if active, false otherwise
/// </summary>
public bool isSpinning;
/// <summary>
/// The Spinner constructor
/// </summary>
public Spinner()
{
isSpinning = false;
}
/// <summary>
/// The method that is called to start spinning the spinner
/// </summary>
public async void Start()
{
isSpinning = true;
@@ -32,8 +41,13 @@ namespace PluginManager.Items
}
}
/// <summary>
/// The method that is called to stop the spinner from spinning
/// </summary>
public void Stop()
{
if (!isSpinning)
throw new Others.Exceptions.APIException("The spinner was not running", "Stop()");
isSpinning = false;
}
}

View File

@@ -8,10 +8,34 @@ namespace PluginManager.LanguageSystem
{
public class Language
{
/// <summary>
/// The active language
/// </summary>
public static Language? ActiveLanguage = null;
private static readonly string LanguageFileExtension = ".lng";
/// <summary>
/// The name of the language
/// </summary>
public string LanguageName { get; }
/// <summary>
/// The file where the language is imported from
/// </summary>
public string fileName { get; }
/// <summary>
/// The dictionary of the language
/// </summary>
public Dictionary<string, string> LanguageWords { get; }
/// <summary>
/// The Language constructor
/// </summary>
/// <param name="fileName">The file to import the language from</param>
/// <param name="words">The dictionary of the language</param>
/// <param name="LanguageName">The name of the language</param>
private Language(string fileName, Dictionary<string, string> 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<string, string> LanguageWords { get; }
/// <summary>
/// Load language from file
/// </summary>
/// <param name="LanguageFileLocation">The file path</param>
/// <returns></returns>
public static Language? CreateLanguageFromFile(string LanguageFileLocation)
{
if (!LanguageFileLocation.EndsWith(LanguageFileExtension))
@@ -58,6 +81,12 @@ namespace PluginManager.LanguageSystem
return new Language(LanguageFileLocation, words, languageName);
}
/// <summary>
/// Format text by inserting parameters
/// </summary>
/// <param name="text">The raw text</param>
/// <param name="args">The arguments</param>
/// <returns></returns>
public string FormatText(string text, params string[] args)
{
if (ActiveLanguage == null) return text;

View File

@@ -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;
/// <summary>
/// Event fired when a command is loaded
/// </summary>
internal onCommandLoaded? OnCommandLoaded;
public CommandsLoader(string CommandPath, string CommandExtension)
/// <summary>
/// Event fired when the file is loaded
/// </summary>
internal onCommandFileLoaded? OnCommandFileLoaded;
/// <summary>
/// Command Loader contructor
/// </summary>
/// <param name="CommandPath">The path to the commands</param>
/// <param name="CommandExtension">The extension to search for in the <paramref name="CommandPath"/></param>
internal CommandsLoader(string CommandPath, string CommandExtension)
{
CMDPath = CommandPath;
CMDExtension = CommandExtension;
}
public List<DBCommand>? LoadCommands()
/// <summary>
/// The method that loads all commands
/// </summary>
/// <returns></returns>
internal List<DBCommand>? LoadCommands()
{
if (!Directory.Exists(CMDPath))
{

View File

@@ -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;
/// <summary>
/// An event that is fired whenever a <see cref="DBEvent"/> event is loaded in memory
/// </summary>
internal onEventLoad? EventLoad;
public EventsLoader(string path, string ext)
/// <summary>
/// An event that is fired whenever a <see cref="DBEvent"/> event file is loaded
/// </summary>
internal onEventFileLoaded? EventFileLoaded;
/// <summary>
/// The Event Loader constructor
/// </summary>
/// <param name="path">The path to all events</param>
/// <param name="ext">The extension for events</param>
internal EventsLoader(string path, string ext)
{
EVPath = path;
EVExtension = ext;
}
public List<DBEvent>? LoadEvents()
/// <summary>
/// The method that loads all events
/// </summary>
/// <returns></returns>
internal List<DBEvent>? LoadEvents()
{
if (!Directory.Exists(EVPath))

View File

@@ -10,6 +10,11 @@ namespace PluginManager.Loaders
public class PluginLoader
{
private DiscordSocketClient client;
/// <summary>
/// The Plugin Loader constructor
/// </summary>
/// <param name="discordSocketClient">The discord bot client where the plugins will pe attached to</param>
public PluginLoader(DiscordSocketClient discordSocketClient)
{
this.client = discordSocketClient;
@@ -21,17 +26,33 @@ namespace PluginManager.Loaders
private const string pluginCMDExtension = ".dll";
private const string pluginEVEExtension = ".dll";
/// <summary>
/// A list of <see cref="DBCommand"/> commands
/// </summary>
public static List<DBCommand>? Plugins { get; set; }
/// <summary>
/// A list of <see cref="DBEvent"/> commands
/// </summary>
public static List<DBEvent>? 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);
/// <summary>
/// Event that is fired when a <see cref="DBCommand"/> is successfully loaded into commands list
/// </summary>
public CMDLoaded? onCMDLoad;
/// <summary>
/// Event that is fired when a <see cref="DBEvent"/> is successfully loaded into events list
/// </summary>
public EVELoaded? onEVELoad;
/// <summary>
/// The main mathod that is called to load all events
/// </summary>
public void LoadPlugins()
{

View File

@@ -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
{
/// <summary>
/// Downloads a <see cref="Stream"/> and saves it to another <see cref="Stream"/>.
/// </summary>
/// <param name="client">The <see cref="HttpClient"/> that is used to download the file</param>
/// <param name="url">The url to the file</param>
/// <param name="destination">The <see cref="Stream"/> to save the downloaded data</param>
/// <param name="progress">The <see cref="IProgress{T}"/> that is used to track the download progress</param>
/// <param name="cancellation">The cancellation token</param>
/// <returns></returns>
internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, IProgress<float> progress = null, CancellationToken cancellation = default)
{
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
@@ -39,6 +44,12 @@ namespace PluginManager.Online.Helpers
}
}
/// <summary>
/// Read contents of a file as string from specified URL
/// </summary>
/// <param name="url">The URL to read from</param>
/// <param name="cancellation">The cancellation token</param>
/// <returns></returns>
internal static async Task<string> DownloadStringAsync(string url, CancellationToken cancellation = default)
{
using (var client = new HttpClient())

View File

@@ -11,8 +11,17 @@ namespace PluginManager.Online
public class LanguageManager
{
private string link;
/// <summary>
/// The Language Manager constructor
/// </summary>
/// <param name="link">The link to where all the languages for the bot are stored</param>
public LanguageManager(string link) => this.link = link;
/// <summary>
/// The method to list all languages
/// </summary>
/// <returns></returns>
public async Task ListAllLanguages()
{
@@ -49,6 +58,11 @@ namespace PluginManager.Online
}
/// <summary>
/// A function that gets the download link for specified language
/// </summary>
/// <param name="langName">The name of the language</param>
/// <returns></returns>
public async Task<string[]?> GetDownloadLink(string langName)
{
try

View File

@@ -10,13 +10,24 @@ namespace PluginManager.Online
{
public class PluginsManager
{
/// <summary>
/// The URL of the server
/// </summary>
public string PluginsLink { get; private set; }
/// <summary>
/// The Plugin Manager constructor
/// </summary>
/// <param name="link">The link to the file where all plugins are stored</param>
public PluginsManager(string link)
{
PluginsLink = link;
}
/// <summary>
/// The method to load all plugins
/// </summary>
/// <returns></returns>
public async Task ListAvailablePlugins()
{
try
@@ -77,6 +88,11 @@ namespace PluginManager.Online
}
/// <summary>
/// The method to get plugin information by its name
/// </summary>
/// <param name="name">The plugin name</param>
/// <returns></returns>
public async Task<string[]> GetPluginLinkByName(string name)
{
try

View File

@@ -13,27 +13,26 @@ namespace PluginManager.Online
{
public class ServerCom
{
/// <summary>
/// Read all lines from a file async
/// </summary>
/// <param name="link">The link of the file</param>
/// <returns></returns>
public static async Task<List<string>> 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<string> s = new List<string>();
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
}
/// <summary>
/// Download file from url
/// </summary>
/// <param name="URL">The url to the file</param>
/// <param name="location">The location where to store the downloaded data</param>
/// <param name="progress">The <see cref="IProgress{T}"/> to track the download</param>
/// <returns></returns>
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> 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<float> progress = new Progress<float>(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
}
}
}

View File

@@ -4,11 +4,38 @@ using System.Threading.Tasks;
namespace PluginManager.Others
{
/// <summary>
/// A class that handles the sending of messages to the user.
/// </summary>
public static class ChannelManagement
{
/// <summary>
/// Get the text channel by name from server
/// </summary>
/// <param name="server">The server</param>
/// <param name="name">The channel name</param>
/// <returns><see cref="IGuildChannel"/></returns>
public static IGuildChannel GetTextChannel(this IGuild server, string name) => server.GetTextChannel(name);
/// <summary>
/// Get the voice channel by name from server
/// </summary>
/// <param name="server">The server</param>
/// <param name="name">The channel name</param>
/// <returns><see cref="IGuildChannel"/></returns>
public static IGuildChannel GetVoiceChannel(this IGuild server, string name) => server.GetVoiceChannel(name);
/// <summary>
/// Get the DM channel between <see cref="Discord.WebSocket.DiscordSocketClient"/> and <see cref="IGuildUser"/>
/// </summary>
/// <param name="user"></param>
/// <returns><see cref="IDMChannel"/></returns>
public static async Task<IDMChannel> GetDMChannel(IGuildUser user) => await user.CreateDMChannelAsync();
/// <summary>
/// Get the channel where the message was sent
/// </summary>
/// <param name="message">The message</param>
/// <returns><see cref="IChannel"/></returns>
public static IChannel GetChannel(IMessage message) => message.Channel;
}

View File

@@ -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]);
}
/// <summary>
/// A way to create a table based on input data

View File

@@ -1,7 +1,49 @@
using System;
namespace PluginManager.Others
{
public class Cryptography
{
/// <summary>
/// Translate hex to string
/// </summary>
/// <param name="hexString">The encrypted string</param>
/// <returns></returns>
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);
}
/// <summary>
/// Translate string to hex
/// </summary>
/// <param name="str">The string to encrypt</param>
/// <returns></returns>
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();
}
/// <summary>
/// Create MD5 hash
/// </summary>
/// <param name="text">The text to encrypt</param>
/// <returns></returns>
public static async System.Threading.Tasks.Task<string> CreateMD5(string text)
{
string output = "";
@@ -17,6 +59,11 @@ namespace PluginManager.Others
return output;
}
/// <summary>
/// Create SHA256 hash
/// </summary>
/// <param name="text">The text to encrypt</param>
/// <returns></returns>
public static async System.Threading.Tasks.Task<string> 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);

View File

@@ -7,8 +7,14 @@
public enum OperatingSystem
{ WINDOWS, LINUX, MAC_OS, UNKNOWN }
/// <summary>
/// A list with all errors
/// </summary>
public enum Error
{ UNKNOWN_ERROR, GUILD_NOT_FOUND, STREAM_NOT_FOUND }
/// <summary>
/// The output log type
/// </summary>
public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL }
}

View File

@@ -5,10 +5,28 @@ namespace PluginManager.Others.Exceptions
[Serializable]
public class APIException : Exception
{
/// <summary>
/// The function where the error occurred
/// </summary>
public string? Function { get; } = "not specified";
/// <summary>
/// The error code
/// </summary>
public Error? ErrorCode { get; } = Error.UNKNOWN_ERROR;
/// <summary>
/// The possible cause that determined the error
/// </summary>
public string? PossibleCause { get; } = "not specified";
/// <summary>
/// The APIException contructor
/// </summary>
/// <param name="message">The error message</param>
/// <param name="function">The function where the message was triggered</param>
/// <param name="possible_cause">The possible cause of the error</param>
/// <param name="error">The error code</param>
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;
}
/// <summary>
/// The APIException contructor
/// </summary>
/// <param name="message">The error message</param>
/// <param name="function">The function where the message was triggered</param>
/// <param name="errorCode">The error code</param>
public APIException(string message, string? function, Error? errorCode) : base(message)
{
ErrorCode = errorCode;
Function = function;
}
/// <summary>
/// The APIException contructor
/// </summary>
/// <param name="message">The error message</param>
/// <param name="function">The function where the message was triggered</param>
public APIException(string message, string? function) : base(message)
{
Function = function;
}
/// <summary>
/// The APIException contructor
/// </summary>
/// <param name="message">The error message</param>
public APIException(string message) : base(message)
{
}
/// <summary>
/// Method to print the error to <see cref="Console"/>
/// </summary>
public void Print()
{
Console.WriteLine("Message Content: " + Message);

View File

@@ -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();
}
/// <summary>
/// Copy one Stream to another <see langword="async"/>
/// </summary>

View File

@@ -5,15 +5,47 @@ using System.Linq;
namespace PluginManager.Others.Permissions
{
/// <summary>
/// A class whith all discord permissions
/// </summary>
public static class DiscordPermissions
{
/// <summary>
/// Checks if the role has the specified permission
/// </summary>
/// <param name="role">The role</param>
/// <param name="permission">The permission</param>
/// <returns></returns>
public static bool hasPermission(this IRole role, GuildPermission permission) => role.Permissions.Has(permission);
/// <summary>
/// Check if user has the specified role
/// </summary>
/// <param name="user">The user</param>
/// <param name="role">The role</param>
/// <returns></returns>
public static bool hasRole(this SocketGuildUser user, IRole role) => user.Roles.Contains(role);
/// <summary>
/// Check if user has the specified permission
/// </summary>
/// <param name="user">The user</param>
/// <param name="permission">The permission</param>
/// <returns></returns>
public static bool hasPermission(this SocketGuildUser user, GuildPermission permission)
=> user.Roles.Where(role => role.hasPermission(permission)).Any() || user.Guild.Owner == user;
/// <summary>
/// Check if user is administrator of server
/// </summary>
/// <param name="user">The user</param>
/// <returns></returns>
public static bool isAdmin(this SocketGuildUser user) => user.hasPermission(GuildPermission.Administrator);
/// <summary>
/// Check if user is administrator of server
/// </summary>
/// <param name="user">The user</param>
/// <returns></returns>
public static bool isAdmin(this SocketUser user) => isAdmin((SocketGuildUser)user);
}