mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-18 09:35:48 +00:00
Split Discord Start and Stop into separate methods, so they can be called from other parts of the application.
This commit is contained in:
parent
ad9455c0c7
commit
c85ec44d9f
3 changed files with 151 additions and 58 deletions
|
|
@ -39,7 +39,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
private CancellationTokenSource _tokenSource;
|
private CancellationTokenSource _tokenSourceDiscordBot;
|
||||||
private GlobalizedApplication _globalizer;
|
private GlobalizedApplication _globalizer;
|
||||||
private bool _applicationStarted;
|
private bool _applicationStarted;
|
||||||
private string _args;
|
private string _args;
|
||||||
|
|
@ -151,6 +151,18 @@ namespace ServerManagerTool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool DiscordBotStarted
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _tokenSourceDiscordBot != null;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Title
|
public string Title
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -498,27 +510,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
if (Config.Default.DiscordBotEnabled)
|
if (Config.Default.DiscordBotEnabled)
|
||||||
{
|
{
|
||||||
_tokenSource = new CancellationTokenSource();
|
StartDiscordBot();
|
||||||
|
|
||||||
Task discordTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
var discordWhiteList = new List<string>();
|
|
||||||
if (Config.Default.DiscordBotWhitelist != null)
|
|
||||||
{
|
|
||||||
discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast<string>());
|
|
||||||
}
|
|
||||||
|
|
||||||
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -560,11 +552,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
private void ShutDownApplication()
|
private void ShutDownApplication()
|
||||||
{
|
{
|
||||||
if (!(_tokenSource is null))
|
StopDiscordBot();
|
||||||
{
|
|
||||||
_tokenSource.Cancel();
|
|
||||||
_tokenSource.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ApplicationStarted)
|
if (ApplicationStarted)
|
||||||
{
|
{
|
||||||
|
|
@ -605,5 +593,60 @@ namespace ServerManagerTool
|
||||||
SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder);
|
SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder);
|
||||||
SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder);
|
SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void StartDiscordBot()
|
||||||
|
{
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_tokenSourceDiscordBot = new CancellationTokenSource();
|
||||||
|
DiscordBotStarted = true;
|
||||||
|
|
||||||
|
Task discordTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
var discordWhiteList = new List<string>();
|
||||||
|
if (Config.Default.DiscordBotWhitelist != null)
|
||||||
|
{
|
||||||
|
discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSourceDiscordBot.Token);
|
||||||
|
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
// cleanup the token
|
||||||
|
_tokenSourceDiscordBot.Dispose();
|
||||||
|
_tokenSourceDiscordBot = null;
|
||||||
|
}
|
||||||
|
DiscordBotStarted = false;
|
||||||
|
}, _tokenSourceDiscordBot.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);
|
||||||
|
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
// cleanup the token
|
||||||
|
_tokenSourceDiscordBot.Dispose();
|
||||||
|
_tokenSourceDiscordBot = null;
|
||||||
|
}
|
||||||
|
DiscordBotStarted = false;
|
||||||
|
}, TaskContinuationOptions.OnlyOnFaulted);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopDiscordBot()
|
||||||
|
{
|
||||||
|
if (!(_tokenSourceDiscordBot is null))
|
||||||
|
{
|
||||||
|
_tokenSourceDiscordBot.Cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ namespace ServerManagerTool
|
||||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
|
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
|
||||||
|
|
||||||
|
public static readonly DependencyProperty AppInstanceProperty = DependencyProperty.Register(nameof(AppInstance), typeof(App), typeof(GlobalSettingsControl), new PropertyMetadata(null));
|
||||||
public static readonly DependencyProperty IsAdministratorProperty = DependencyProperty.Register(nameof(IsAdministrator), typeof(bool), typeof(GlobalSettingsControl), new PropertyMetadata(false));
|
public static readonly DependencyProperty IsAdministratorProperty = DependencyProperty.Register(nameof(IsAdministrator), typeof(bool), typeof(GlobalSettingsControl), new PropertyMetadata(false));
|
||||||
public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register(nameof(Config), typeof(Config), typeof(GlobalSettingsControl), new PropertyMetadata(null));
|
public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register(nameof(Config), typeof(Config), typeof(GlobalSettingsControl), new PropertyMetadata(null));
|
||||||
public static readonly DependencyProperty CommonConfigProperty = DependencyProperty.Register(nameof(CommonConfig), typeof(CommonConfig), typeof(GlobalSettingsControl), new PropertyMetadata(null));
|
public static readonly DependencyProperty CommonConfigProperty = DependencyProperty.Register(nameof(CommonConfig), typeof(CommonConfig), typeof(GlobalSettingsControl), new PropertyMetadata(null));
|
||||||
|
|
@ -38,11 +39,11 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
public GlobalSettingsControl()
|
public GlobalSettingsControl()
|
||||||
{
|
{
|
||||||
this.Version = GetDeployedVersion();
|
this.AppInstance = App.Instance;
|
||||||
|
|
||||||
this.Config = Config.Default;
|
this.Config = Config.Default;
|
||||||
this.CommonConfig = CommonConfig.Default;
|
this.CommonConfig = CommonConfig.Default;
|
||||||
this.DataContext = this;
|
this.IsAdministrator = SecurityUtils.IsAdministrator();
|
||||||
|
this.Version = GetDeployedVersion();
|
||||||
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
WindowUtils.RemoveDefaultResourceDictionary(this, Config.Default.DefaultGlobalizationFile);
|
WindowUtils.RemoveDefaultResourceDictionary(this, Config.Default.DefaultGlobalizationFile);
|
||||||
|
|
@ -59,7 +60,13 @@ namespace ServerManagerTool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.IsAdministrator = SecurityUtils.IsAdministrator();
|
this.DataContext = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public App AppInstance
|
||||||
|
{
|
||||||
|
get { return GetValue(AppInstanceProperty) as App; }
|
||||||
|
set { SetValue(AppInstanceProperty, value); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Version
|
public string Version
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
private CancellationTokenSource _tokenSource;
|
private CancellationTokenSource _tokenSourceDiscordBot;
|
||||||
private GlobalizedApplication _globalizer;
|
private GlobalizedApplication _globalizer;
|
||||||
private bool _applicationStarted;
|
private bool _applicationStarted;
|
||||||
private string _args;
|
private string _args;
|
||||||
|
|
@ -150,6 +150,18 @@ namespace ServerManagerTool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool DiscordBotStarted
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _tokenSourceDiscordBot != null;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
OnPropertyChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Title
|
public string Title
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -479,27 +491,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
if (Config.Default.DiscordBotEnabled)
|
if (Config.Default.DiscordBotEnabled)
|
||||||
{
|
{
|
||||||
_tokenSource = new CancellationTokenSource();
|
StartDiscordBot();
|
||||||
|
|
||||||
Task discordTask = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
var discordWhiteList = new List<string>();
|
|
||||||
if (Config.Default.DiscordBotWhitelist != null)
|
|
||||||
{
|
|
||||||
discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast<string>());
|
|
||||||
}
|
|
||||||
|
|
||||||
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken,Config.Default.DiscordBotPrefix, Config.Default.DataPath, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -541,11 +533,7 @@ namespace ServerManagerTool
|
||||||
|
|
||||||
private void ShutDownApplication()
|
private void ShutDownApplication()
|
||||||
{
|
{
|
||||||
if (!(_tokenSource is null))
|
StopDiscordBot();
|
||||||
{
|
|
||||||
_tokenSource.Cancel();
|
|
||||||
_tokenSource.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.ApplicationStarted)
|
if (this.ApplicationStarted)
|
||||||
{
|
{
|
||||||
|
|
@ -586,5 +574,60 @@ namespace ServerManagerTool
|
||||||
SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder);
|
SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder);
|
||||||
SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder);
|
SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void StartDiscordBot()
|
||||||
|
{
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_tokenSourceDiscordBot = new CancellationTokenSource();
|
||||||
|
DiscordBotStarted = true;
|
||||||
|
|
||||||
|
Task discordTask = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
var discordWhiteList = new List<string>();
|
||||||
|
if (Config.Default.DiscordBotWhitelist != null)
|
||||||
|
{
|
||||||
|
discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataPath, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSourceDiscordBot.Token);
|
||||||
|
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
// cleanup the token
|
||||||
|
_tokenSourceDiscordBot.Dispose();
|
||||||
|
_tokenSourceDiscordBot = null;
|
||||||
|
}
|
||||||
|
DiscordBotStarted = false;
|
||||||
|
}, _tokenSourceDiscordBot.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);
|
||||||
|
|
||||||
|
if (_tokenSourceDiscordBot != null)
|
||||||
|
{
|
||||||
|
// cleanup the token
|
||||||
|
_tokenSourceDiscordBot.Dispose();
|
||||||
|
_tokenSourceDiscordBot = null;
|
||||||
|
}
|
||||||
|
DiscordBotStarted = false;
|
||||||
|
}, TaskContinuationOptions.OnlyOnFaulted);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopDiscordBot()
|
||||||
|
{
|
||||||
|
if (!(_tokenSourceDiscordBot is null))
|
||||||
|
{
|
||||||
|
_tokenSourceDiscordBot.Cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue