From d526a3f457bf29375cc4152523b13b47d1a1b112 Mon Sep 17 00:00:00 2001 From: Brett Hewitson Date: Sat, 4 Dec 2021 15:01:47 +1000 Subject: [PATCH] Created Helper classes for Discord Bot and Plugin --- src/ARKServerManager/ARKServerManager.csproj | 2 ++ src/ARKServerManager/App.xaml.cs | 25 +++---------------- .../Utils/DiscordBotHelper.cs | 13 ++++++++++ .../Utils/DiscordPluginHelper.cs | 18 +++++++++++++ src/ConanServerManager/App.xaml.cs | 18 +++---------- .../ConanServerManager.csproj | 2 ++ .../Utils/DiscordBotHelper.cs | 13 ++++++++++ .../Utils/DiscordPluginHelper.cs | 18 +++++++++++++ 8 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 src/ARKServerManager/Utils/DiscordBotHelper.cs create mode 100644 src/ARKServerManager/Utils/DiscordPluginHelper.cs create mode 100644 src/ConanServerManager/Utils/DiscordBotHelper.cs create mode 100644 src/ConanServerManager/Utils/DiscordPluginHelper.cs diff --git a/src/ARKServerManager/ARKServerManager.csproj b/src/ARKServerManager/ARKServerManager.csproj index d291d245..3d5408c4 100644 --- a/src/ARKServerManager/ARKServerManager.csproj +++ b/src/ARKServerManager/ARKServerManager.csproj @@ -203,6 +203,8 @@ + + AddUserWindow.xaml diff --git a/src/ARKServerManager/App.xaml.cs b/src/ARKServerManager/App.xaml.cs index e3b1e01c..5fe95cd2 100644 --- a/src/ARKServerManager/App.xaml.cs +++ b/src/ARKServerManager/App.xaml.cs @@ -1,16 +1,16 @@ using ArkData; -using ServerManagerTool.Discord.Interfaces; using NLog; using NLog.Config; using NLog.Targets; using ServerManagerTool.Common; using ServerManagerTool.Common.Utils; +using ServerManagerTool.Discord; using ServerManagerTool.Enums; using ServerManagerTool.Lib; using ServerManagerTool.Plugin.Common; +using ServerManagerTool.Utils; using ServerManagerTool.Windows; using System; -using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; @@ -21,10 +21,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using System.Windows; -using System.Xml; using WPFSharp.Globalizer; -using ServerManagerTool.Discord; -using ServerManagerTool.Discord.Enums; namespace ServerManagerTool { @@ -180,15 +177,6 @@ namespace ServerManagerTool } } - private IList FetchProfiles() - { - return ServerManager.Instance.Servers.Select(s => new ServerManagerTool.Plugin.Common.Lib.Profile() - { - ProfileName = s?.Profile?.ProfileName ?? string.Empty, - InstallationFolder = s?.Profile?.InstallDirectory ?? string.Empty - }).ToList(); - } - public static string GetLogFolder() => IOUtils.NormalizePath(Path.Combine(Config.Default.DataDir, Config.Default.LogsDir)); public static string GetProfileLogFolder(string profileId) => IOUtils.NormalizePath(Path.Combine(Config.Default.DataDir, Config.Default.LogsDir, profileId.ToLower())); @@ -225,11 +213,6 @@ namespace ServerManagerTool return LogManager.GetLogger(loggerName); } - private static IList HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId) - { - return new List() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" }; - } - private static void MigrateSettings() { var installFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); @@ -324,7 +307,7 @@ namespace ServerManagerTool var installPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); PluginHelper.Instance.BetaEnabled = this.BetaVersion; PluginHelper.Instance.LoadPlugins(installPath, true); - PluginHelper.Instance.SetFetchProfileCallback(FetchProfiles); + PluginHelper.Instance.SetFetchProfileCallback(DiscordPluginHelper.FetchProfiles); OnResourceDictionaryChanged(Thread.CurrentThread.CurrentCulture.Name); // check if we are starting ASM for the old server restart - no longer supported @@ -473,7 +456,7 @@ namespace ServerManagerTool Task discordTask = Task.Run(async () => { - await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, HandleDiscordCommand, _tokenSource.Token); + await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, DiscordBotHelper.HandleDiscordCommand, _tokenSource.Token); }, _tokenSource.Token) .ContinueWith(t => { var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; diff --git a/src/ARKServerManager/Utils/DiscordBotHelper.cs b/src/ARKServerManager/Utils/DiscordBotHelper.cs new file mode 100644 index 00000000..2ca1be1f --- /dev/null +++ b/src/ARKServerManager/Utils/DiscordBotHelper.cs @@ -0,0 +1,13 @@ +using ServerManagerTool.Discord.Enums; +using System.Collections.Generic; + +namespace ServerManagerTool.Utils +{ + internal static class DiscordBotHelper + { + public static IList HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId) + { + return new List() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" }; + } + } +} diff --git a/src/ARKServerManager/Utils/DiscordPluginHelper.cs b/src/ARKServerManager/Utils/DiscordPluginHelper.cs new file mode 100644 index 00000000..7ec9ee93 --- /dev/null +++ b/src/ARKServerManager/Utils/DiscordPluginHelper.cs @@ -0,0 +1,18 @@ +using ServerManagerTool.Lib; +using System.Collections.Generic; +using System.Linq; + +namespace ServerManagerTool.Utils +{ + internal static class DiscordPluginHelper + { + public static IList FetchProfiles() + { + return ServerManager.Instance.Servers.Select(s => new ServerManagerTool.Plugin.Common.Lib.Profile() + { + ProfileName = s?.Profile?.ProfileName ?? string.Empty, + InstallationFolder = s?.Profile?.InstallDirectory ?? string.Empty + }).ToList(); + } + } +} diff --git a/src/ConanServerManager/App.xaml.cs b/src/ConanServerManager/App.xaml.cs index 3818dd44..15a08442 100644 --- a/src/ConanServerManager/App.xaml.cs +++ b/src/ConanServerManager/App.xaml.cs @@ -1,15 +1,14 @@ -using Microsoft.WindowsAPICodePack.Dialogs; -using NLog; +using NLog; using NLog.Config; using NLog.Targets; using ServerManagerTool.Common; using ServerManagerTool.Common.Utils; using ServerManagerTool.Discord; using ServerManagerTool.Discord.Enums; -using ServerManagerTool.Discord.Interfaces; using ServerManagerTool.Enums; using ServerManagerTool.Lib; using ServerManagerTool.Plugin.Common; +using ServerManagerTool.Utils; using ServerManagerTool.Windows; using System; using System.Collections.Generic; @@ -179,15 +178,6 @@ namespace ServerManagerTool } } - private IList FetchProfiles() - { - return ServerManager.Instance.Servers.Select(s => new ServerManagerTool.Plugin.Common.Lib.Profile() - { - ProfileName = s?.Profile?.ProfileName ?? string.Empty, - InstallationFolder = s?.Profile?.InstallDirectory ?? string.Empty - }).ToList(); - } - public static string GetLogFolder() => IOUtils.NormalizePath(Path.Combine(Config.Default.DataPath, Config.Default.LogsRelativePath)); public static string GetProfileLogFolder(string profileId) => IOUtils.NormalizePath(Path.Combine(Config.Default.DataPath, Config.Default.LogsRelativePath, profileId.ToLower())); @@ -317,7 +307,7 @@ namespace ServerManagerTool var installPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); PluginHelper.Instance.BetaEnabled = this.BetaVersion; PluginHelper.Instance.LoadPlugins(installPath, true); - PluginHelper.Instance.SetFetchProfileCallback(FetchProfiles); + PluginHelper.Instance.SetFetchProfileCallback(DiscordPluginHelper.FetchProfiles); OnResourceDictionaryChanged(Thread.CurrentThread.CurrentCulture.Name); // check if we are starting server manager for server shutdown @@ -454,7 +444,7 @@ namespace ServerManagerTool Task discordTask = Task.Run(async () => { - await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken,Config.Default.DiscordBotPrefix, Config.Default.DataPath, HandleDiscordCommand, _tokenSource.Token); + await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken,Config.Default.DiscordBotPrefix, Config.Default.DataPath, DiscordBotHelper.HandleDiscordCommand, _tokenSource.Token); }, _tokenSource.Token) .ContinueWith(t => { var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; diff --git a/src/ConanServerManager/ConanServerManager.csproj b/src/ConanServerManager/ConanServerManager.csproj index da8a7e5a..463cdd2a 100644 --- a/src/ConanServerManager/ConanServerManager.csproj +++ b/src/ConanServerManager/ConanServerManager.csproj @@ -171,6 +171,8 @@ + + AddUserWindow.xaml diff --git a/src/ConanServerManager/Utils/DiscordBotHelper.cs b/src/ConanServerManager/Utils/DiscordBotHelper.cs new file mode 100644 index 00000000..2ca1be1f --- /dev/null +++ b/src/ConanServerManager/Utils/DiscordBotHelper.cs @@ -0,0 +1,13 @@ +using ServerManagerTool.Discord.Enums; +using System.Collections.Generic; + +namespace ServerManagerTool.Utils +{ + internal static class DiscordBotHelper + { + public static IList HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId) + { + return new List() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" }; + } + } +} diff --git a/src/ConanServerManager/Utils/DiscordPluginHelper.cs b/src/ConanServerManager/Utils/DiscordPluginHelper.cs new file mode 100644 index 00000000..7ec9ee93 --- /dev/null +++ b/src/ConanServerManager/Utils/DiscordPluginHelper.cs @@ -0,0 +1,18 @@ +using ServerManagerTool.Lib; +using System.Collections.Generic; +using System.Linq; + +namespace ServerManagerTool.Utils +{ + internal static class DiscordPluginHelper + { + public static IList FetchProfiles() + { + return ServerManager.Instance.Servers.Select(s => new ServerManagerTool.Plugin.Common.Lib.Profile() + { + ProfileName = s?.Profile?.ProfileName ?? string.Empty, + InstallationFolder = s?.Profile?.InstallDirectory ?? string.Empty + }).ToList(); + } + } +}