Discord Bot Scaffolding

This commit is contained in:
Brett Hewitson 2021-12-04 11:39:01 +10:00
parent ceb3ab73c4
commit c4bf4906ea
24 changed files with 1119 additions and 5 deletions

View file

@ -570,6 +570,15 @@
<setting name="ServerFilesGridHeight" serializeAs="String">
<value>250</value>
</setting>
<setting name="DiscordBotEnabled" serializeAs="String">
<value>False</value>
</setting>
<setting name="DiscordBotPrefix" serializeAs="String">
<value>csm</value>
</setting>
<setting name="DiscordBotToken" serializeAs="String">
<value />
</setting>
</ServerManagerTool.Config>
</userSettings>
</configuration>

View file

@ -4,6 +4,9 @@ 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;
@ -37,6 +40,7 @@ namespace ServerManagerTool
public event PropertyChangedEventHandler PropertyChanged;
private CancellationTokenSource _tokenSource;
private GlobalizedApplication _globalizer;
private bool _applicationStarted;
private string _args;
@ -142,6 +146,12 @@ namespace ServerManagerTool
}
}
public IServerManagerBot ServerManagerBot
{
get;
set;
}
public static void DiscoverMachinePublicIP(bool forceOverride)
{
if (forceOverride || string.IsNullOrWhiteSpace(Config.Default.MachinePublicIP))
@ -177,7 +187,11 @@ 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();
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));
@ -216,6 +230,11 @@ namespace ServerManagerTool
return LogManager.GetLogger(loggerName);
}
private static IList<string> HandleDiscordCommand(CommandType commandType, string channelId, string profileId)
{
return null;
}
private static void MigrateSettings()
{
var installFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
@ -395,6 +414,7 @@ namespace ServerManagerTool
this.ApplicationStarted = true;
var restartRequired = false;
if (string.IsNullOrWhiteSpace(Config.Default.DataPath))
{
var dataDirectoryWindow = new DataDirectoryWindow();
@ -405,6 +425,8 @@ namespace ServerManagerTool
{
Environment.Exit(0);
}
restartRequired = true;
}
Config.Default.ConfigPath = Path.Combine(Config.Default.DataPath, Config.Default.ProfilesRelativePath);
@ -412,6 +434,11 @@ namespace ServerManagerTool
Config.Default.Save();
CommonConfig.Default.Save();
if (restartRequired)
{
Environment.Exit(0);
}
if (e.Args.Any(a => a.StartsWith(Constants.ARG_SERVERMONITOR, StringComparison.OrdinalIgnoreCase)))
{
ServerRuntime.EnableUpdateModStatus = false;
@ -426,6 +453,27 @@ namespace ServerManagerTool
StartupUri = new Uri("Windows/AutoUpdateWindow.xaml", UriKind.RelativeOrAbsolute);
}
if (Config.Default.DiscordBotEnabled)
{
_tokenSource = new CancellationTokenSource();
ServerManagerBot = ServerManagerBotFactory.GetServerManagerBot();
Task discordTask = Task.Run(async () =>
{
await ServerManagerBot.StartAsync(Config.Default.DiscordBotPrefix, Config.Default.DiscordBotToken, Config.Default.DataPath, HandleDiscordCommand, _tokenSource.Token);
}, _tokenSource.Token)
.ContinueWith(t => {
var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message;
if (message.StartsWith("#"))
{
message = _globalizer.GetResourceString(message.Substring(1)) ?? message.Substring(1);
}
MessageBox.Show(message, _globalizer.GetResourceString("DiscordBot_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
}, TaskContinuationOptions.OnlyOnFaulted);
}
}
protected override void OnExit(ExitEventArgs e)
@ -466,6 +514,12 @@ namespace ServerManagerTool
private void ShutDownApplication()
{
if (!(_tokenSource is null))
{
_tokenSource.Cancel();
_tokenSource.Dispose();
}
if (this.ApplicationStarted)
{
foreach (var server in ServerManager.Instance.Servers)

View file

@ -1965,5 +1965,41 @@ namespace ServerManagerTool {
return ((string)(this["DefaultDataDirectoryName"]));
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool DiscordBotEnabled {
get {
return ((bool)(this["DiscordBotEnabled"]));
}
set {
this["DiscordBotEnabled"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("csm")]
public string DiscordBotPrefix {
get {
return ((string)(this["DiscordBotPrefix"]));
}
set {
this["DiscordBotPrefix"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string DiscordBotToken {
get {
return ((string)(this["DiscordBotToken"]));
}
set {
this["DiscordBotToken"] = value;
}
}
}
}

View file

@ -545,5 +545,14 @@
<Setting Name="DefaultDataDirectoryName" Type="System.String" Scope="Application">
<Value Profile="(Default)">csmdata</Value>
</Setting>
<Setting Name="DiscordBotEnabled" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="DiscordBotPrefix" Type="System.String" Scope="User">
<Value Profile="(Default)">csm</Value>
</Setting>
<Setting Name="DiscordBotToken" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View file

@ -1193,4 +1193,10 @@
<sys:String x:Key="ServerUpdate_WarningLabel">There was a problem while performing the server update. This may leave your server in a incomplete state.\r\n\r\nDo you want to continue with the server start, this could cause problems?</sys:String>
<!--#endregion-->
<!--#region Discord Bot -->
<sys:String x:Key="DiscordBot_ErrorTitle">Discord Bot Error</sys:String>
<sys:String x:Key="DiscordBot_MissingTokenError">The discord bot requires a valid token so it can log into the discord server\r\nThis can be set in the global settings.</sys:String>
<sys:String x:Key="DiscordBot_InvalidPrefixError">The discord bot prefix contains invalid characters. Only letters and numbers are allowed.</sys:String>
<!--#endregion-->
</Globalization:GlobalizationResourceDictionary>