From 8b36c086ef810ab5cbc690d768170c599bae45a1 Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Wed, 17 Aug 2022 14:19:46 +0300 Subject: [PATCH] New Command added & Leveling System update --- DiscordBot/DiscordBot.csproj | 4 +-- DiscordBot/Program.cs | 21 +++--------- EVE_LevelingSystem/Level.cs | 8 +++-- .../LevelingSystemCore/LevelCalculator.cs | 15 +++++---- EVE_LevelingSystem/Settings.cs | 2 ++ PluginManager/Items/ConsoleCommandsHandler.cs | 33 +++++++++++++++++++ PluginManager/Others/Functions.cs | 5 +-- 7 files changed, 58 insertions(+), 30 deletions(-) diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index fdfaf9e..adc235a 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -12,11 +12,11 @@ - none + none - none + none diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index cc19549..77f2207 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -2,18 +2,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net.Http; using System.Reflection; -using System.Runtime.Loader; using System.Threading; using System.Threading.Tasks; -using Discord.WebSocket; - using DiscordBot.Discord.Core; using PluginManager; -using PluginManager.Interfaces; using PluginManager.Items; using PluginManager.Online; using PluginManager.Others; @@ -119,22 +114,16 @@ public class Program if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs"); #endif Config.SaveConfig(); - while (true) { - Console.ForegroundColor = ConsoleColor.White; - -#if DEBUG //Console_Utilities.WriteColorText("&rSethBot (&yDEBUG&r) &c> ", false); var cmd = Console.ReadLine(); - if (!consoleCommandsHandler.HandleCommand(cmd!, false) && cmd.Length > 0) - Console.WriteLine("Failed to run command " + cmd); -#else - //Console_Utilities.WriteColorText("&rSethBot &c> ", false); - var cmd = Console.ReadLine(); - if (!consoleCommandsHandler.HandleCommand(cmd!) && cmd.Length > 0) - Console.WriteLine("Failed to run command " + cmd); + if (!consoleCommandsHandler.HandleCommand(cmd!, +#if DEBUG + false #endif + ) && cmd.Length > 0) + Console.WriteLine("Failed to run command " + cmd); } } diff --git a/EVE_LevelingSystem/Level.cs b/EVE_LevelingSystem/Level.cs index 9afa612..68ece34 100644 --- a/EVE_LevelingSystem/Level.cs +++ b/EVE_LevelingSystem/Level.cs @@ -1,6 +1,8 @@ using Discord; using Discord.WebSocket; + using EVE_LevelingSystem.LevelingSystemCore; + using PluginManager; using PluginManager.Interfaces; using PluginManager.Others; @@ -9,8 +11,8 @@ namespace EVE_LevelingSystem { internal class Level : DBEvent { - public string name => "Leveling System Event Handler"; - public string description => "The Leveling System Event Handler"; + public string name => "Leveling System Event Handler"; + public string description => "The Leveling System Event Handler"; internal static Settings globalSettings = new(); @@ -39,7 +41,7 @@ namespace EVE_LevelingSystem { if (arg.Author.IsBot || arg.IsTTS || arg.Content.StartsWith(Config.GetValue("prefix"))) return; string userID = arg.Author.Id.ToString(); - User user; + User user; if (File.Exists($"{Config.GetValue("LevelingSystemPath")}/{userID}.dat")) { user = await Functions.ConvertFromJson(Config.GetValue("LevelingSystemPath")! + $"/{userID}.dat"); diff --git a/EVE_LevelingSystem/LevelingSystemCore/LevelCalculator.cs b/EVE_LevelingSystem/LevelingSystemCore/LevelCalculator.cs index 733adac..0359c6b 100644 --- a/EVE_LevelingSystem/LevelingSystemCore/LevelCalculator.cs +++ b/EVE_LevelingSystem/LevelingSystemCore/LevelCalculator.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; + using PluginManager; namespace EVE_LevelingSystem.LevelingSystemCore @@ -18,7 +19,7 @@ namespace EVE_LevelingSystem.LevelingSystemCore internal static void LevelUp(this User user) { - user.CurrentEXP = 0; + user.CurrentEXP = 0; user.RequiredEXPToLevelUp = GetNextLevelRequiredEXP(user.CurrentLevel); user.CurrentLevel++; } @@ -26,15 +27,15 @@ namespace EVE_LevelingSystem.LevelingSystemCore internal static bool AddEXP(this User user) { if (OnWaitingList.Contains(user.user.userID.ToString())) return false; - Random r = new Random(); - int exp = r.Next(2, 12); - Int64 userXP = user.CurrentEXP; - Int64 reqEXP = user.RequiredEXPToLevelUp; + Random r = new Random(); + int exp = r.Next(Level.globalSettings.MinEXP, Level.globalSettings.MaxEXP + 1); + Int64 userXP = user.CurrentEXP; + Int64 reqEXP = user.RequiredEXPToLevelUp; if (userXP + exp >= reqEXP) { user.LevelUp(); user.CurrentEXP = exp - (reqEXP - userXP); - Console.WriteLine("Level up"); + //Console.WriteLine("Level up"); return true; } @@ -49,7 +50,7 @@ namespace EVE_LevelingSystem.LevelingSystemCore Thread.Sleep(60000 * minutesToWait); OnWaitingList.Remove(user.user.userID.ToString()); } - ); + ).Start(); return false; } diff --git a/EVE_LevelingSystem/Settings.cs b/EVE_LevelingSystem/Settings.cs index 38a8caa..5225e9d 100644 --- a/EVE_LevelingSystem/Settings.cs +++ b/EVE_LevelingSystem/Settings.cs @@ -9,5 +9,7 @@ namespace EVE_LevelingSystem public class Settings { public int TimeToWaitBetweenMessages { get; set; } + public int MinEXP { get; set; } + public int MaxEXP { get; set; } } } diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index d549ed8..8c67892 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -2,11 +2,14 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Http; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using Discord.WebSocket; +using PluginManager.Interfaces; using PluginManager.Loaders; using PluginManager.Online; using PluginManager.Online.Helpers; @@ -262,6 +265,35 @@ public class ConsoleCommandsHandler Environment.Exit(0); } ); + + AddCommand("extern", "Load an external command", "extern [pluginName]", async (args) => + { + if (args.Length <= 1) return; + string pName = Functions.MergeStrings(args, 1); + HttpClient client = new HttpClient(); + string url = (await manager.GetPluginLinkByName(pName))[1]; + Stream s = await client.GetStreamAsync(url); + MemoryStream str = new MemoryStream(); + await s.CopyToAsync(str); + var asmb = Assembly.Load(str.ToArray()); + + var types = asmb.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && typeof(DBEvent).IsAssignableFrom(type)) + { + DBEvent instance = (DBEvent)Activator.CreateInstance(type); + instance.Start(this.client); + Console.WriteLine($"Loaded external {type.FullName}!"); + } + else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type)) + { + Console.WriteLine("Only events can be loaded from external sources !"); + return; + } + } + }); + //Sort the commands by name commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName)); } @@ -309,6 +341,7 @@ public class ConsoleCommandsHandler public bool HandleCommand(string command, bool removeCommandExecution = true) { + Console.ForegroundColor = ConsoleColor.White; var args = command.Split(' '); foreach (var item in commandList.ToList()) if (item.CommandName == args[0]) diff --git a/PluginManager/Others/Functions.cs b/PluginManager/Others/Functions.cs index 61a88dd..8039902 100644 --- a/PluginManager/Others/Functions.cs +++ b/PluginManager/Others/Functions.cs @@ -261,7 +261,8 @@ namespace PluginManager.Others /// public static async Task SaveToJsonFile(string file, T Data) { - var s = File.OpenWrite(file); + File.Delete(file); + var s = File.Open(file, FileMode.OpenOrCreate); await JsonSerializer.SerializeAsync(s, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true }); s.Close(); } @@ -276,7 +277,7 @@ namespace PluginManager.Others { Stream text; if (File.Exists(input)) - text = File.Open(input, FileMode.OpenOrCreate); + text = File.OpenRead(input); else text = new MemoryStream(Encoding.ASCII.GetBytes(input));