mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Added Discord Bot Status and a button to Stop/Start the discord bot
This commit is contained in:
parent
a2b811972f
commit
13713b7092
18 changed files with 439 additions and 16 deletions
|
|
@ -30,6 +30,9 @@ namespace ServerManagerTool
|
|||
private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
|
||||
private readonly ActionQueue versionChecker;
|
||||
private readonly ActionQueue scheduledTaskChecker;
|
||||
private readonly ActionQueue discordBotChecker;
|
||||
|
||||
private bool discordBotStateClicked = false;
|
||||
|
||||
public static readonly DependencyProperty AppInstanceProperty = DependencyProperty.Register(nameof(AppInstance), typeof(App), typeof(MainWindow), new PropertyMetadata(null));
|
||||
public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register(nameof(Config), typeof(Config), typeof(MainWindow), new PropertyMetadata(null));
|
||||
|
|
@ -40,6 +43,8 @@ namespace ServerManagerTool
|
|||
public static readonly DependencyProperty AutoUpdateStateProperty = DependencyProperty.Register(nameof(AutoUpdateState), typeof(Microsoft.Win32.TaskScheduler.TaskState), typeof(MainWindow), new PropertyMetadata(Microsoft.Win32.TaskScheduler.TaskState.Unknown));
|
||||
public static readonly DependencyProperty AutoUpdateStateStringProperty = DependencyProperty.Register(nameof(AutoUpdateStateString), typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
|
||||
public static readonly DependencyProperty AutoUpdateNextRunTimeProperty = DependencyProperty.Register(nameof(AutoUpdateNextRunTime), typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
|
||||
public static readonly DependencyProperty DiscordBotStateProperty = DependencyProperty.Register(nameof(DiscordBotState), typeof(DiscordBot.Enums.BotState), typeof(MainWindow), new PropertyMetadata(DiscordBot.Enums.BotState.Unknown));
|
||||
public static readonly DependencyProperty DiscordBotStateStringProperty = DependencyProperty.Register(nameof(DiscordBotStateString), typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));
|
||||
public static readonly DependencyProperty IsIpValidProperty = DependencyProperty.Register(nameof(IsIpValid), typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
|
||||
public static readonly DependencyProperty LatestServerManagerVersionProperty = DependencyProperty.Register(nameof(LatestServerManagerVersion), typeof(Version), typeof(MainWindow), new PropertyMetadata(new Version()));
|
||||
public static readonly DependencyProperty NewServerManagerAvailableProperty = DependencyProperty.Register(nameof(NewServerManagerAvailable), typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
|
||||
|
|
@ -57,6 +62,7 @@ namespace ServerManagerTool
|
|||
this.DataContext = this;
|
||||
this.versionChecker = new ActionQueue();
|
||||
this.scheduledTaskChecker = new ActionQueue();
|
||||
this.discordBotChecker = new ActionQueue();
|
||||
|
||||
IsAdministrator = SecurityUtils.IsAdministrator();
|
||||
if (!string.IsNullOrWhiteSpace(App.Instance.Title))
|
||||
|
|
@ -139,6 +145,18 @@ namespace ServerManagerTool
|
|||
set { SetValue(AutoUpdateNextRunTimeProperty, value); }
|
||||
}
|
||||
|
||||
public DiscordBot.Enums.BotState DiscordBotState
|
||||
{
|
||||
get { return (DiscordBot.Enums.BotState)GetValue(DiscordBotStateProperty); }
|
||||
set { SetValue(DiscordBotStateProperty, value); }
|
||||
}
|
||||
|
||||
public string DiscordBotStateString
|
||||
{
|
||||
get { return (string)GetValue(DiscordBotStateStringProperty); }
|
||||
set { SetValue(DiscordBotStateStringProperty, value); }
|
||||
}
|
||||
|
||||
public bool IsAdministrator
|
||||
{
|
||||
get;
|
||||
|
|
@ -191,6 +209,7 @@ namespace ServerManagerTool
|
|||
|
||||
this.versionChecker.PostAction(CheckForUpdates).DoNotWait();
|
||||
this.scheduledTaskChecker.PostAction(CheckForScheduledTasks).DoNotWait();
|
||||
this.discordBotChecker.PostAction(CheckForDiscordBot).DoNotWait();
|
||||
}
|
||||
|
||||
private void MainWindow_LocationChanged(object sender, EventArgs e)
|
||||
|
|
@ -254,6 +273,7 @@ namespace ServerManagerTool
|
|||
this.Title = _globalizer.GetResourceString("MainWindow_Title");
|
||||
|
||||
this.scheduledTaskChecker.PostAction(CheckForScheduledTasks).DoNotWait();
|
||||
this.discordBotChecker.PostAction(CheckForDiscordBot).DoNotWait();
|
||||
}
|
||||
|
||||
private void PatchNotes_Click(object sender, RoutedEventArgs e)
|
||||
|
|
@ -563,6 +583,36 @@ namespace ServerManagerTool
|
|||
}
|
||||
}
|
||||
|
||||
private async void DiscordBotTaskState_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (discordBotStateClicked)
|
||||
return;
|
||||
discordBotStateClicked = true;
|
||||
|
||||
try
|
||||
{
|
||||
switch (DiscordBotState)
|
||||
{
|
||||
case DiscordBot.Enums.BotState.Stopped:
|
||||
AppInstance.StartDiscordBot();
|
||||
break;
|
||||
case DiscordBot.Enums.BotState.Running:
|
||||
AppInstance.StopDiscordBot();
|
||||
break;
|
||||
}
|
||||
|
||||
await Task.Delay(5000);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
finally
|
||||
{
|
||||
discordBotStateClicked = false;
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand ShowWindowCommand
|
||||
{
|
||||
get
|
||||
|
|
@ -654,8 +704,8 @@ namespace ServerManagerTool
|
|||
this.AutoBackupState = backupState;
|
||||
this.AutoUpdateState = updateState;
|
||||
|
||||
this.AutoBackupStateString = GetTaskStateString(AutoBackupState);
|
||||
this.AutoUpdateStateString = GetTaskStateString(AutoUpdateState);
|
||||
this.AutoBackupStateString = GetTaskSchedulerStateString(AutoBackupState);
|
||||
this.AutoUpdateStateString = GetTaskSchedulerStateString(AutoUpdateState);
|
||||
|
||||
this.AutoBackupNextRunTime = backupnextRunTime == DateTime.MinValue ? string.Empty : $"{_globalizer.GetResourceString("MainWindow_TaskRunTimeLabel")} {backupnextRunTime:G}";
|
||||
this.AutoUpdateNextRunTime = updatenextRunTime == DateTime.MinValue ? string.Empty : $"{_globalizer.GetResourceString("MainWindow_TaskRunTimeLabel")} {updatenextRunTime:G}";
|
||||
|
|
@ -672,7 +722,46 @@ namespace ServerManagerTool
|
|||
this.scheduledTaskChecker.PostAction(CheckForScheduledTasks).DoNotWait();
|
||||
}
|
||||
|
||||
private string GetTaskStateString(Microsoft.Win32.TaskScheduler.TaskState taskState)
|
||||
private async Task CheckForDiscordBot()
|
||||
{
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var botState = DiscordBot.Enums.BotState.Unknown;
|
||||
if (AppInstance.DiscordBotStarted)
|
||||
{
|
||||
botState = DiscordBot.Enums.BotState.Running;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Config.DiscordBotEnabled)
|
||||
{
|
||||
botState = DiscordBot.Enums.BotState.Stopped;
|
||||
}
|
||||
else
|
||||
{
|
||||
botState = DiscordBot.Enums.BotState.Disabled;
|
||||
}
|
||||
}
|
||||
|
||||
this.DiscordBotState = botState;
|
||||
|
||||
this.DiscordBotStateString = GetDiscordBotStateString(botState);
|
||||
|
||||
Logger.Info($"{nameof(CheckForDiscordBot)} performed");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
}).DoNotWait();
|
||||
|
||||
await Task.Delay(Config.Default.DiscordBotStatusCheckTime * 1 * 1000);
|
||||
this.discordBotChecker.PostAction(CheckForDiscordBot).DoNotWait();
|
||||
}
|
||||
|
||||
private string GetTaskSchedulerStateString(Microsoft.Win32.TaskScheduler.TaskState taskState)
|
||||
{
|
||||
switch (taskState)
|
||||
{
|
||||
|
|
@ -691,6 +780,21 @@ namespace ServerManagerTool
|
|||
}
|
||||
}
|
||||
|
||||
private string GetDiscordBotStateString(DiscordBot.Enums.BotState botState)
|
||||
{
|
||||
switch (botState)
|
||||
{
|
||||
case DiscordBot.Enums.BotState.Disabled:
|
||||
return _globalizer.GetResourceString("MainWindow_TaskStateDisabledLabel");
|
||||
case DiscordBot.Enums.BotState.Running:
|
||||
return _globalizer.GetResourceString("MainWindow_TaskStateRunningLabel");
|
||||
case DiscordBot.Enums.BotState.Stopped:
|
||||
return _globalizer.GetResourceString("MainWindow_TaskStateStoppedLabel");
|
||||
default:
|
||||
return _globalizer.GetResourceString("MainWindow_TaskStateUnknownLabel");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StartServerAsync(Server server)
|
||||
{
|
||||
if (server == null || server.Profile == null || server.Runtime == null || server.Runtime.Status != ServerStatus.Stopped)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue