Finished the remaing bot commands

This commit is contained in:
Brett Hewitson 2021-12-05 15:08:37 +10:00
parent 0f3c6e6be9
commit aa62646f0f
23 changed files with 1041 additions and 287 deletions

View file

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

View file

@ -7,6 +7,7 @@
Status,
Backup,
Restart,
Shutdown,
Start,
Stop,

View file

@ -6,6 +6,8 @@ namespace ServerManagerTool.DiscordBot.Interfaces
{
public interface IServerManagerBot
{
CancellationToken Token { get; }
Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
}
}

View file

@ -4,7 +4,9 @@ using Discord.Commands;
using Microsoft.Extensions.Configuration;
using ServerManagerTool.DiscordBot.Delegates;
using ServerManagerTool.DiscordBot.Enums;
using ServerManagerTool.DiscordBot.Interfaces;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ServerManagerTool.DiscordBot.Modules
@ -12,19 +14,21 @@ namespace ServerManagerTool.DiscordBot.Modules
[Name("Server Commands")]
public sealed class ServerCommandModule : InteractiveBase
{
private readonly IServerManagerBot _serverManagerBot;
private readonly CommandService _service;
private readonly HandleCommandDelegate _handleCommandCallback;
private readonly IConfigurationRoot _config;
public ServerCommandModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
public ServerCommandModule(IServerManagerBot serverManagerBot, CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
{
_serverManagerBot = serverManagerBot;
_service = service;
_handleCommandCallback = handleCommandCallback;
_config = config;
}
[Command("backup", RunMode = RunMode.Async)]
[Summary("Perform a backup of the server")]
[Summary("Backup the server")]
[Remarks("backup profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task BackupServerAsync(string profileId)
@ -34,7 +38,38 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Backup, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Backup, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
}
else
{
foreach (var output in response)
{
await ReplyAsync(output.Replace("&", "_"));
await Task.Delay(1000);
}
}
}
catch (Exception ex)
{
await ReplyAsync($"'{Context.Message}' command sent and failed with exception ({ex.Message})");
}
}
[Command("restart", RunMode = RunMode.Async)]
[Summary("Restart the server")]
[Remarks("restart profileId")]
[RequireBotPermission(ChannelPermission.ViewChannel | ChannelPermission.SendMessages)]
public async Task RestartServerAsync(string profileId)
{
try
{
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Restart, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -65,7 +100,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Shutdown, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Shutdown, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -96,7 +131,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Start, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Start, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -127,7 +162,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Stop, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Stop, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -158,7 +193,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Update, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Update, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");

View file

@ -4,7 +4,9 @@ using Discord.Commands;
using Microsoft.Extensions.Configuration;
using ServerManagerTool.DiscordBot.Delegates;
using ServerManagerTool.DiscordBot.Enums;
using ServerManagerTool.DiscordBot.Interfaces;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ServerManagerTool.DiscordBot.Modules
@ -12,12 +14,14 @@ namespace ServerManagerTool.DiscordBot.Modules
[Name("Server Query")]
public sealed class ServerQueryModule : InteractiveBase
{
private readonly IServerManagerBot _serverManagerBot;
private readonly CommandService _service;
private readonly HandleCommandDelegate _handleCommandCallback;
private readonly IConfigurationRoot _config;
public ServerQueryModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
public ServerQueryModule(IServerManagerBot serverManagerBot, CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
{
_serverManagerBot = serverManagerBot;
_service = service;
_handleCommandCallback = handleCommandCallback;
_config = config;
@ -43,7 +47,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Info, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Info, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -74,7 +78,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.List, serverId, channelId, null);
var response = _handleCommandCallback?.Invoke(CommandType.List, serverId, channelId, null, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");
@ -114,7 +118,7 @@ namespace ServerManagerTool.DiscordBot.Modules
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = _handleCommandCallback?.Invoke(CommandType.Status, serverId, channelId, profileId);
var response = _handleCommandCallback?.Invoke(CommandType.Status, serverId, channelId, profileId, _serverManagerBot.Token);
if (response is null)
{
await ReplyAsync("No servers associated with this channel.");

View file

@ -24,11 +24,8 @@ namespace ServerManagerTool.DiscordBot
Started = false;
}
private bool Started
{
get;
set;
}
public CancellationToken Token { get; private set; }
public bool Started { get; private set; }
public async Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token)
{
@ -43,6 +40,8 @@ namespace ServerManagerTool.DiscordBot
return;
}
Token = token;
if (commandPrefix.Any(c => !char.IsLetterOrDigit(c)))
{
throw new Exception("#DiscordBot_InvalidPrefixError");
@ -57,7 +56,7 @@ namespace ServerManagerTool.DiscordBot
{
{ "DiscordSettings:Token", discordToken },
{ "DiscordSettings:Prefix", commandPrefix },
{ "ServerManager:DataDirectory", dataDirectory }
{ "ServerManager:DataDirectory", dataDirectory },
};
// Begin building the configuration file
@ -107,7 +106,8 @@ namespace ServerManagerTool.DiscordBot
.AddSingleton<Random>()
.AddSingleton(config)
.AddSingleton(handleCommandCallback)
.AddSingleton(handleTranslationCallback);
.AddSingleton(handleTranslationCallback)
.AddSingleton<IServerManagerBot>(this);
// Create the service provider
using (var provider = services.BuildServiceProvider())