Updated initial setup

This commit is contained in:
2023-04-01 16:14:04 +03:00
parent f2418d0395
commit d20cb62139
12 changed files with 338 additions and 135 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -12,8 +13,7 @@ using PluginManager.Items;
using PluginManager.Online; using PluginManager.Online;
using PluginManager.Online.Helpers; using PluginManager.Online.Helpers;
using PluginManager.Others; using PluginManager.Others;
using PluginManager.WindowManagement;
using Terminal.Gui;
using OperatingSystem = PluginManager.Others.OperatingSystem; using OperatingSystem = PluginManager.Others.OperatingSystem;
@@ -41,7 +41,7 @@ public class Program
Config.Data["prefix"]?.Length != 1 || Config.Data["prefix"]?.Length != 1 ||
(args.Length == 1 && args[0] == "/reset")) (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(); HandleInput(args).Wait();
@@ -336,145 +336,56 @@ public class Program
Console.Clear(); Console.Clear();
} }
public static void GenerateStartUI(string titleMessage) public static void GenerateStartUI()
{ {
Application.Init(); InputBox box = new InputBox();
var top = Application.Top; box.Title = "Discord Bot Config - " + Assembly.GetExecutingAssembly().GetName().Version;
var win = new Window("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);
X = 0, box.AddLabel("<executable path>/Data/Resources/Config.json", TextType.SUCCESS);
Y = 1,
Width = Dim.Fill(),
Height = Dim.Fill()
};
top.Add(win); box.AddOption("Bot Token", (token) => {
if (token.Length != 70 && token.Length != 59)
var labelInfo = new Label(titleMessage)
{ {
X = Pos.Center(), Console.WriteLine("The token is invalid !");
Y = 2 return false;
};
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 != "")
{
MessageBox.ErrorQuery("Discord Bot Settings",
"Failed to pass check. Invalid information given:\n" + passMessage, "Retry");
return;
} }
return true;
});
Config.Data.Add("ServerID", (string)textFiledServerID.Text); box.AddOption("Bot prefix (should be one character long)", (prefix) => {
Config.Data.Add("token", (string)textFiledToken.Text); if (int.TryParse(prefix, out int value))
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 = Console.WriteLine("The prefix can not be a number");
await ServerCom.ReadTextFromURL( return false;
"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)
{
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;
} }
}
};
button3.Clicked += () => if (prefix.Length != 1)
{ {
MessageBox.Query("Discord Bot Settings", Console.WriteLine("The bot does not support longer prefixes");
"Server ID can be found in Server settings => Widget => Server ID", return false;
"Close"); }
};
win.Add(labelInfo, labelPrefix, labelServerid, labelToken); return true;
win.Add(textFiledToken, textFiledPrefix, textFiledServerID, button3);
win.Add(button, button2); });
Application.Run();
Application.Shutdown();
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<string> result = box.Show();
Config.Data.Add("ServerID", result[2]);
Config.Data.Add("token", result[0]);
Config.Data.Add("prefix", result[1]);
} }
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using PluginManager.Others;
using Discord; using Discord;
@@ -118,6 +119,34 @@ namespace PluginManager
LogEvent?.Invoke(message); LogEvent?.Invoke(message);
Console.ForegroundColor = oldColor; 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;
}
/// <summary> /// <summary>
/// Write logs to file /// Write logs to file

View File

@@ -60,3 +60,12 @@ public enum ProgressBarType
NORMAL, NORMAL,
NO_END NO_END
} }
public enum TextType
{
NORMAL,
ERROR,
WARNING,
SUCCESS
}

View File

@@ -14,6 +14,5 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net" Version="3.8.1" /> <PackageReference Include="Discord.Net" Version="3.8.1" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" /> <PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
<PackageReference Include="Terminal.Gui" Version="1.8.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -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; }
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<string, bool> IsValid { get; set; }
public bool SetText(string text)
{
if(IsValid(text))
{
Text = text;
return true;
}
return false;
}
}
}

View File

@@ -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<TextBox> options = new List<TextBox>();
private List<Label> labels = new List<Label>();
private string InputStr = "=> ";
public List<string> Show()
{
List<string> result = new List<string>();
Console.Clear();
Console.WriteLine(Title);
Console.WriteLine(Message);
foreach (var label in labels)
label.Show();
foreach (var option in options)
{
option.Label.Show();
while (true)
{
Console.Write(InputStr);
if(option.SetText(Console.ReadLine()))
{
result.Add(option.Text);
break;
}
}
}
return result;
}
public void AddOption(string text, Func<string, bool> isValid)
{
options.Add(new TextBox() { Label = new Label() {Text = text}, IsValid = isValid });
}
public void AddLabel(string text, TextType type)
{
labels.Add(new Label() {Text = text, Type = type});
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.WindowManagement.Controls;
namespace PluginManager.WindowManagement
{
public class MessageBox
{
public string Title { get; set; }
public string Message { get; set; }
private List<ConsoleOption> options = new List<ConsoleOption>();
public int OptionsCount {get => options.Count;}
public MessageBox(string title, string message)
{
Title = title;
Message = message;
}
public void AddOption(string text, Action action)
{
options.Add(new ConsoleOption() { Text = text, Index = (byte)(options.Count+1), Action = action});
}
public void AddRangeOptions(List<string> texts, List<Action> actions)
{
for (int i = 0; i < texts.Count; i++)
{
options.Add(new ConsoleOption() { Text = texts[i], Index = (byte)(options.Count + 1), Action = actions[i] });
}
}
public int Show()
{
Console.Clear();
Console.WriteLine(Title);
Console.WriteLine(Message);
foreach (var option in options)
{
Console.WriteLine($"{option.Index}. {option.Text}");
}
if(options.Count == 0)
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return 0;
}
if(int.TryParse(Console.ReadLine(), out int result))
{
if(result > 0 && result <= options.Count)
{
if(options[result - 1].Action != null)
options[result - 1].Action();
return result;
}
}
return -1;
}
}
}

View File

@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{99F8602F-4238-455E-B965-E19B62219458}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU
{99F8602F-4238-455E-B965-E19B62219458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99F8602F-4238-455E-B965-E19B62219458}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99F8602F-4238-455E-B965-E19B62219458}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99F8602F-4238-455E-B965-E19B62219458}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

16
test/Program.cs Normal file
View File

@@ -0,0 +1,16 @@
using PluginManager.WindowManagement;
InputBox inputBox = new InputBox();
inputBox.Title = "Test";
inputBox.Message = "This is a test";
inputBox.AddOption("Enter a number", (input) => int.TryParse(input, out int result));
inputBox.AddOption("Enter a string", (input) => !string.IsNullOrEmpty(input));
inputBox.AddOption("Enter a number", (input) => int.TryParse(input, out int result));
var result = inputBox.Show();
foreach (var item in result)
{
Console.WriteLine(item);
}

14
test/test.csproj Normal file
View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>