mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
GetServerInfo, GetServerList and GetServerStatus done
This commit is contained in:
parent
d526a3f457
commit
824daed0d1
27 changed files with 484 additions and 91 deletions
|
|
@ -4,7 +4,7 @@ using NLog.Config;
|
|||
using NLog.Targets;
|
||||
using ServerManagerTool.Common;
|
||||
using ServerManagerTool.Common.Utils;
|
||||
using ServerManagerTool.Discord;
|
||||
using ServerManagerTool.DiscordBot;
|
||||
using ServerManagerTool.Enums;
|
||||
using ServerManagerTool.Lib;
|
||||
using ServerManagerTool.Plugin.Common;
|
||||
|
|
@ -456,7 +456,7 @@ namespace ServerManagerTool
|
|||
|
||||
Task discordTask = Task.Run(async () =>
|
||||
{
|
||||
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, DiscordBotHelper.HandleDiscordCommand, _tokenSource.Token);
|
||||
await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSource.Token);
|
||||
}, _tokenSource.Token)
|
||||
.ContinueWith(t => {
|
||||
var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message;
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@
|
|||
public enum AvailabilityStatus
|
||||
{
|
||||
Unknown,
|
||||
NeedPublicIP,
|
||||
SetPublicIP,
|
||||
Unavailable,
|
||||
WaitingForPublication,
|
||||
Waiting,
|
||||
Available
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5544,6 +5544,18 @@
|
|||
<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>
|
||||
|
||||
<sys:String x:Key="DiscordBot_CommandNotImplemented">Command '{0}' has not been implemented.</sys:String>
|
||||
<sys:String x:Key="DiscordBot_CommandUnknown">Unknown command '{0}'.</sys:String>
|
||||
<sys:String x:Key="DiscordBot_CommandRunning">Another command is currently running.</sys:String>
|
||||
|
||||
<sys:String x:Key="DiscordBot_CommandProfileMissing">The command requires a profile id.</sys:String>
|
||||
<sys:String x:Key="DiscordBot_CommandProfileNotFound">Profile id {0} not found or is not associated with the channel.</sys:String>
|
||||
|
||||
<sys:String x:Key="DiscordBot_CommandInfoFailed">Call to the server '{0}' failed.</sys:String>
|
||||
|
||||
<sys:String x:Key="DiscordBot_CountLabel">Count:</sys:String>
|
||||
<sys:String x:Key="DiscordBot_MapLabel">Map:</sys:String>
|
||||
<!--#endregion-->
|
||||
|
||||
</Globalization:GlobalizationResourceDictionary>
|
||||
|
|
@ -281,13 +281,13 @@ namespace ServerManagerTool.Lib
|
|||
|
||||
case WatcherServerStatus.RunningLocalCheck:
|
||||
if (oldStatus != ServerStatus.Stopping)
|
||||
UpdateServerStatus(ServerStatus.Running, this.Availability != AvailabilityStatus.Available ? AvailabilityStatus.WaitingForPublication : this.Availability, oldStatus != ServerStatus.Running && oldStatus != ServerStatus.Unknown);
|
||||
UpdateServerStatus(ServerStatus.Running, this.Availability != AvailabilityStatus.Available ? AvailabilityStatus.Waiting : this.Availability, oldStatus != ServerStatus.Running && oldStatus != ServerStatus.Unknown);
|
||||
if (this.ProfileSnapshot.MOTDIntervalEnabled && this.motdIntervalTimer != null && !this.motdIntervalTimer.Enabled) this.motdIntervalTimer.Start();
|
||||
break;
|
||||
|
||||
case WatcherServerStatus.RunningExternalCheck:
|
||||
if (oldStatus != ServerStatus.Stopping)
|
||||
UpdateServerStatus(ServerStatus.Running, AvailabilityStatus.WaitingForPublication, oldStatus != ServerStatus.Running && oldStatus != ServerStatus.Unknown);
|
||||
UpdateServerStatus(ServerStatus.Running, AvailabilityStatus.Waiting, oldStatus != ServerStatus.Running && oldStatus != ServerStatus.Unknown);
|
||||
if (this.ProfileSnapshot.MOTDIntervalEnabled && this.motdIntervalTimer != null && !this.motdIntervalTimer.Enabled) this.motdIntervalTimer.Start();
|
||||
break;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,189 @@
|
|||
using ServerManagerTool.Discord.Enums;
|
||||
using QueryMaster;
|
||||
using ServerManagerTool.Common.Utils;
|
||||
using ServerManagerTool.DiscordBot.Enums;
|
||||
using ServerManagerTool.Lib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using WPFSharp.Globalizer;
|
||||
|
||||
namespace ServerManagerTool.Utils
|
||||
{
|
||||
internal static class DiscordBotHelper
|
||||
{
|
||||
private static readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
|
||||
private static bool _runningCommand = false;
|
||||
|
||||
public static IList<string> HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { $"{commandType}; {serverId}; {channelId}; {profileId ?? "no profile"}" };
|
||||
// check if incoming values are valid
|
||||
if (string.IsNullOrWhiteSpace(serverId) || string.IsNullOrWhiteSpace(channelId))
|
||||
return null;
|
||||
|
||||
// check if the server ids match
|
||||
if (!serverId.Equals(Config.Default.DiscordBotServerId))
|
||||
return new List<string>();
|
||||
|
||||
if (_runningCommand)
|
||||
return new List<string> { _globalizer.GetResourceString("DiscordBot_CommandRunning") };
|
||||
_runningCommand = true;
|
||||
|
||||
try
|
||||
{
|
||||
switch (commandType)
|
||||
{
|
||||
case CommandType.Info:
|
||||
return GetServerInfo(channelId, profileId);
|
||||
case CommandType.List:
|
||||
return GetServerList(channelId);
|
||||
case CommandType.Status:
|
||||
return GetServerStatus(channelId, profileId);
|
||||
|
||||
case CommandType.Backup:
|
||||
return BackupServer(channelId, profileId);
|
||||
case CommandType.Shutdown:
|
||||
return ShutdownServer(channelId, profileId);
|
||||
case CommandType.Stop:
|
||||
return StopServer(channelId, profileId);
|
||||
case CommandType.Start:
|
||||
return StartServer(channelId, profileId);
|
||||
case CommandType.Update:
|
||||
return UpdateServer(channelId, profileId);
|
||||
|
||||
default:
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), commandType) };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var message = ex.InnerException is null ? ex.Message : ex.InnerException.Message;
|
||||
return new string[] { message };
|
||||
}
|
||||
finally
|
||||
{
|
||||
_runningCommand = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string HandleTranslation(string translationKey)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(translationKey) ? string.Empty : _globalizer.GetResourceString(translationKey) ?? translationKey;
|
||||
}
|
||||
|
||||
private static IList<string> GetServerInfo(string channelId, string profileId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(profileId))
|
||||
{
|
||||
return new List<string> { _globalizer.GetResourceString("DiscordBot_CommandProfileMissing") };
|
||||
}
|
||||
|
||||
var serverName = string.Empty;
|
||||
var serverIp = IPAddress.Loopback;
|
||||
var queryPort = 0;
|
||||
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var server = ServerManager.Instance.Servers.Where(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID)).FirstOrDefault();
|
||||
|
||||
if (server is null)
|
||||
{
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_CommandProfileNotFound"), profileId));
|
||||
}
|
||||
|
||||
serverName = server.Profile.ServerName;
|
||||
if (!string.IsNullOrWhiteSpace(server.Profile.ServerIP))
|
||||
{
|
||||
IPAddress.TryParse(server.Profile.ServerIP, out serverIp);
|
||||
}
|
||||
queryPort = server.Profile.QueryPort;
|
||||
}).Wait();
|
||||
|
||||
List<string> response = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
using (var gameServer = ServerQuery.GetServerInstance(EngineType.Source, new IPEndPoint(serverIp, queryPort)))
|
||||
{
|
||||
var info = gameServer?.GetInfo();
|
||||
if (info is null)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_CommandInfoFailed"), serverName));
|
||||
}
|
||||
else
|
||||
{
|
||||
var mapName = _globalizer.GetResourceString($"Map_{info.Map}") ?? info.Map;
|
||||
response.Add($"```{info.Name}\n{_globalizer.GetResourceString("DiscordBot_MapLabel")} {mapName}\n{_globalizer.GetResourceString("ServerSettings_PlayersLabel")} {info.Players} / {info.MaxPlayers}```");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_CommandInfoFailed"), serverName));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static IList<string> GetServerList(string channelId)
|
||||
{
|
||||
List<string> response = new List<string>();
|
||||
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var serverList = ServerManager.Instance.Servers.Where(s => Equals(channelId, s.Profile.DiscordChannelId));
|
||||
|
||||
response.Add($"**{_globalizer.GetResourceString("DiscordBot_CountLabel")}** {serverList.Count()}");
|
||||
foreach (var server in serverList)
|
||||
{
|
||||
response.Add($"```{_globalizer.GetResourceString("ServerSettings_ProfileIdLabel")} {server.Profile.ProfileID}\n{_globalizer.GetResourceString("ServerSettings_ProfileLabel")} {server.Profile.ProfileName}\n{_globalizer.GetResourceString("ServerSettings_ServerNameLabel")} {server.Profile.ServerName}```");
|
||||
}
|
||||
}).Wait();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static IList<string> GetServerStatus(string channelId, string profileId)
|
||||
{
|
||||
List<string> response = new List<string>();
|
||||
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var serverList = ServerManager.Instance.Servers.Where(s => Equals(channelId, s.Profile.DiscordChannelId) && (string.IsNullOrWhiteSpace(profileId) || Equals(profileId, s.Profile.ProfileID)));
|
||||
|
||||
response.Add($"**{_globalizer.GetResourceString("DiscordBot_CountLabel")}** {serverList.Count()}");
|
||||
foreach (var server in serverList)
|
||||
{
|
||||
response.Add($"```{_globalizer.GetResourceString("ServerSettings_ProfileLabel")} {server.Profile.ProfileName}\n{_globalizer.GetResourceString("ServerSettings_ServerNameLabel")} {server.Profile.ServerName}\n{_globalizer.GetResourceString("ServerSettings_StatusLabel")} {server.Runtime.StatusString}\n{_globalizer.GetResourceString("ServerSettings_AvailabilityLabel")} {_globalizer.GetResourceString($"ServerSettings_Availability_{server.Runtime.Availability}")}```");
|
||||
}
|
||||
}).Wait();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static IList<string> BackupServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Backup) };
|
||||
}
|
||||
|
||||
private static IList<string> ShutdownServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Shutdown) };
|
||||
}
|
||||
|
||||
private static IList<string> StopServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Stop) };
|
||||
}
|
||||
|
||||
private static IList<string> StartServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Start) };
|
||||
}
|
||||
|
||||
private static IList<string> UpdateServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Update) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -420,13 +420,13 @@
|
|||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.Unknown}">
|
||||
<Setter Property="Content" Value="{DynamicResource ServerSettings_Availability_Unknown}"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.NeedPublicIP}">
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.SetPublicIP}">
|
||||
<Setter Property="Content" Value="{DynamicResource ServerSettings_Availability_SetPublicIP}"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.Unavailable}">
|
||||
<Setter Property="Content" Value="{DynamicResource ServerSettings_Availability_Unavailable}"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.WaitingForPublication}">
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.Waiting}">
|
||||
<Setter Property="Content" Value="{DynamicResource ServerSettings_Availability_Waiting}"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Availability}" Value="{x:Static enum:AvailabilityStatus.Available}">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue