diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 0d76686..5f0f3aa 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -27,6 +27,7 @@ namespace DiscordBot private static bool listLanguagAtStartup = false; private static bool PluginsLoaded = false; + private static bool ShowStartupMessage = true; /// /// The main entry point for the application. @@ -411,10 +412,15 @@ namespace DiscordBot Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Discord BOT for Cross Platform"); Console.WriteLine("Created by: Wizzy\nDiscord: Wizzy#9181"); + if (ShowStartupMessage) + try + { + Console.WriteLine("Connecting to server ..."); + List text = await ServerCom.ReadTextFromFile("https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/StartupMessage"); + foreach (var t in text) Console_Utilities.WriteColorText(t); - List text = await ServerCom.ReadTextFromFile("https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/StartupMessage"); - foreach (var t in text) Console_Utilities.WriteColorText(t); - + } + catch { Console.WriteLine("Failed to connect to server."); } Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("============================ Discord BOT - Cross Platform ============================"); string token = Functions.readCodeFromFile(Functions.dataFolder + "DiscordBotCore.data", "BOT_TOKEN", '='); @@ -449,23 +455,22 @@ namespace DiscordBot private static async Task HandleInput(string[] args) { - if (args.Length > 0) - if (args[0] == "progress") - { - Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(100, "Download"); - for (int i = 0; i <= 100; i++) - { - bar.Update(i); - await Task.Delay(10); - - } - return; - } - else if (args[0] == "test") - { - return; - } + /* if (args.Length > 0) + if (args[0] == "progress") + { + Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(100, "Download"); + for (int i = 0; i <= 100; i++) + { + bar.Update(i); + await Task.Delay(10); + } + return; + } + else if (args[0] == "test") + { + return; + }*/ if (args.Length == 0) { @@ -498,7 +503,7 @@ namespace DiscordBot return; } - if (len > 0 && (args.Contains("--cmd") || args.Contains("--args"))) + if (len > 0 && (args.Contains("--cmd") || args.Contains("--args") || args.Contains("--nomessage"))) { if (args.Contains("lp") || args.Contains("loadplugins")) loadPluginsOnStartup = true; @@ -506,12 +511,14 @@ namespace DiscordBot listPluginsAtStartup = true; if (args.Contains("listlang")) listLanguagAtStartup = true; - + if (args.Contains("--nomessage")) + ShowStartupMessage = false; len = 0; } + if (len == 0 || args[0] != "--exec" && args[0] != "--execute") { Boot b = await StartNoGUI(); diff --git a/DiscordBotGUI/App.axaml.cs b/DiscordBotGUI/App.axaml.cs index 8850f62..a3b2c89 100644 --- a/DiscordBotGUI/App.axaml.cs +++ b/DiscordBotGUI/App.axaml.cs @@ -17,7 +17,7 @@ namespace DiscordBotGUI { - desktop.MainWindow = new AppUpdater() { Width = 250, Height = 50 }; + desktop.MainWindow = new AppUpdater() { Width = 300, Height = 50, WindowStartupLocation = Avalonia.Controls.WindowStartupLocation.CenterScreen }; } diff --git a/DiscordBotGUI/AppUpdater.axaml b/DiscordBotGUI/AppUpdater.axaml index 9400060..c20c2e5 100644 --- a/DiscordBotGUI/AppUpdater.axaml +++ b/DiscordBotGUI/AppUpdater.axaml @@ -4,10 +4,13 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="50" x:Class="DiscordBotGUI.AppUpdater" - Title="AppUpdater"> + Title="AppUpdater" + Background="Transparent" + TransparencyLevelHint="AcrylicBlur" + HasSystemDecorations="False"> - + diff --git a/DiscordBotGUI/AppUpdater.axaml.cs b/DiscordBotGUI/AppUpdater.axaml.cs index 3edf0c5..ecc5405 100644 --- a/DiscordBotGUI/AppUpdater.axaml.cs +++ b/DiscordBotGUI/AppUpdater.axaml.cs @@ -7,11 +7,15 @@ using PluginManager.Others; using System.Threading.Tasks; using System; using System.IO; +using System.Threading; +using System.Drawing; +using Avalonia.Media; namespace DiscordBotGUI { public partial class AppUpdater : Window { + private string _version; public AppUpdater() { InitializeComponent(); @@ -19,38 +23,51 @@ namespace DiscordBotGUI { textBox1.Text = "Checking ..."; File.WriteAllText("./Version.txt", "DiscordBotVersion=0"); - DownloadDiscordBotClientNoGUIAsDLL(); + //DownloadDiscordBotClientNoGUIAsDLL(); } Updates(); } - private async void DownloadDiscordBotClientNoGUIAsDLL() - { + /* private async void DownloadDiscordBotClientNoGUIAsDLL() + { - //await Task.Delay(5000); - string url_bot_dll = "https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/Updates/DiscordBot.dll"; - IProgress progress = new Progress((percent) => - { - textBox1.Text = "Downloading DiscordBot.dll ... " + percent.ToString() + "%"; - this.progressBar1.Value = percent * 100; - }); + //await Task.Delay(5000); + string url_bot_dll = "https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/Updates/DiscordBot.dll"; + IProgress progress = new Progress((percent) => + { + textBox1.Text = "Downloading DiscordBot.dll ... " + (percent * 100).ToString() + "%"; + this.progressBar1.Value = percent * 100; + }); - this.progressBar1.IsIndeterminate = false; - await ServerCom.DownloadFileAsync(url_bot_dll, "./DiscordBot.dll", progress); - new MainWindow().Show(); - Close(); - } + this.progressBar1.IsIndeterminate = false; + try + { + await ServerCom.DownloadFileAsync(url_bot_dll, "./DiscordBot.dll", progress); + } + catch + { + textBox1.Text = "Error downloading DiscordBot.dll. Server is not responding."; + + await Task.Delay(1000); + return; + } + + //new MainWindow() { Height = 425, Width = 500 }.Show(); + //Close(); + }*/ private async void Updates() { + this.progressBar1.IsIndeterminate = true; + await Task.Delay(1000); if (!await CheckForUpdates()) { - await Task.Delay(5000); - textBox1.Text = "There is no update found !"; + //await Task.Delay(5000); + textBox1.Text = $"You are running on the latest version ({_version}) !"; await Task.Delay(2000); - new MainWindow().Show(); + new MainWindow() { Height = 425, Width = 650 }.Show(); this.Close(); return; } @@ -59,8 +76,8 @@ namespace DiscordBotGUI if (file == null) { textBox1.Text = "There was an error while downloading the update !"; - await Task.Delay(5000); - new MainWindow().Show(); + await Task.Delay(2000); + new MainWindow() { Height = 425, Width = 650 }.Show(); this.Close(); return; } @@ -69,12 +86,15 @@ namespace DiscordBotGUI { this.progressBar1.Value = percent; }); + + textBox1.Text = "Extracting update files ..."; await Functions.ExtractArchive(file, "./", progress); - - - textBox1.Text = "Update downloaded successfully !"; - await Task.Delay(2000); - new MainWindow().Show(); + progressBar1.IsIndeterminate = true; + textBox1.Text = "Setting up the new version ..."; + File.Delete(file); + File.WriteAllText("./Version.txt", "DiscordBotVersion=" + _version); + await Task.Delay(5000); + new MainWindow() { Height = 425, Width = 650 }.Show(); this.Close(); } @@ -82,16 +102,47 @@ namespace DiscordBotGUI private async Task DownloadNewUpdate() { string urlNewUpdateZip = (await ServerCom.ReadTextFromFile("https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/Updates/Version"))[1]; + int secondsPast = 0; + bool isDownloading = true; + this.progressBar1.IsIndeterminate = true; + textBox1.Text = "Downloading update ..."; + + + IProgress downloaded = new Progress((bytes) => + { + (double, string) download = Functions.ConvertBytes(bytes); + textBox1.Text = $"Downloading update ... {Math.Round(download.Item1 / secondsPast, 2)} {download.Item2}/s"; + }); IProgress progress = new Progress((percent) => { + progressBar1.IsIndeterminate = false; this.progressBar1.Value = percent; }); - this.progressBar1.IsIndeterminate = false; - string FileName = $"{urlNewUpdateZip.Split('/')[urlNewUpdateZip.Split('/').Length - 1]}.zip"; - await ServerCom.DownloadFileAsync(urlNewUpdateZip, FileName, progress); + string FileName = $"{urlNewUpdateZip.Split('/')[urlNewUpdateZip.Split('/').Length - 1]}"; + try + { + + new Thread(new Task(() => + { + while (isDownloading) + { + Thread.Sleep(1000); + secondsPast++; + } + }).Start).Start(); + await ServerCom.DownloadFileAsync(urlNewUpdateZip, FileName, progress, downloaded); + } + catch + { + textBox1.Text = "Error downloading the update. Server is not responding."; + isDownloading = false; + await Task.Delay(1000); + return null; + } + isDownloading = false; return FileName; } @@ -102,6 +153,7 @@ namespace DiscordBotGUI string current_version = Functions.readCodeFromFile("Version.txt", "DiscordBotVersion", '=') ?? "0"; string latest_version = (await ServerCom.ReadTextFromFile("https://sethdiscordbot.000webhostapp.com/Storage/Discord%20Bot/Updates/Version"))[0]; + _version = latest_version; if (current_version != latest_version) { return true; @@ -111,7 +163,7 @@ namespace DiscordBotGUI } catch (Exception ex) { - //File.WriteAllText("./Debug.txt", "Error while checking for updates !\n" + ex.ToString()); + textBox1.Text = "Error while checking for updates. Server is not responding."; return false; } } diff --git a/DiscordBotGUI/MainWindow.axaml b/DiscordBotGUI/MainWindow.axaml index a00456b..43854e9 100644 --- a/DiscordBotGUI/MainWindow.axaml +++ b/DiscordBotGUI/MainWindow.axaml @@ -2,15 +2,15 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="500" + mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="425" x:Class="DiscordBotGUI.MainWindow" - Title="DiscordBotGUI"> + Title="DiscordBotGUI" + Background="Transparent" + TransparencyLevelHint="AcrylicBlur" + ExtendClientAreaToDecorationsHint="True" > - + - @@ -24,8 +24,8 @@