From d20cb62139f3b4fd29acd8748b232b066c98f7ce Mon Sep 17 00:00:00 2001 From: Andrei Date: Sat, 1 Apr 2023 16:14:04 +0300 Subject: [PATCH] Updated initial setup --- DiscordBot/Program.cs | 177 +++++------------- PluginManager/Logger.cs | 29 +++ PluginManager/Others/Enums.cs | 11 +- PluginManager/PluginManager.csproj | 1 - .../Controls/ConsoleOption.cs | 15 ++ .../WindowManagement/Controls/Label.cs | 47 +++++ .../WindowManagement/Controls/TextBox.cs | 26 +++ PluginManager/WindowManagement/InputBox.cs | 61 ++++++ PluginManager/WindowManagement/MessageBox.cs | 70 +++++++ SethDiscordBot.sln | 6 + test/Program.cs | 16 ++ test/test.csproj | 14 ++ 12 files changed, 338 insertions(+), 135 deletions(-) create mode 100644 PluginManager/WindowManagement/Controls/ConsoleOption.cs create mode 100644 PluginManager/WindowManagement/Controls/Label.cs create mode 100644 PluginManager/WindowManagement/Controls/TextBox.cs create mode 100644 PluginManager/WindowManagement/InputBox.cs create mode 100644 PluginManager/WindowManagement/MessageBox.cs create mode 100644 test/Program.cs create mode 100644 test/test.csproj diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 2865814..2e90dd2 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -12,8 +13,7 @@ using PluginManager.Items; using PluginManager.Online; using PluginManager.Online.Helpers; using PluginManager.Others; - -using Terminal.Gui; +using PluginManager.WindowManagement; using OperatingSystem = PluginManager.Others.OperatingSystem; @@ -41,7 +41,7 @@ public class Program Config.Data["prefix"]?.Length != 1 || (args.Length == 1 && args[0] == "/reset")) { - GenerateStartUI("First time setup. Please fill the following with your discord bot data.\nThis are saved ONLY on YOUR computer."); + GenerateStartUI(); } HandleInput(args).Wait(); @@ -336,145 +336,56 @@ public class Program Console.Clear(); } - public static void GenerateStartUI(string titleMessage) + public static void GenerateStartUI() { - Application.Init(); - var top = Application.Top; - var win = new Window("Discord Bot Config - " + Assembly.GetExecutingAssembly().GetName().Version) - { - X = 0, - Y = 1, - Width = Dim.Fill(), - Height = Dim.Fill() - }; + InputBox box = new InputBox(); + box.Title = "Discord Bot Config - " + Assembly.GetExecutingAssembly().GetName().Version; + box.Message = "Let's setup the bot. Please go through this setup so that you can run the bot.\n\n"; + box.AddLabel("Note:All the information collected here will only be stored locally (on your machine) at the following path: ", TextType.WARNING); + box.AddLabel("/Data/Resources/Config.json", TextType.SUCCESS); - top.Add(win); - - var labelInfo = new Label(titleMessage) - { - X = Pos.Center(), - Y = 2 - }; - - - var labelToken = new Label("Please insert your token here: ") - { - X = 5, - Y = 5 - }; - - var textFiledToken = new TextField(Config.Data["token"] ?? "") - { - X = Pos.Left(labelToken) + labelToken.Text.Length + 2, - Y = labelToken.Y, - Width = 70 - }; - - var labelPrefix = new Label("Please insert your prefix here: ") - { - X = 5, - Y = 8 - }; - var textFiledPrefix = new TextField(Config.Data["prefix"] ?? "") - { - X = Pos.Left(labelPrefix) + labelPrefix.Text.Length + 2, - Y = labelPrefix.Y, - Width = 1 - }; - - var labelServerid = new Label("Please insert your server id here (optional): ") - { - X = 5, - Y = 11 - }; - var textFiledServerID = new TextField(Config.Data["ServerID"] ?? "") - { - X = Pos.Left(labelServerid) + labelServerid.Text.Length + 2, - Y = labelServerid.Y, - Width = 18 - }; - - var button = new Button("Submit") - { - X = Pos.Center() - 10, - Y = 16 - }; - - var button2 = new Button("License") - { - X = Pos.Center() + 10, - Y = 16 - }; - - var button3 = new Button("ⓘ") - { - X = Pos.Left(textFiledServerID) + 20, - Y = textFiledServerID.Y - }; - - Console.CancelKeyPress += (sender, e) => { top.Running = false; }; - - button.Clicked += () => - { - var passMessage = ""; - if (textFiledToken.Text.Length != 70 && textFiledToken.Text.Length != 59) - passMessage += "Invalid token, "; - if (textFiledPrefix.Text.ContainsAny("0123456789/\\ ") || textFiledPrefix.Text.Length != 1) - passMessage += "Invalid prefix, "; - if (textFiledServerID.Text.Length != 18 && textFiledServerID.Text.Length > 0) - passMessage += "Invalid serverID"; - - if (passMessage != "") + box.AddOption("Bot Token", (token) => { + if (token.Length != 70 && token.Length != 59) { - MessageBox.ErrorQuery("Discord Bot Settings", - "Failed to pass check. Invalid information given:\n" + passMessage, "Retry"); - return; + Console.WriteLine("The token is invalid !"); + return false; } + return true; + }); - Config.Data.Add("ServerID", (string)textFiledServerID.Text); - Config.Data.Add("token", (string)textFiledToken.Text); - Config.Data.Add("prefix", (string)textFiledPrefix.Text); - - MessageBox.Query("Discord Bot Settings", "Successfully saved config !\nJust start the bot :D", - "Start :D"); - top.Running = false; - }; - - button2.Clicked += async () => - { - var license = - await ServerCom.ReadTextFromURL( - "https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/LICENSE.txt"); - var ProductLicense = - "Seth Discord Bot\n\nDeveloped by Wizzy#9181\nThis application can be used and modified by anyone. Plugin development for this application is also free and supported"; - var r = MessageBox.Query("Discord Bot Settings", ProductLicense, "Close", "Read about libraries used"); - if (r == 1) + box.AddOption("Bot prefix (should be one character long)", (prefix) => { + if (int.TryParse(prefix, out int value)) { - var i = 0; - while (i < license.Count) - { - var print_message = license[i++] + "\n"; - for (; i < license.Count && !license[i].StartsWith("-----------"); i++) - print_message += license[i] + "\n"; - if (print_message.Contains("https://")) - print_message += "\n\nCTRL + Click on a link to open it"; - if (MessageBox.Query("Licenses", print_message, "Next", "Quit") == 1) break; - } + Console.WriteLine("The prefix can not be a number"); + return false; } - }; - button3.Clicked += () => - { - MessageBox.Query("Discord Bot Settings", - "Server ID can be found in Server settings => Widget => Server ID", - "Close"); - }; + if (prefix.Length != 1) + { + Console.WriteLine("The bot does not support longer prefixes"); + return false; + } - win.Add(labelInfo, labelPrefix, labelServerid, labelToken); - win.Add(textFiledToken, textFiledPrefix, textFiledServerID, button3); - win.Add(button, button2); - Application.Run(); - Application.Shutdown(); + return true; + + }); + + + box.AddOption("Server ID (Optional => Press ENTER to leave empty)\nIf you let the Server ID field option empty, some plugins may not work", (servId) => { + if (servId.Length != 18 && servId.Length > 0) + { + Console.WriteLine("The Server ID is invalid"); + return false; + } + + return true; + }); + + List result = box.Show(); + + Config.Data.Add("ServerID", result[2]); + Config.Data.Add("token", result[0]); + Config.Data.Add("prefix", result[1]); } } \ No newline at end of file diff --git a/PluginManager/Logger.cs b/PluginManager/Logger.cs index a63da13..68d79d9 100644 --- a/PluginManager/Logger.cs +++ b/PluginManager/Logger.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Numerics; +using PluginManager.Others; using Discord; @@ -118,6 +119,34 @@ namespace PluginManager LogEvent?.Invoke(message); Console.ForegroundColor = oldColor; } + public static void SetConsoleColor(TextType type) + { + if(!isConsole) return; + switch (type) + { + case TextType.NORMAL: + Console.ForegroundColor = ConsoleColor.White; + break; + case TextType.ERROR: + Console.ForegroundColor = ConsoleColor.Red; + break; + case TextType.WARNING: + Console.ForegroundColor = ConsoleColor.Yellow; + break; + case TextType.SUCCESS: + Console.ForegroundColor = ConsoleColor.Green; + break; + default: + Console.ForegroundColor = ConsoleColor.White; + break; + } + } + + public static void ResetConsoleColor() + { + if (!isConsole) return; + Console.ForegroundColor = ConsoleColor.White; + } /// /// Write logs to file diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index 9ecd975..1e71fa6 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -59,4 +59,13 @@ public enum ProgressBarType { NORMAL, NO_END -} \ No newline at end of file +} + +public enum TextType +{ + NORMAL, + ERROR, + WARNING, + SUCCESS +} + diff --git a/PluginManager/PluginManager.csproj b/PluginManager/PluginManager.csproj index da82f6f..42446cf 100644 --- a/PluginManager/PluginManager.csproj +++ b/PluginManager/PluginManager.csproj @@ -14,6 +14,5 @@ - \ No newline at end of file diff --git a/PluginManager/WindowManagement/Controls/ConsoleOption.cs b/PluginManager/WindowManagement/Controls/ConsoleOption.cs new file mode 100644 index 0000000..7a212f2 --- /dev/null +++ b/PluginManager/WindowManagement/Controls/ConsoleOption.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace PluginManager.WindowManagement.Controls +{ + public class ConsoleOption + { + public string Text { get; set; } + public byte Index { get; set; } + + public Action Action { get; set; } + } +} \ No newline at end of file diff --git a/PluginManager/WindowManagement/Controls/Label.cs b/PluginManager/WindowManagement/Controls/Label.cs new file mode 100644 index 0000000..e7d41ce --- /dev/null +++ b/PluginManager/WindowManagement/Controls/Label.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using PluginManager.Others; + +namespace PluginManager.WindowManagement.Controls +{ + public class Label + { + public string Text { get; set; } + public TextType Type { get; set; } = TextType.NORMAL; + + public void Show() + { + Logger.SetConsoleColor(Type); + Console.WriteLine(Text); + Logger.ResetConsoleColor(); + } + + public void Show(int x, int y) + { + Logger.SetConsoleColor(Type); + Console.SetCursorPosition(x, y); + Console.WriteLine(Text); + + Logger.ResetConsoleColor(); + } + + public void Show(int x, int y, ConsoleColor color) + { + Console.SetCursorPosition(x, y); + Console.ForegroundColor = color; + Console.WriteLine(Text); + Console.ResetColor(); + } + + public void Show(int x, int y, ConsoleColor color, ConsoleColor background) + { + Console.SetCursorPosition(x, y); + Console.ForegroundColor = color; + Console.BackgroundColor = background; + Console.WriteLine(Text); + Console.ResetColor(); + } + } +} \ No newline at end of file diff --git a/PluginManager/WindowManagement/Controls/TextBox.cs b/PluginManager/WindowManagement/Controls/TextBox.cs new file mode 100644 index 0000000..2a58b6c --- /dev/null +++ b/PluginManager/WindowManagement/Controls/TextBox.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace PluginManager.WindowManagement.Controls +{ + public class TextBox + { + public Label Label { get; set; } + public string Text { get; private set; } + public Func IsValid { get; set; } + + public bool SetText(string text) + { + if(IsValid(text)) + { + Text = text; + return true; + } + + return false; + + } + } +} \ No newline at end of file diff --git a/PluginManager/WindowManagement/InputBox.cs b/PluginManager/WindowManagement/InputBox.cs new file mode 100644 index 0000000..34ee93d --- /dev/null +++ b/PluginManager/WindowManagement/InputBox.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using PluginManager.WindowManagement.Controls; +using PluginManager.Others; + +namespace PluginManager.WindowManagement +{ + public class InputBox + { + public string Title { get; set; } + public string Message { get; set; } + + private List options = new List(); + private List