Created Helper classes for Discord Bot and Plugin

This commit is contained in:
Brett Hewitson 2021-12-04 15:01:47 +10:00
parent 8d9d208a66
commit d526a3f457
8 changed files with 74 additions and 35 deletions

View file

@ -203,6 +203,8 @@
<Compile Include="Lib\ViewConverters\MapNameValueConverter.cs" />
<Compile Include="Lib\ViewModel\Engram.cs" />
<Compile Include="Lib\ViewModel\EngramSettings.cs" />
<Compile Include="Utils\DiscordBotHelper.cs" />
<Compile Include="Utils\DiscordPluginHelper.cs" />
<Compile Include="Utils\ModUtils.cs" />
<Compile Include="Windows\AddUserWindow.xaml.cs">
<DependentUpon>AddUserWindow.xaml</DependentUpon>

View file

@ -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<Plugin.Common.Lib.Profile> 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<string> HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId)
{
return new List<string>() { $"{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;

View file

@ -0,0 +1,13 @@
using ServerManagerTool.Discord.Enums;
using System.Collections.Generic;
namespace ServerManagerTool.Utils
{
internal static class DiscordBotHelper
{
public static IList<string> HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId)
{
return new List<string>() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" };
}
}
}

View file

@ -0,0 +1,18 @@
using ServerManagerTool.Lib;
using System.Collections.Generic;
using System.Linq;
namespace ServerManagerTool.Utils
{
internal static class DiscordPluginHelper
{
public static IList<Plugin.Common.Lib.Profile> 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();
}
}
}

View file

@ -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<Plugin.Common.Lib.Profile> 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;

View file

@ -171,6 +171,8 @@
<Compile Include="Lib\ViewConverters\EnumDescriptionTypeConverter.cs" />
<Compile Include="Lib\ViewConverters\MapNameValueConverter.cs" />
<Compile Include="Lib\ViewModel\PlayerInfo.cs" />
<Compile Include="Utils\DiscordBotHelper.cs" />
<Compile Include="Utils\DiscordPluginHelper.cs" />
<Compile Include="Windows\AddUserWindow.xaml.cs">
<DependentUpon>AddUserWindow.xaml</DependentUpon>
</Compile>

View file

@ -0,0 +1,13 @@
using ServerManagerTool.Discord.Enums;
using System.Collections.Generic;
namespace ServerManagerTool.Utils
{
internal static class DiscordBotHelper
{
public static IList<string> HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId)
{
return new List<string>() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" };
}
}
}

View file

@ -0,0 +1,18 @@
using ServerManagerTool.Lib;
using System.Collections.Generic;
using System.Linq;
namespace ServerManagerTool.Utils
{
internal static class DiscordPluginHelper
{
public static IList<Plugin.Common.Lib.Profile> 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();
}
}
}