GetServerInfo, GetServerList and GetServerStatus done

This commit is contained in:
Brett Hewitson 2021-12-04 21:47:16 +10:00
parent d526a3f457
commit 824daed0d1
27 changed files with 484 additions and 91 deletions

View file

@ -1,7 +1,7 @@
using ServerManagerTool.Discord.Enums;
using ServerManagerTool.DiscordBot.Enums;
using System.Collections.Generic;
namespace ServerManagerTool.Discord.Delegates
namespace ServerManagerTool.DiscordBot.Delegates
{
public delegate IList<string> HandleCommandDelegate(CommandType commandType, string serverId, string channelId, string profileId);
}

View file

@ -0,0 +1,4 @@
namespace ServerManagerTool.DiscordBot.Delegates
{
public delegate string HandleTranslationDelegate(string translationKey);
}

View file

@ -1,6 +1,6 @@
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.DiscordBot.Delegates;
namespace ServerManagerTool.Discord
namespace ServerManagerTool.DiscordBot
{
public static class DiscordBot
{

View file

@ -1,14 +1,15 @@
namespace ServerManagerTool.Discord.Enums
namespace ServerManagerTool.DiscordBot.Enums
{
public enum CommandType
{
BackupServer,
ServerInfo,
ServerList,
ServerStatus,
ShutdownServer,
StartServer,
StopServer,
UpdateServer,
Info,
List,
Status,
Backup,
Shutdown,
Start,
Stop,
Update,
}
}

View file

@ -1,11 +1,11 @@
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.DiscordBot.Delegates;
using System.Threading;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Interfaces
namespace ServerManagerTool.DiscordBot.Interfaces
{
public interface IServerManagerBot
{
Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token);
Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
}
}

View file

@ -6,7 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Modules
namespace ServerManagerTool.DiscordBot.Modules
{
[Name("Help")]
public sealed class HelpModule : ModuleBase<SocketCommandContext>
@ -26,6 +26,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("help")]
[Summary("Provides a list of available commands")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task HelpAsync()
{
var prefix = _config["DiscordSettings:Prefix"];
@ -102,6 +103,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("help")]
[Summary("Searches a list of available commands")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task HelpAsync(string command)
{
var searchResults = _service.Search(Context, command);

View file

@ -1,12 +1,13 @@
using Discord.Addons.Interactive;
using Discord;
using Discord.Addons.Interactive;
using Discord.Commands;
using Microsoft.Extensions.Configuration;
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.Discord.Enums;
using ServerManagerTool.DiscordBot.Delegates;
using ServerManagerTool.DiscordBot.Enums;
using System;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Modules
namespace ServerManagerTool.DiscordBot.Modules
{
[Name("Server Commands")]
public sealed class ServerCommandModule : InteractiveBase
@ -25,6 +26,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("backup", RunMode = RunMode.Async)]
[Summary("Perform a backup of the server")]
[Remarks("backup")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task BackupServerAsync()
{
await BackupServerAsync(null);
@ -33,6 +35,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("backup", RunMode = RunMode.Async)]
[Summary("Perform a backup of the server")]
[Remarks("backup profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task BackupServerAsync(string profileId)
{
try
@ -40,8 +43,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.BackupServer, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Backup, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -49,7 +52,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -63,6 +66,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("shutdown", RunMode = RunMode.Async)]
[Summary("Shuts down the server properly")]
[Remarks("shutdown")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ShutdownServerAsync()
{
await ShutdownServerAsync(null);
@ -71,6 +75,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("shutdown", RunMode = RunMode.Async)]
[Summary("Shuts down the server properly")]
[Remarks("shutdown profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ShutdownServerAsync(string profileId)
{
try
@ -78,8 +83,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.ShutdownServer, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Shutdown, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -87,7 +92,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -101,6 +106,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("start", RunMode = RunMode.Async)]
[Summary("Starts the server")]
[Remarks("start")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task StartServerAsync()
{
await StartServerAsync(null);
@ -109,6 +115,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("start", RunMode = RunMode.Async)]
[Summary("Starts the server")]
[Remarks("start profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task StartServerAsync(string profileId)
{
try
@ -116,8 +123,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.StartServer, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Start, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -125,7 +132,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -139,6 +146,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("stop", RunMode = RunMode.Async)]
[Summary("Forcibly stops the server")]
[Remarks("stop")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task StopServerAsync()
{
await StopServerAsync(null);
@ -147,6 +155,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("stop", RunMode = RunMode.Async)]
[Summary("Forcibly stops the server")]
[Remarks("stop profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task StopServerAsync(string profileId)
{
try
@ -154,8 +163,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.StopServer, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Stop, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -163,7 +172,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -177,6 +186,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("update", RunMode = RunMode.Async)]
[Summary("Updates the server")]
[Remarks("update")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task UpdateServerAsync()
{
await UpdateServerAsync(null);
@ -185,6 +195,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("update", RunMode = RunMode.Async)]
[Summary("Updates the server")]
[Remarks("update profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task UpdateServerAsync(string profileId)
{
try
@ -192,8 +203,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.UpdateServer, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Update, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -201,7 +212,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}

View file

@ -1,12 +1,13 @@
using Discord.Addons.Interactive;
using Discord;
using Discord.Addons.Interactive;
using Discord.Commands;
using Microsoft.Extensions.Configuration;
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.Discord.Enums;
using ServerManagerTool.DiscordBot.Delegates;
using ServerManagerTool.DiscordBot.Enums;
using System;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Modules
namespace ServerManagerTool.DiscordBot.Modules
{
[Name("Server Query")]
public sealed class ServerQueryModule : InteractiveBase
@ -25,6 +26,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("info", RunMode = RunMode.Async)]
[Summary("Poll server for information")]
[Remarks("info")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ServerInfoAsync()
{
await ServerInfoAsync(null);
@ -33,6 +35,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("info", RunMode = RunMode.Async)]
[Summary("Poll server for information")]
[Remarks("info profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ServerInfoAsync(string profileId)
{
try
@ -40,8 +43,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.ServerInfo, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Info, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -49,7 +52,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -63,6 +66,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("list", RunMode = RunMode.Async)]
[Summary("List of all servers associated with this channel")]
[Remarks("list")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ServerListAsync()
{
try
@ -70,8 +74,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.ServerList, serverId, channelId, null);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.List, serverId, channelId, null);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -79,7 +83,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
@ -93,6 +97,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("status", RunMode = RunMode.Async)]
[Summary("Poll server for status")]
[Remarks("status")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ServerStatusAsync()
{
await ServerStatusAsync(null);
@ -101,6 +106,7 @@ namespace ServerManagerTool.Discord.Modules
[Command("status", RunMode = RunMode.Async)]
[Summary("Poll server for status")]
[Remarks("status profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task ServerStatusAsync(string profileId)
{
try
@ -108,8 +114,8 @@ namespace ServerManagerTool.Discord.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.ServerStatus, serverId, channelId, profileId);
if (response is null || response.Count == 0)
var response = _handleCommandCallback?.Invoke(CommandType.Status, serverId, channelId, profileId);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
@ -117,7 +123,7 @@ namespace ServerManagerTool.Discord.Modules
{
foreach (var output in response)
{
await ReplyAsync(output);
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}

View file

@ -5,7 +5,7 @@
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>ServerManagerTool.Discord</RootNamespace>
<RootNamespace>ServerManagerTool.DiscordBot</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>

View file

@ -5,9 +5,9 @@ using Discord.Net.Providers.WS4Net;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.Discord.Interfaces;
using ServerManagerTool.Discord.Services;
using ServerManagerTool.DiscordBot.Delegates;
using ServerManagerTool.DiscordBot.Interfaces;
using ServerManagerTool.DiscordBot.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -15,7 +15,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord
namespace ServerManagerTool.DiscordBot
{
public sealed class ServerManagerBot : IServerManagerBot
{
@ -30,7 +30,7 @@ namespace ServerManagerTool.Discord
set;
}
public async Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token)
public async Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token)
{
if (Started)
{
@ -38,7 +38,7 @@ namespace ServerManagerTool.Discord
}
Started = true;
if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken) || handleCommandCallback is null)
if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken) || handleTranslationCallback is null || handleCommandCallback is null)
{
return;
}
@ -55,8 +55,8 @@ namespace ServerManagerTool.Discord
var settings = new Dictionary<string, string>
{
{ "DiscordSettings:Prefix", commandPrefix },
{ "DiscordSettings:Token", discordToken },
{ "DiscordSettings:Prefix", commandPrefix },
{ "ServerManager:DataDirectory", dataDirectory }
};
@ -106,7 +106,8 @@ namespace ServerManagerTool.Discord
.AddSingleton<ShutdownService>()
.AddSingleton<Random>()
.AddSingleton(config)
.AddSingleton(handleCommandCallback);
.AddSingleton(handleCommandCallback)
.AddSingleton(handleTranslationCallback);
// Create the service provider
using (var provider = services.BuildServiceProvider())

View file

@ -1,6 +1,6 @@
using ServerManagerTool.Discord.Interfaces;
using ServerManagerTool.DiscordBot.Interfaces;
namespace ServerManagerTool.Discord
namespace ServerManagerTool.DiscordBot
{
public static class ServerManagerBotFactory
{

View file

@ -4,7 +4,7 @@ using Microsoft.Extensions.Configuration;
using System;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Services
namespace ServerManagerTool.DiscordBot.Services
{
public class CommandHandlerService
{

View file

@ -6,7 +6,7 @@ using System;
using System.IO;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Services
namespace ServerManagerTool.DiscordBot.Services
{
public class LoggingService
{

View file

@ -1,7 +1,7 @@
using Discord.WebSocket;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Services
namespace ServerManagerTool.DiscordBot.Services
{
public class ShutdownService
{

View file

@ -6,7 +6,7 @@ using System;
using System.Reflection;
using System.Threading.Tasks;
namespace ServerManagerTool.Discord.Services
namespace ServerManagerTool.DiscordBot.Services
{
public class StartupService
{