mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +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;
|
||||
|
||||
private CancellationTokenSource _tokenSource;
|
||||
private CancellationTokenSource _tokenSourceDiscordBot;
|
||||
private GlobalizedApplication _globalizer;
|
||||
private bool _applicationStarted;
|
||||
private string _args;
|
||||
|
|
@ -151,6 +151,18 @@ namespace ServerManagerTool
|
|||
}
|
||||
}
|
||||
|
||||
public bool DiscordBotStarted
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tokenSourceDiscordBot != null;
|
||||
}
|
||||
set
|
||||
{
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
|
|
@ -498,27 +510,7 @@ namespace ServerManagerTool
|
|||
|
||||
if (Config.Default.DiscordBotEnabled)
|
||||
{
|
||||
_tokenSource = new CancellationTokenSource();
|
||||
|
||||
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);
|
||||
StartDiscordBot();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -560,11 +552,7 @@ namespace ServerManagerTool
|
|||
|
||||
private void ShutDownApplication()
|
||||
{
|
||||
if (!(_tokenSource is null))
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
_tokenSource.Dispose();
|
||||
}
|
||||
StopDiscordBot();
|
||||
|
||||
if (ApplicationStarted)
|
||||
{
|
||||
|
|
@ -605,5 +593,60 @@ namespace ServerManagerTool
|
|||
SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue