Add project files.

This commit is contained in:
Wizzy69
2022-01-07 20:26:10 +02:00
parent 8b8d4c7147
commit 84ee5c6235
53 changed files with 3544 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
namespace PluginManager.Interfaces
{
public interface DBCommand
{
string Command { get; }
string Description { get; }
string Usage { get; }
bool canUseDM { get; }
bool canUseServer { get; }
void Execute(Discord.Commands.SocketCommandContext context,
Discord.WebSocket.SocketMessage message,
Discord.WebSocket.DiscordSocketClient client,
bool isDM);
}
}

View File

@@ -0,0 +1,12 @@
using Discord.WebSocket;
namespace PluginManager.Interfaces
{
public interface DBEvent
{
string name { get; }
string description { get; }
void Start(DiscordSocketClient client);
}
}

View File

@@ -0,0 +1,27 @@
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

@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
namespace PluginManager.Items
{
public class Spinner
{
public bool isSpinning;
public Spinner()
{
isSpinning = false;
}
public async void Start()
{
isSpinning = true;
int cnt = 0;
while (isSpinning)
{
cnt++;
switch (cnt % 4)
{
case 0: Console.Write("/"); break;
case 1: Console.Write("-"); break;
case 2: Console.Write("\\"); break;
case 3: Console.Write("|"); break;
}
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
await Task.Delay(500);
}
}
public void Stop()
{
isSpinning = false;
}
}
}

View File

@@ -0,0 +1,69 @@
using PluginManager.Others;
using System.Collections.Generic;
using System;
using System.IO;
namespace PluginManager.LanguageSystem
{
public class Language
{
public static Language? ActiveLanguage = null;
private static readonly string LanguageFileExtension = ".lng";
private Language(string fileName, Dictionary<string, string> words, string LanguageName)
{
this.fileName = fileName;
this.LanguageName = LanguageName;
LanguageWords = words;
}
public string LanguageName { get; }
public string fileName { get; }
public Dictionary<string, string> LanguageWords { get; }
public static Language? CreateLanguageFromFile(string LanguageFileLocation)
{
if (!LanguageFileLocation.EndsWith(LanguageFileExtension))
{
Console.WriteLine("Failed to load Language from file: " + LanguageFileLocation +
"\nFile extension is not .lng");
return null;
}
string[] lines = File.ReadAllLines(LanguageFileLocation);
var languageName = "Unknown";
var words = new Dictionary<string, string>();
foreach (string line in lines)
{
if (line.StartsWith("#") || line.Length < 4)
continue;
string[] sLine = line.Split('=');
if (sLine[0] == "LANGUAGE_NAME")
{
languageName = sLine[1];
continue;
}
words.Add(sLine[0], sLine[1]);
}
Functions.WriteLogFile("Successfully loaded language: " + languageName + " from file : " +
LanguageFileLocation.Replace('\\', '/'));
return new Language(LanguageFileLocation, words, languageName);
}
public string FormatText(string text, params string[] args)
{
if (ActiveLanguage == null) return text;
int l = args.Length;
for (var i = 0; i < l; i++) text = text.Replace($"{i}", args[i]);
return text;
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using PluginManager.Interfaces;
namespace PluginManager.Loaders
{
public 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);
public onCommandLoaded? OnCommandLoaded;
public onCommandFileLoaded? OnCommandFileLoaded;
public CommandsLoader(string CommandPath, string CommandExtension)
{
CMDPath = CommandPath;
CMDExtension = CommandExtension;
}
public List<DBCommand>? LoadCommands()
{
if (!Directory.Exists(CMDPath))
{
Directory.CreateDirectory(CMDPath);
return null;
}
string[] files = Directory.GetFiles(CMDPath, $"*{CMDExtension}", SearchOption.AllDirectories);
foreach (var file in files)
{
Assembly.LoadFile(Path.GetFullPath(file));
if (OnCommandFileLoaded != null)
OnCommandFileLoaded.Invoke(file);
}
List<DBCommand> plugins = new List<DBCommand>();
try
{
Type interfaceType = typeof(DBCommand);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
foreach (Type type in types)
{
try
{
DBCommand plugin = (DBCommand)Activator.CreateInstance(type)!;
plugins.Add(plugin);
if (OnCommandLoaded != null)
OnCommandLoaded.Invoke(type.FullName!, true, plugin);
}
catch (Exception e)
{
if (OnCommandLoaded != null)
OnCommandLoaded.Invoke(type.FullName!, false, null, e);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return plugins;
}
}
}

View File

@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using PluginManager.Interfaces;
namespace PluginManager.Loaders
{
public 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);
public onEventLoad? EventLoad;
public onEventFileLoaded? EventFileLoaded;
public EventsLoader(string path, string ext)
{
EVPath = path;
EVExtension = ext;
}
public List<DBEvent>? LoadEvents()
{
if (!Directory.Exists(EVPath))
{
Directory.CreateDirectory(EVPath);
return null;
}
string[] files = Directory.GetFiles(EVPath, $"*{EVExtension}", SearchOption.AllDirectories);
foreach (var file in files)
{
Assembly.LoadFile(Path.GetFullPath(file));
if (EventFileLoaded != null)
EventFileLoaded.Invoke(file);
}
List<DBEvent> events = new List<DBEvent>();
try
{
Type interfaceType = typeof(DBEvent);
Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
.ToArray();
foreach (Type type in types)
{
try
{
DBEvent ev = (DBEvent)Activator.CreateInstance(type)!;
events.Add(ev);
if (EventLoad != null)
EventLoad.Invoke(type.FullName!, true, ev);
}
catch (Exception e)
{
if (EventLoad != null)
EventLoad.Invoke(type.FullName!, false, null, e);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return events;
}
}
}

View File

@@ -0,0 +1,86 @@
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Others;
using System;
using System.Collections.Generic;
namespace PluginManager.Loaders
{
public class PluginLoader
{
private DiscordSocketClient client;
public PluginLoader(DiscordSocketClient discordSocketClient)
{
this.client = discordSocketClient;
}
private const string pluginCMDFolder = @"./Data/Plugins/Commands/";
private const string pluginEVEFolder = @"./Data/Plugins/Events/";
private const string pluginCMDExtension = ".dll";
private const string pluginEVEExtension = ".dll";
public static List<DBCommand>? Plugins { get; set; }
public static List<DBEvent>? Events { get; set; }
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);
public CMDLoaded? onCMDLoad;
public EVELoaded? onEVELoad;
public void LoadPlugins()
{
Plugins = new List<DBCommand>();
Events = new List<DBEvent>();
Functions.WriteLogFile("Starting plugin loader...");
if (LanguageSystem.Language.ActiveLanguage != null)
Functions.WriteColorText(LanguageSystem.Language.ActiveLanguage.FormatText(LanguageSystem.Language.ActiveLanguage.LanguageWords["PLUGIN_LOADING_START"]));
//Load commands
CommandsLoader CMDLoader = new CommandsLoader(pluginCMDFolder, pluginCMDExtension);
CMDLoader.OnCommandLoaded += OnCommandLoaded!;
CMDLoader.OnCommandFileLoaded += OnCommandFileLoaded;
Plugins = CMDLoader.LoadCommands();
//Load Events
EventsLoader EVLoader = new EventsLoader(pluginEVEFolder, pluginEVEExtension);
EVLoader.EventLoad += OnEventLoaded!;
EVLoader.EventFileLoaded += EventFileLoaded;
Events = EVLoader.LoadEvents();
}
private void EventFileLoaded(string path)
{
if (path != null)
Functions.WriteLogFile($"[EVENT] Event from file [{path}] has been successfully created !");
}
private void OnCommandFileLoaded(string path)
{
if (path != null)
Functions.WriteLogFile($"[CMD] Command from file [{path}] has been successfully loaded !");
}
private void OnEventLoaded(string typename, bool success, DBEvent eve, Exception exception)
{
if (eve != null && success)
eve.Start(client);
if (onEVELoad != null)
onEVELoad.Invoke(eve!.name, typename, success, exception);
}
private void OnCommandLoaded(string name, bool success, DBCommand command, Exception exception)
{
if (onCMDLoad != null)
onCMDLoad.Invoke(command.Command, name, success, exception);
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using PluginManager.Items;
namespace PluginManager.Online
{
public class Downloader
{
public bool isWorking { get; private set; }
public int percent { get; private set; }
private string fileName;
private string downloadLink;
public Downloader(string fileName, string fileLink)
{
this.downloadLink = fileLink;
this.fileName = fileName;
}
public async Task DownloadFileAsync(string location = @"./Downloads/", string? pluginType = null)
{
Directory.CreateDirectory(location);
if (isWorking) return;
isWorking = true;
percent = 0;
Spinner s = new Spinner();
Console.Write("Downloading:\t\t");
s.Start();
#pragma warning disable SYSLIB0014
WebClient client = new WebClient();
#pragma warning restore SYSLIB0014
client.DownloadFileCompleted += (sender, args) =>
{
isWorking = false;
s.Stop();
var c = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("OK");
Console.ForegroundColor = c;
Console.Write(" !\n");
//Console.WriteLine("Your plugin has been successfully downloaded !");
if (pluginType == "Event/Command" || pluginType == "Command/Event")
{
File.Copy(location + fileName, location + "Commands/" + fileName, true);
File.Move(location + fileName, location + "Events/" + fileName, true);
}
};
string l = "";
if (pluginType == "Command")
l = location + "Commands/" + fileName;
else if (pluginType == "Event")
l = location + "Events/" + fileName;
else l = location + fileName;
try
{
await client.DownloadFileTaskAsync(new Uri(this.downloadLink), l);
}
catch
{
var c = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("FAIL");
Console.ForegroundColor = c;
Console.Write(" !\n");
}
}
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using System.Collections.Generic;
using PluginManager.Others;
namespace PluginManager.Online
{
public class LanguageManager
{
private string link;
public LanguageManager(string link) => this.link = link;
public async Task ListAllLanguages()
{
try
{
#pragma warning disable SYSLIB0014
WebClient client = new WebClient();
#pragma warning restore SYSLIB0014
Stream data = await client.OpenReadTaskAsync(link);
string[] lines = (await new StreamReader(data).ReadToEndAsync()).Split('\n');
List<string[]> info = new List<string[]>();
info.Add(new string[] { "-", "-" });
info.Add(new string[] { "Language Name", "File Size" });
info.Add(new string[] { "-", "-" });
foreach (var line in lines)
{
if (line.Length <= 2) continue;
string[] d = line.Split(',');
if (d[3].Contains("cp") || d[3].Contains("CrossPlatform"))
info.Add(new string[] { d[0], d[1] });
}
info.Add(new string[] { "-", "-" });
Functions.FormatAndAlignTable(info);
}
catch (Exception exception)
{
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
Others.Functions.WriteErrFile(exception.ToString());
}
}
public async Task<string[]?> GetDownloadLink(string langName)
{
try
{
#pragma warning disable SYSLIB0014
WebClient client = new WebClient();
#pragma warning restore SYSLIB0014
Stream data = await client.OpenReadTaskAsync(link);
string[] lines = (await new StreamReader(data).ReadToEndAsync()).Split('\n');
foreach (var line in lines)
{
if (line.Length <= 2) continue;
string[] d = line.Split(',');
if (d[0].Equals(langName) && (d[3].Contains("cp") || d[3].Contains("CrossPlatform")))
return new string[] { d[2], d[3] };
}
}
catch (Exception exception)
{
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
Others.Functions.WriteErrFile(exception.ToString());
}
return null;
}
}
}

View File

@@ -0,0 +1,108 @@
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Collections.Generic;
using PluginManager.Others;
namespace PluginManager.Online
{
public class PluginsManager
{
public string PluginsLink { get; private set; }
public PluginsManager(string link)
{
PluginsLink = link;
}
public async Task ListAvailablePlugins()
{
try
{
#pragma warning disable SYSLIB0014
WebClient client = new WebClient();
#pragma warning restore SYSLIB0014
Stream s = await client.OpenReadTaskAsync(PluginsLink);
string text = await new StreamReader(s).ReadToEndAsync();
List<string[]> data = new List<string[]>();
var op = Functions.GetOperatinSystem();
string[] lines = text.Split('\n');
int len = lines.Length;
string[] titles = { "Name", "Description", "Plugin Type" };
data.Add(new string[] { "-", "-", "-" });
data.Add(titles);
data.Add(new string[] { "-", "-", "-" });
for (int i = 0; i < len; i++)
{
if (lines[i].Length <= 2) continue;
string[] content = lines[i].Split(',');
string[] display = new string[3];
if (op == PluginManager.Others.OperatingSystem.WINDOWS)
{
if (content[4].Contains("Windows"))
{
display[0] = content[0];
display[1] = content[1];
display[2] = content[2];
data.Add(display);
continue;
}
}
else if (op == PluginManager.Others.OperatingSystem.LINUX)
{
if (content[4].Contains("Linux"))
{
display[0] = content[0];
display[1] = content[1];
display[2] = content[2];
data.Add(display);
continue;
}
}
}
data.Add(new string[] { "-", "-", "-" });
Functions.FormatAndAlignTable(data);
}
catch (Exception exception)
{
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
Others.Functions.WriteErrFile(exception.ToString());
}
}
public async Task<string[]> GetPluginLinkByName(string name)
{
try
{
#pragma warning disable SYSLIB0014
WebClient client = new WebClient();
#pragma warning restore SYSLIB0014
Stream s = await client.OpenReadTaskAsync(PluginsLink);
string text = await new StreamReader(s).ReadToEndAsync();
string[] lines = text.Split('\n');
int len = lines.Length;
for (int i = 0; i < len; i++)
{
string[] contents = lines[i].Split(',');
if (contents[0] == name)
return new string[] { contents[2], contents[3] };
}
}
catch (Exception exception)
{
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
Others.Functions.WriteErrFile(exception.ToString());
}
return new string[] { null!, null! };
}
}
}

View File

@@ -0,0 +1,15 @@
using Discord;
using System.Threading.Tasks;
namespace PluginManager.Others
{
public static class ChannelManagement
{
public static IGuildChannel GetTextChannel(this IGuild server, string name) => server.GetTextChannel(name);
public static IGuildChannel GetVoiceChannel(this IGuild server, string name) => server.GetVoiceChannel(name);
public static async Task<IDMChannel> GetDMChannel(IGuildUser user) => await user.CreateDMChannelAsync();
public static IChannel GetChannel(IMessage message) => message.Channel;
}
}

View File

@@ -0,0 +1,44 @@
namespace PluginManager.Others
{
public class Cryptography
{
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;
}
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;
}
public 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;
}
}
}

View File

@@ -0,0 +1,14 @@
namespace PluginManager.Others
{
/// <summary>
/// A list of operating systems
/// </summary>
public enum OperatingSystem
{ WINDOWS, LINUX, MAC_OS, UNKNOWN }
public enum Error
{ UNKNOWN_ERROR, GUILD_NOT_FOUND, }
public enum OutputLogLevel { NONE, INFO, WARNING, ERROR, CRITICAL }
}

View File

@@ -0,0 +1,37 @@
using System;
namespace PluginManager.Others.Exceptions
{
[System.Serializable]
public class APIException : Exception
{
public string? Function { get; }
public Error? ErrorCode { get; }
public APIException(string message, string? function, Error? errorCode) : base(message)
{
ErrorCode = errorCode;
Function = function;
}
public APIException(string message, string? function) : base(message)
{
ErrorCode = Error.UNKNOWN_ERROR;
Function = function;
}
public APIException(string message) : base(message)
{
ErrorCode = Error.UNKNOWN_ERROR;
Function = "Unspecified_Function";
}
public void Print()
{
Console.WriteLine("Message Content: " + Message);
Console.WriteLine("Function: " + Function);
Console.WriteLine("Error Code: " + ErrorCode.ToString());
}
}
}

View File

@@ -0,0 +1,300 @@
using System.IO.Compression;
using System.IO;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
namespace PluginManager.Others
{
public static class Functions
{
/// <summary>
/// The location for the Resources folder
/// </summary>
public static readonly string dataFolder = @"./Data/Resources/";
/// <summary>
/// The location for all logs
/// </summary>
public static readonly string logFolder = @"./Output/Logs/";
/// <summary>
/// The location for all errors
/// </summary>
public static readonly string errFolder = @"./Output/Errors/";
/// <summary>
/// The location for all languages
/// </summary>
public static readonly string langFolder = @"./Data/Languages/";
/// <summary>
/// Archives folder
/// </summary>
public static readonly string pakFolder = @"./Data/Resources/PAKS/";
/// <summary>
/// The mark that the line is a comment
/// </summary>
private static readonly char commentMark = '#';
/// <summary>
/// Read data from file
/// </summary>
/// <param name="fileName">File name</param>
/// <param name="Code">Setting name</param>
/// <param name="separator">Separator between setting key code and its value</param>
/// <returns>The value of the specified setting key code in the specified file (STRING)</returns>
public static string? readCodeFromFile(string fileName, string Code, char separator)
=> File.ReadAllLines(fileName)
.Where(p => p.StartsWith(Code) && !p.StartsWith(commentMark.ToString()))
.First().Split(separator)[1] ?? null;
/// <summary>
/// Read data from a file that is inside an archive (ZIP format)
/// </summary>
/// <param name="FileName">The file name that is inside the archive or its full path</param>
/// <param name="archFile">The archive location from the PAKs folder</param>
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
public static async Task<string?> ReadFromPakAsync(string FileName, string archFile)
{
archFile = pakFolder + archFile;
Directory.CreateDirectory(pakFolder);
if (!File.Exists(archFile))
{
throw new Exception("Failed to load file !");
}
string? textValue = null;
var fs = new FileStream(archFile, FileMode.Open);
var zip = new ZipArchive(fs, ZipArchiveMode.Read);
foreach (var entry in zip.Entries)
{
if (entry.Name == FileName || entry.FullName == FileName)
{
Stream s = entry.Open();
StreamReader reader = new StreamReader(s);
textValue = await reader.ReadToEndAsync();
reader.Close();
s.Close();
fs.Close();
break;
}
}
return textValue;
}
/// <summary>
/// Write logs to file
/// </summary>
/// <param name="LogMessage">The message to be wrote</param>
public static void WriteLogFile(string LogMessage)
{
string logsPath = logFolder + "Log.txt";
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
File.AppendAllText(logsPath, LogMessage + " \n");
}
/// <summary>
/// Write error to file
/// </summary>
/// <param name="ErrMessage">The message to be wrote</param>
public static void WriteErrFile(string ErrMessage)
{
string errPath = errFolder + "Error.txt";
if (!Directory.Exists(errFolder))
Directory.CreateDirectory(errFolder);
File.AppendAllText(errPath, ErrMessage + " \n");
}
/// <summary>
/// Write to settings file
/// </summary>
/// <param name="file">The settings file path</param>
/// <param name="Code">The Key value of the setting</param>
/// <param name="newValue">The new value of the settings</param>
/// <param name="separator">The separator between the key and the value</param>
public static void WriteToSettings(string file, string Code, string newValue, char separator)
{
string[] lines = File.ReadAllLines(file);
File.Delete(file);
bool ok = false;
foreach (var line in lines)
if (line.StartsWith(Code))
{
File.AppendAllText(file, Code + separator + newValue + "\n"); ok = true;
}
else File.AppendAllText(file, line + "\n");
if (!ok)
File.AppendAllText(file, Code + separator + newValue + "\n");
}
/// <summary>
/// Merge one array of strings into one string
/// </summary>
/// <param name="s">The array of strings</param>
/// <param name="indexToStart">The index from where the merge should start (included)</param>
/// <returns>A string built based on the array</returns>
public static string MergeStrings(this string[] s, int indexToStart)
{
string r = "";
int len = s.Length;
if (len <= indexToStart) return "";
for (int i = indexToStart; i < len - 1; ++i)
{
r += s[i] + " ";
}
r += s[len - 1];
return r;
}
/// <summary>
/// Get the Operating system you are runnin on
/// </summary>
/// <returns>An Operating system</returns>
public static OperatingSystem GetOperatinSystem()
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows)) return OperatingSystem.WINDOWS;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux)) return OperatingSystem.LINUX;
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX)) return OperatingSystem.MAC_OS;
return OperatingSystem.UNKNOWN;
}
/// <summary>
/// A way to create a table based on input data
/// EpicWings (Pasca Robert) este cel mai bun
/// Special thanks to Kami-sama <3
/// </summary>
/// <param name="data">The List of arrays of strings that represent the rows.</param>
public static void FormatAndAlignTable(List<string[]> data)
{
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
}
}
/// <summary>
/// Write the text using color options( &g-green; &b-blue; &r-red; &c-clear; )
/// </summary>
/// <param name="text">The text</param>
public static void WriteColorText(string text)
{
string[] words = text.Split(' ');
Dictionary<string, ConsoleColor> colors = new Dictionary<string, ConsoleColor>()
{
{"&g", ConsoleColor.Green },
{"&b", ConsoleColor.Blue },
{"&r", ConsoleColor.Red },
{"&c", Console.ForegroundColor }
};
foreach (string word in words)
{
if (word.Length >= 2)
{
string prefix = word.Substring(0, 2);
if (colors.ContainsKey(prefix))
Console.ForegroundColor = colors[prefix];
}
string m = word.Replace("&g", "").Replace("&b", "").Replace("&r", "").Replace("&c", "");
Console.Write(m + " ");
}
Console.Write('\n');
}
/// <summary>
/// Write setting
/// </summary>
/// <param name="SettingName">The full path to the setting</param>
/// <param name="NewValue">The new Value</param>
public static void WriteToSettingsFast(string SettingName, string NewValue)
{
string path = dataFolder; // Resources/
string[] args = SettingName.Split('.');
int len = args.Length;
if (len < 2) return;
for (int i = 0; i < len - 2; i++)
path += args[i] + "/";
path += args[len - 2] + ".txt";
WriteToSettings(path, args[len - 1].Replace('_', ' '), NewValue, '=');
}
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();
}
}
}

View File

@@ -0,0 +1,20 @@
using Discord;
using Discord.WebSocket;
using System.Linq;
namespace PluginManager.Others
{
public static class Permissions
{
public static bool hasPermission(this IRole role, GuildPermission permission) => role.Permissions.Has(permission);
public static bool hasRole(this SocketGuildUser user, IRole role) => user.Roles.Contains(role);
public static bool hasPermission(this SocketGuildUser user, GuildPermission permission)
=> user.Roles.Where(role => role.hasPermission(permission)).Any();
}
}

View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.1.0" />
</ItemGroup>
</Project>