This commit is contained in:
2022-06-08 18:54:58 +03:00
parent c66ff52d94
commit 531edcd3cc
55 changed files with 2253 additions and 2360 deletions

View File

@@ -1,42 +1,60 @@
using System.Threading.Tasks;
using Discord;
using System.Threading.Tasks;
namespace PluginManager.Others;
namespace PluginManager.Others
/// <summary>
/// A class that handles the sending of messages to the user.
/// </summary>
public static class ChannelManagement
{
/// <summary>
/// A class that handles the sending of messages to the user.
/// Get the text channel by name from server
/// </summary>
public static class ChannelManagement
/// <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)
{
/// <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;
return 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)
{
return 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)
{
return 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)
{
return message.Channel;
}
}

View File

@@ -10,23 +10,13 @@ namespace PluginManager.Others
/// </summary>
public class ProgressBar
{
public int Max { get; set; }
public string Message { get; set; }
public int Max { get; init; }
public ConsoleColor Color { get; init; }
public bool NoColor { get; init; }
public ProgressBar(int max, string message)
{
Max = max;
Message = message;
var consoleColors = Enum.GetValues(typeof(ConsoleColor));
while ((Color = (ConsoleColor)consoleColors.GetValue(new Random().Next(consoleColors.Length))!) == ConsoleColor.White && Color != ConsoleColor.Black) ;
}
public void Update(int progress, double speed = -1, string? unit = null)
{
//progress bar
Console.CursorLeft = 0;
Console.Write("[");
Console.CursorLeft = 32;
@@ -38,15 +28,21 @@ namespace PluginManager.Others
for (int i = 0; i < onechunk * progress; i++)
{
Console.BackgroundColor = this.Color;
if (NoColor)
Console.BackgroundColor = ConsoleColor.Black; //this.Color
else
Console.BackgroundColor = this.Color;
Console.CursorLeft = position++;
Console.Write(" ");
Console.Write("#");
}
for (int i = position; i <= 31; i++)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.CursorLeft = position++;
if (NoColor)
Console.BackgroundColor = ConsoleColor.Black; // background of empty bar
else
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.CursorLeft = position++;
Console.Write(" ");
}

View File

@@ -1,91 +1,88 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace PluginManager.Others
namespace PluginManager.Others;
public class Cryptography
{
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);
/// <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 = "";
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
using (var s = GenerateStreamFromString(text))
{
byte[] t = await md5.ComputeHashAsync(s);
output = System.Convert.ToBase64String(t);
}
}
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 = "";
using (System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create())
{
using (var s = GenerateStreamFromString(text))
{
byte[] t = await sha.ComputeHashAsync(s);
output = System.Convert.ToBase64String(t);
}
}
return output;
}
private static System.IO.Stream GenerateStreamFromString(string s)
{
var stream = new System.IO.MemoryStream();
var writer = new System.IO.StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
return 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 StringBuilder();
var bytes = 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 Task<string> CreateMD5(string text)
{
var output = "";
using (var md5 = MD5.Create())
{
using (var s = GenerateStreamFromString(text))
{
var t = await md5.ComputeHashAsync(s);
output = Convert.ToBase64String(t);
}
}
return output;
}
/// <summary>
/// Create SHA256 hash
/// </summary>
/// <param name="text">The text to encrypt</param>
/// <returns></returns>
public static async Task<string> CreateSHA256(string text)
{
var output = "";
using (var sha = SHA256.Create())
{
using (var s = GenerateStreamFromString(text))
{
var t = await sha.ComputeHashAsync(s);
output = Convert.ToBase64String(t);
}
}
return output;
}
private static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}

View File

@@ -1,20 +1,22 @@
namespace PluginManager.Others
namespace PluginManager.Others;
/// <summary>
/// A list of operating systems
/// </summary>
public enum OperatingSystem
{
WINDOWS, LINUX, MAC_OS, UNKNOWN
}
/// <summary>
/// A list of operating systems
/// </summary>
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, INVALID_USER, INVALID_CHANNEL, INVALID_PERMISSIONS
}
/// <summary>
/// A list with all errors
/// </summary>
public enum Error
{ UNKNOWN_ERROR, GUILD_NOT_FOUND, STREAM_NOT_FOUND, INVALID_USER, INVALID_CHANNEL, INVALID_PERMISSIONS }
/// <summary>
/// The output log type
/// </summary>
public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL }
}
/// <summary>
/// The output log type
/// </summary>
public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL }

View File

@@ -1,94 +1,91 @@
using System;
namespace PluginManager.Others.Exceptions
namespace PluginManager.Others.Exceptions;
/// <summary>
/// Custom Exception for PluginManager
/// </summary>
[Serializable]
public class APIException : Exception
{
/// <summary>
/// Custom Exception for PluginManager
/// The APIException contructor
/// </summary>
[Serializable]
public class APIException : Exception
/// <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)
{
/// <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;
Function = function;
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>
/// The APIException constructor
/// </summary>
/// <param name="message">The error message</param>
/// <param name="errorLocation">The class where the error was thrown</param>
public APIException(string message, Type errorLocation) : base(message)
{
Function = errorLocation.FullName;
}
/// <summary>
/// Method to print the error to <see cref="Console"/>
/// </summary>
public void Print()
{
Console.WriteLine("Message Content: " + Message);
Console.WriteLine("Function: " + Function);
Console.WriteLine("Error Code: " + ErrorCode.ToString());
Console.WriteLine("Possible cause: " + PossibleCause);
if (this.StackTrace != null)
Functions.WriteErrFile(this.StackTrace);
}
ErrorCode = error;
Function = function;
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>
/// The APIException constructor
/// </summary>
/// <param name="message">The error message</param>
/// <param name="errorLocation">The class where the error was thrown</param>
public APIException(string message, Type errorLocation) : base(message)
{
Function = errorLocation.FullName;
}
/// <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>
/// Method to print the error to <see cref="Console" />
/// </summary>
public void Print()
{
Console.WriteLine("Message Content: " + Message);
Console.WriteLine("Function: " + Function);
Console.WriteLine("Error Code: " + ErrorCode);
Console.WriteLine("Possible cause: " + PossibleCause);
if (StackTrace != null) Functions.WriteErrFile(StackTrace);
}
}

View File

@@ -212,11 +212,22 @@ namespace PluginManager.Others
/// <returns></returns>
public static (double, string) ConvertBytes(long bytes)
{
if (bytes < 1024) return (bytes, "B");
if (bytes < 1024 * 1024) return (bytes / 1024.0, "KB");
if (bytes < 1024 * 1024 * 1024) return (bytes / 1024.0 / 1024.0, "MB");
return (bytes / 1024.0 / 1024.0 / 1024.0, "GB");
List<string> units = new List<string>()
{
"B",
"KB",
"MB",
"GB",
"TB"
};
int i = 0;
while (bytes >= 1024)
{
i++;
bytes /= 1024;
}
return (bytes, units[i]);
}
/// <summary>

View File

@@ -1,55 +1,64 @@
using Discord;
using System.Linq;
using Discord;
using Discord.WebSocket;
using System.Linq;
namespace PluginManager.Others.Permissions;
namespace PluginManager.Others.Permissions
/// <summary>
/// A class whith all discord permissions
/// </summary>
public static class DiscordPermissions
{
/// <summary>
/// A class whith all discord permissions
/// Checks if the role has the specified permission
/// </summary>
public static class DiscordPermissions
/// <param name="role">The role</param>
/// <param name="permission">The permission</param>
/// <returns></returns>
public static bool hasPermission(this IRole role, GuildPermission permission)
{
/// <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);
return 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)
{
return 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)
{
return 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)
{
return 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)
{
return isAdmin((SocketGuildUser)user);
}
}