From 497cba65caa38c9d07961d9ed7de06795010f84b Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Sat, 7 May 2022 13:56:31 +0300 Subject: [PATCH] --- DiscordBot/Program.cs | 11 +- PluginManager/Loaders/PluginLoader.cs | 6 +- PluginManager/Online/LanguageManager.cs | 5 - PluginManager/Online/PluginsManager.cs | 2 +- PluginManager/Online/ServerCom.cs | 30 +----- PluginManager/Others/Console Utilities.cs | 2 - README.md | 125 ++++++++++++++++++++++ 7 files changed, 141 insertions(+), 40 deletions(-) create mode 100644 README.md diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 92902d4..dad195a 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -9,11 +9,10 @@ using PluginManager.Others; using PluginManager.Loaders; using PluginManager.LanguageSystem; using PluginManager.Online; + using System.Diagnostics; using System.Collections.Generic; using System.Linq; -using System.Threading; - namespace DiscordBot { public class Program @@ -37,7 +36,7 @@ namespace DiscordBot Directory.CreateDirectory("./Data/Languages"); Directory.CreateDirectory("./Data/Plugins/Commands"); Directory.CreateDirectory("./Data/Plugins/Events"); - if (!File.Exists("./Data/Resources/DiscordBotCore.data") || Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN", '=')!.Length != 59) + if (!File.Exists("./Data/Resources/DiscordBotCore.data") || (Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN", '=').Length != 59 && Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN", '=').Length != 70)) { File.WriteAllText("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN=token\nBOT_PREFIX=!\n"); while (true) @@ -45,7 +44,7 @@ namespace DiscordBot Console.WriteLine("Please insert your token: "); Console.Write("TOKEN: "); string botToken = Console.ReadLine(); - if (botToken.Length == 59) + if (botToken.Length == 59 || botToken.Length == 70) { string prefix = Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_PREFIX", '='); if (prefix == string.Empty || prefix == null) @@ -96,7 +95,9 @@ namespace DiscordBot Console_Utilities.WriteColorText("&mConsole > &c", false); string[] data = Console.ReadLine().Split(' '); - if (data[0].Length < 2) continue; + if (data[0].Length < 2) + continue; // The input command is less then 2 characters long + switch (data[0]) { case "shutdown": diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index 3f6ed97..36a192a 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -61,7 +61,11 @@ namespace PluginManager.Loaders Functions.WriteLogFile("Starting plugin loader..."); if (LanguageSystem.Language.ActiveLanguage != null) - Console_Utilities.WriteColorText(LanguageSystem.Language.ActiveLanguage.FormatText(LanguageSystem.Language.ActiveLanguage.LanguageWords["PLUGIN_LOADING_START"])); + Console_Utilities.WriteColorText( + LanguageSystem.Language.ActiveLanguage.FormatText( + LanguageSystem.Language.ActiveLanguage.LanguageWords["PLUGIN_LOADING_START"] + ) + ); //Load commands CommandsLoader CMDLoader = new CommandsLoader(pluginCMDFolder, pluginCMDExtension); diff --git a/PluginManager/Online/LanguageManager.cs b/PluginManager/Online/LanguageManager.cs index 71eacdd..408315c 100644 --- a/PluginManager/Online/LanguageManager.cs +++ b/PluginManager/Online/LanguageManager.cs @@ -27,11 +27,6 @@ namespace PluginManager.Online 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 list = await ServerCom.ReadTextFromFile(link); string[] lines = list.ToArray(); diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 1de837e..344d3fd 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -63,7 +63,7 @@ namespace PluginManager.Online continue; } } - else if (op == PluginManager.Others.OperatingSystem.LINUX) + else if (op == Others.OperatingSystem.LINUX) { if (content[4].Contains("Linux")) { diff --git a/PluginManager/Online/ServerCom.cs b/PluginManager/Online/ServerCom.cs index d53e833..edd7e58 100644 --- a/PluginManager/Online/ServerCom.cs +++ b/PluginManager/Online/ServerCom.cs @@ -57,11 +57,8 @@ namespace PluginManager.Online bool isDownloading = true; int c_progress = 0; - long secondsPast = 0; - long m_dwBytes = 0; - double c_downloadSpeed = 0f; - string c_downloadUnit = "MB"; - + //long m_dwBytes = 0; + Others.Console_Utilities.ProgressBar pbar = new Others.Console_Utilities.ProgressBar(100, ""); IProgress progress = new Progress(percent => @@ -71,39 +68,20 @@ namespace PluginManager.Online IProgress progress_downloaded = new Progress(downloadedBytes => { - m_dwBytes = downloadedBytes; + //m_dwBytes = downloadedBytes; }); Task updateProgressBarTask = new Task(async () => { while (isDownloading) { - //pbar.Update(c_progress, c_downloadSpeed, c_downloadUnit); pbar.Update(c_progress); if (c_progress == 100) break; System.Threading.Thread.Sleep(500); } }); - - Task calculateDownloadSpeed = new Task(async () => - { - - while (isDownloading) - { - secondsPast++; - c_downloadSpeed = m_dwBytes / secondsPast; - - c_downloadSpeed /= 1024; // in KB - c_downloadSpeed /= 1024; // in MB - - c_downloadSpeed = Math.Round(c_downloadSpeed, 2, MidpointRounding.AwayFromZero); - - System.Threading.Thread.Sleep(1000); - } - }); - - // new System.Threading.Thread(calculateDownloadSpeed.Start).Start(); + new System.Threading.Thread(updateProgressBarTask.Start).Start(); await DownloadFileAsync(URL, location, progress, progress_downloaded); diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index 8c0f230..80e3330 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -96,7 +96,6 @@ namespace PluginManager.Others foreach (string[] row in data) { - //Console.Write("\t"); if (row[0][0] == tableLine) Console.Write(tableCross); else Console.Write(tableWall); for (int l = 0; l < row.Length; l++) @@ -135,7 +134,6 @@ namespace PluginManager.Others /// /// Write the text using color options( &g-green; &b-blue; &r-red; &c-clear; ) - /// /// /// The text public static void WriteColorText(string text, bool appendNewLine = true) diff --git a/README.md b/README.md new file mode 100644 index 0000000..45ad2e1 --- /dev/null +++ b/README.md @@ -0,0 +1,125 @@ +# Seth Discord Bot + +This is a Discord Bot made with C# that accepts plugins as extensions for more commands and events. All basic commands are built in already in the PluginManager class library. +This project is based on .NET 6 (C#) and [Discord.Net](https://github.com/discord-net/Discord.Net) + + +## Plugins +#### Requirements: +- [Visual Studio](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&channel=Release&version=VS2022&source=VSLandingPage&cid=2030&passive=false) +- .NET 6 (downloaded with Visual Studio) + +Plugin Types: +1. Commands +2. Events + + +### How to create a plugin + +First of all, Create a new project (class library) in Visual Studio. +![Imgur Image](https://i.imgur.com/KUqzKsB.png) + +![Imgur Image](https://i.imgur.com/JzpEViR.png) + +![Imgur Image](https://i.imgur.com/vtoEepX.png) + +![Imgur Image](https://i.imgur.com/ceaVR2R.png) + +Now, let's add the PluginManager reference. It can be found inside the bot's main folder under +`DiscordBot/bin/Debug/net6.0/PluginManager.dll` or `PluginManager/bin/Debug/net6.0/PluginManager.dll` +after one successfull build. + +![Imgur Image](https://i.imgur.com/UMSitk4.png) + +![Imgur Image](https://i.imgur.com/GEjShdl.png) + +1. Commands + +Commands are loaded when all plugins are loaded into memory. When an user executes the command, only then the Execute function is called. +Commands are plugins that allow users to interact with them. +Here is an example of class that is a command class +```cs +using Discord.Commands; +using Discord.WebSocket; + +using PluginManager.Interfaces; + +namespace CMD_Utils +{ + class FlipCoin : DBCommand + { + public string Command => "flip"; + + public string Description => "Flip a coin"; + + public string Usage => "flip"; + + public bool canUseDM => true; + + public bool canUseServer => true; + + public bool requireAdmin => false; + + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + { + System.Random random = new System.Random(); + int r = random.Next(1, 3); + if (r == 1) + await message.Channel.SendMessageAsync("Heads"); + else await message.Channel.SendMessageAsync("Tails"); + } + } +} + +``` +#### Code description: +- Command - The keyword that triggers the execution for the command. This is what players must type in order to execute your command +- Description - The description of your command. Can be anything you like +- Usage - The usage of your command. This is what `help [Command]` command will display +- canUseDM - true if you plan to let users execute this command in DM chat with bot +- canUseServer - true if you plan to let the users execute this command in a server chat +- requireAdmin - true if this command requres an user with Administrator permission in the server +- Execute () - the function of your command. + - context - the command context + - message - the message itself + - client - the discord bot client + - isDM - true if the message was sent from DM chat + +From here on, start coding. When your plugin is done, build it as any DLL project then add it to the following path +`{bot_executable}/Data/Plugins/Commands//yourDLLName.dll` +Then, reload bot and execute command `lp` in bot's console. The plugin should be loaded into memory or an error is thrown if not. If an error is thrown, then +there is something wrong in your command's code. + +2. Events + +Events are loaded when all plugins are loaded. At the moment when they are loaded, the Start function is called. +Events are used if you want the bot to do something when something happens in server. The following example shows you how to catch when a user joins the server +and send to that user a DM message with `Welcome to server !`. + +```cs +using PluginManager.Others; +using PluginManager.Interfaces; + +public class OnUserJoin : DBEvent +{ + public string name => "MyEvent"; + + public string description => "This is a demo event"; + + public async void Start(Discord.WebSocket.DiscordSocketClient client) + { + Console.WriteLine($"Hello World from {name}"); + + client.UserJoined += async (user) => { + await (await user.CreateDMChannelAsync()).SendMessageAsync("Welcome to server !"); + }; + } +} +``` + +#### Code description: +- name - The name of your event. It will appear in console when it loads +- description - The description of your event +- Start() - The main body of your event. This is executed when the bot loads all plugins + - client - the discord bot client +