Removed URLs file from bot config

This commit is contained in:
2023-09-07 12:50:36 +03:00
parent e929646e8e
commit 92a18e3495
6 changed files with 35 additions and 154 deletions

View File

@@ -33,7 +33,7 @@ public class Help : ICommandAction
items.Add(new[] { "-", "-", "-" }); items.Add(new[] { "-", "-", "-" });
Utilities.Utilities.FormatAndAlignTable(items, ConsoleUtilities.FormatAndAlignTable(items,
TableFormat.CENTER_EACH_COLUMN_BASED TableFormat.CENTER_EACH_COLUMN_BASED
); );
return; return;
@@ -55,7 +55,7 @@ public class Help : ICommandAction
new[] { "-", "-", "-" } new[] { "-", "-", "-" }
}; };
Utilities.Utilities.FormatAndAlignTable(actionData, ConsoleUtilities.FormatAndAlignTable(actionData,
TableFormat.CENTER_EACH_COLUMN_BASED TableFormat.CENTER_EACH_COLUMN_BASED
); );
} }

View File

@@ -30,12 +30,12 @@ public class Plugin : ICommandAction
return; return;
} }
var manager = new PluginsManager();
switch ( args[0] ) switch ( args[0] )
{ {
case "list": case "list":
var manager =
new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
var data = await manager.GetAvailablePlugins(); var data = await manager.GetAvailablePlugins();
var items = new List<string[]> var items = new List<string[]>
@@ -49,7 +49,7 @@ public class Plugin : ICommandAction
items.Add(new[] { "-", "-", "-", "-" }); items.Add(new[] { "-", "-", "-", "-" });
Utilities.Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); ConsoleUtilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
break; break;
@@ -139,10 +139,8 @@ public class Plugin : ICommandAction
break; break;
} }
} }
var pluginManager = var pluginData = await manager.GetPluginLinkByName(pluginName);
new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
var pluginData = await pluginManager.GetPluginLinkByName(pluginName);
if (pluginData == null || pluginData.Length == 0) if (pluginData == null || pluginData.Length == 0)
{ {
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again."); Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
@@ -156,7 +154,7 @@ public class Plugin : ICommandAction
Console.WriteLine("Downloading plugin..."); Console.WriteLine("Downloading plugin...");
//download plugin progress bar for linux and windows terminals //download plugin progress bar for linux and windows terminals
var spinner = new Utilities.Utilities.Spinner(); var spinner = new ConsoleUtilities.Spinner();
spinner.Start(); spinner.Start();
IProgress<float> progress = new Progress<float>(p => { spinner.Message = $"Downloading {pluginName}... {Math.Round(p, 2)}% "; }); IProgress<float> progress = new Progress<float>(p => { spinner.Message = $"Downloading {pluginName}... {Math.Round(p, 2)}% "; });
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress); await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress);

View File

@@ -42,24 +42,4 @@ public static class Installer
Console.WriteLine("Config saved !"); Console.WriteLine("Config saved !");
} }
public static async Task SetupPluginDatabase()
{
Console.WriteLine("The plugin database is required to run the bot but there is nothing configured yet.");
Console.WriteLine("Downloading the default database...");
await DownloadPluginDatabase();
}
private static async Task DownloadPluginDatabase(
string url = "https://raw.githubusercontent.com/andreitdr/SethDiscordBot/gh-pages/defaultURLs.json")
{
var path = "./Data/Resources/URLs.json";
Directory.CreateDirectory("./Data/Resources");
var spinner = new Utilities.Utilities.Spinner();
spinner.Start();
await ServerCom.DownloadFileAsync(url, path, null);
spinner.Stop();
}
} }

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordBot.Utilities;
using PluginManager.Bot; using PluginManager.Bot;
using PluginManager.Online; using PluginManager.Online;
using PluginManager.Online.Helpers; using PluginManager.Online.Helpers;
@@ -15,8 +16,7 @@ namespace DiscordBot;
public class Program public class Program
{ {
public static SettingsDictionary<string, string> URLs; public static InternalActionManager internalActionManager;
public static InternalActionManager internalActionManager;
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
@@ -34,7 +34,7 @@ public class Program
args.Length == 1 && args[0] == "/reset") args.Length == 1 && args[0] == "/reset")
Installer.GenerateStartupConfig(); Installer.GenerateStartupConfig();
HandleInput(args.ToList()).Wait(); HandleInput().Wait();
} }
/// <summary> /// <summary>
@@ -42,13 +42,11 @@ public class Program
/// </summary> /// </summary>
private static void NoGUI() private static void NoGUI()
{ {
#if DEBUG
Console.WriteLine("Debug mode enabled");
internalActionManager.Initialize().Wait(); internalActionManager.Initialize().Wait();
internalActionManager.Execute("plugin", "load").Wait(); // Load plugins at startup internalActionManager.Execute("plugin", "load").Wait();
internalActionManager.Refresh().Wait(); internalActionManager.Refresh().Wait();
#endif
while (true) while (true)
{ {
var cmd = Console.ReadLine(); var cmd = Console.ReadLine();
@@ -66,56 +64,30 @@ public class Program
/// Start the bot without user interface /// Start the bot without user interface
/// </summary> /// </summary>
/// <returns>Returns the boot loader for the Discord Bot</returns> /// <returns>Returns the boot loader for the Discord Bot</returns>
private static async Task<Boot> StartNoGui() private static async Task StartNoGui()
{ {
Console.Clear(); Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkYellow; Console.ForegroundColor = ConsoleColor.DarkYellow;
var startupMessageList = Console.WriteLine($"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}");
await ServerCom.ReadTextFromURL(URLs["StartupMessage"]); Console.WriteLine("Git URL: https://github.com/andreitdr/SethDiscordBot");
foreach (var message in startupMessageList) ConsoleUtilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&yexit&r) or some settings won't be saved");
Console.WriteLine(message);
Console.WriteLine( ConsoleUtilities.WriteColorText($"Running on &m{Functions.GetOperatingSystem()}");
$"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}"
);
Console.WriteLine($"Git URL: {AppSettings["GitURL"]}");
Utilities.Utilities.WriteColorText(
"&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n"
);
Console.ForegroundColor = ConsoleColor.White;
if (AppSettings.ContainsKey("LaunchMessage"))
Utilities.Utilities.WriteColorText(AppSettings["LaunchMessage"]);
Utilities.Utilities.WriteColorText(
"Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)"
);
Console.WriteLine("Running on " + Functions.GetOperatingSystem());
Console.WriteLine("============================ LOG ============================"); Console.WriteLine("============================ LOG ============================");
Console.ForegroundColor = ConsoleColor.White;
try try
{ {
var token = ""; var token = AppSettings["token"];
#if DEBUG
if (File.Exists("./Data/Resources/token.txt")) token = File.ReadAllText("./Data/Resources/token.txt");
else token = AppSettings["token"];
#else
token = AppSettings["token"];
#endif
var prefix = AppSettings["prefix"]; var prefix = AppSettings["prefix"];
var discordbooter = new Boot(token, prefix); var discordbooter = new Boot(token, prefix);
await discordbooter.Awake(); await discordbooter.Awake();
return discordbooter;
} }
catch ( Exception ex ) catch ( Exception ex )
{ {
Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR); Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
return null;
} }
} }
@@ -123,22 +95,9 @@ public class Program
/// Handle user input arguments from the startup of the application /// Handle user input arguments from the startup of the application
/// </summary> /// </summary>
/// <param name="args">The arguments</param> /// <param name="args">The arguments</param>
private static async Task HandleInput(List<string> args) private static async Task HandleInput()
{ {
Console.WriteLine("Loading Core ..."); await StartNoGui();
//Handle arguments here:
if (args.Contains("--gui"))
{
// GUI not implemented yet
Console.WriteLine("GUI not implemented yet");
return;
}
// Starting bot after all arguments are handled
var b = await StartNoGui();
try try
{ {
internalActionManager = new InternalActionManager("./Data/Plugins", "*.dll"); internalActionManager = new InternalActionManager("./Data/Plugins", "*.dll");
@@ -178,70 +137,6 @@ public class Program
Console.WriteLine($"[{type.ToString()}] {message}"); Console.WriteLine($"[{type.ToString()}] {message}");
Console.ResetColor(); Console.ResetColor();
}; };
if (!Directory.Exists("./Data/Resources") || !File.Exists("./Data/Resources/URLs.json"))
await Installer.SetupPluginDatabase();
URLs = new SettingsDictionary<string, string>("./Data/Resources/URLs.json");
Console.WriteLine("Loading resources ...");
if (AppSettings.ContainsKey("DeleteLogsAtStartup"))
if (AppSettings["DeleteLogsAtStartup"] == "true")
foreach (var file in Directory.GetFiles("./Output/Logs/"))
File.Delete(file);
var OnlineDefaultKeys = await ServerCom.ReadTextFromURL(URLs["SetupKeys"]);
AppSettings["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString(); AppSettings["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
foreach (var key in OnlineDefaultKeys)
{
if (key.Length <= 3 || !key.Contains(' ')) continue;
var s = key.Split(' ');
try
{
AppSettings[s[0]] = s[1];
}
catch ( Exception ex )
{
Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
}
}
var onlineSettingsList = await ServerCom.ReadTextFromURL(URLs["Versions"]);
foreach (var key in onlineSettingsList)
{
if (key.Length <= 3 || !key.Contains(' ')) continue;
var s = key.Split(' ');
switch ( s[0] )
{
case "CurrentVersion":
var currentVersion = AppSettings["Version"];
var newVersion = s[1];
if (new VersionString(newVersion) != new VersionString(newVersion))
{
Console.WriteLine("A new updated was found. Check the changelog for more information.");
var changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
foreach (var item in changeLog)
Utilities.Utilities.WriteColorText(item);
Console.WriteLine("Current version: " + currentVersion);
Console.WriteLine("Latest version: " + newVersion);
Console.WriteLine("Download from here: https://github.com/andreitdr/SethDiscordBot/releases");
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
break;
}
}
Console.Clear();
} }
} }

View File

@@ -4,7 +4,7 @@ using System.Threading;
namespace DiscordBot.Utilities; namespace DiscordBot.Utilities;
public static class Utilities public static class ConsoleUtilities
{ {
private static readonly Dictionary<char, ConsoleColor> Colors = new() private static readonly Dictionary<char, ConsoleColor> Colors = new()
{ {
@@ -16,8 +16,7 @@ public static class Utilities
}; };
private static readonly char ColorPrefix = '&'; private static readonly char ColorPrefix = '&';
private static bool CanAproximateTo(this float f, float y) private static bool CanAproximateTo(this float f, float y)
{ {
return MathF.Abs(f - y) < 0.000001; return MathF.Abs(f - y) < 0.000001;

View File

@@ -19,6 +19,15 @@ public class PluginsManager
PluginsLink = plink; PluginsLink = plink;
VersionsLink = vlink; VersionsLink = vlink;
} }
/// <summary>
/// The default Plugin Manager constructor. It uses the default links.
/// </summary>
public PluginsManager()
{
PluginsLink = "https://raw.githubusercontent.com/andreitdr/installer/discord-bot-files/Plugins.txt";
VersionsLink = "https://raw.githubusercontent.com/andreitdr/installer/discord-bot-files/Versions";
}
/// <summary> /// <summary>
/// The URL of the server /// The URL of the server