Discord Bot Changes

1. Added ServerId to Delegate
2. Change Delegate storage to use DI.
This commit is contained in:
Brett Hewitson 2021-12-04 14:35:07 +10:00
parent c4bf4906ea
commit c5775181b3
6 changed files with 31 additions and 23 deletions

View file

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

View file

@ -5,11 +5,5 @@ namespace ServerManagerTool.Discord
public static class DiscordBot public static class DiscordBot
{ {
public const string PREFIX_DELIMITER = "!"; public const string PREFIX_DELIMITER = "!";
internal static HandleCommandDelegate HandleCommandCallback
{
get;
set;
}
} }
} }

View file

@ -6,6 +6,6 @@ namespace ServerManagerTool.Discord.Interfaces
{ {
public interface IServerManagerBot public interface IServerManagerBot
{ {
Task StartAsync(string commandPrefix, string discordToken, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token); Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token);
} }
} }

View file

@ -1,6 +1,7 @@
using Discord.Addons.Interactive; using Discord.Addons.Interactive;
using Discord.Commands; using Discord.Commands;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.Discord.Enums; using ServerManagerTool.Discord.Enums;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,11 +12,13 @@ namespace ServerManagerTool.Discord.Modules
public sealed class ServerCommandModule : InteractiveBase public sealed class ServerCommandModule : InteractiveBase
{ {
private readonly CommandService _service; private readonly CommandService _service;
private readonly HandleCommandDelegate _handleCommandCallback;
private readonly IConfigurationRoot _config; private readonly IConfigurationRoot _config;
public ServerCommandModule(CommandService service, IConfigurationRoot config) public ServerCommandModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
{ {
_service = service; _service = service;
_handleCommandCallback = handleCommandCallback;
_config = config; _config = config;
} }
@ -34,9 +37,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.BackupServer, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.BackupServer, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -71,9 +75,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.ShutdownServer, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.ShutdownServer, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -108,9 +113,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.StartServer, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.StartServer, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -145,9 +151,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.StopServer, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.StopServer, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -182,9 +189,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.UpdateServer, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.UpdateServer, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");

View file

@ -1,6 +1,7 @@
using Discord.Addons.Interactive; using Discord.Addons.Interactive;
using Discord.Commands; using Discord.Commands;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using ServerManagerTool.Discord.Delegates;
using ServerManagerTool.Discord.Enums; using ServerManagerTool.Discord.Enums;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,11 +12,13 @@ namespace ServerManagerTool.Discord.Modules
public sealed class ServerQueryModule : InteractiveBase public sealed class ServerQueryModule : InteractiveBase
{ {
private readonly CommandService _service; private readonly CommandService _service;
private readonly HandleCommandDelegate _handleCommandCallback;
private readonly IConfigurationRoot _config; private readonly IConfigurationRoot _config;
public ServerQueryModule(CommandService service, IConfigurationRoot config) public ServerQueryModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config)
{ {
_service = service; _service = service;
_handleCommandCallback = handleCommandCallback;
_config = config; _config = config;
} }
@ -34,9 +37,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.ServerInfo, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.ServerInfo, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -63,9 +67,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.ServerList, channelId, null); var response = _handleCommandCallback?.Invoke(CommandType.ServerList, serverId, channelId, null);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");
@ -100,9 +105,10 @@ namespace ServerManagerTool.Discord.Modules
{ {
try try
{ {
var serverId = Context?.Guild?.Id.ToString() ?? string.Empty;
var channelId = Context?.Channel?.Id.ToString() ?? string.Empty; var channelId = Context?.Channel?.Id.ToString() ?? string.Empty;
var response = DiscordBot.HandleCommandCallback?.Invoke(CommandType.ServerStatus, channelId, profileId); var response = _handleCommandCallback?.Invoke(CommandType.ServerStatus, serverId, channelId, profileId);
if (response is null || response.Count == 0) if (response is null || response.Count == 0)
{ {
await ReplyAsync("No servers associated with this channel."); await ReplyAsync("No servers associated with this channel.");

View file

@ -21,6 +21,7 @@ namespace ServerManagerTool.Discord
{ {
internal ServerManagerBot() internal ServerManagerBot()
{ {
Started = false;
} }
private bool Started private bool Started
@ -29,7 +30,7 @@ namespace ServerManagerTool.Discord
set; set;
} }
public async Task StartAsync(string commandPrefix, string discordToken, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token) public async Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, CancellationToken token)
{ {
if (Started) if (Started)
{ {
@ -37,7 +38,7 @@ namespace ServerManagerTool.Discord
} }
Started = true; Started = true;
if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken)) if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken) || handleCommandCallback is null)
{ {
return; return;
} }
@ -104,7 +105,8 @@ namespace ServerManagerTool.Discord
.AddSingleton<StartupService>() .AddSingleton<StartupService>()
.AddSingleton<ShutdownService>() .AddSingleton<ShutdownService>()
.AddSingleton<Random>() .AddSingleton<Random>()
.AddSingleton(config); .AddSingleton(config)
.AddSingleton(handleCommandCallback);
// Create the service provider // Create the service provider
using (var provider = services.BuildServiceProvider()) using (var provider = services.BuildServiceProvider())
@ -114,8 +116,6 @@ namespace ServerManagerTool.Discord
await provider?.GetRequiredService<StartupService>().StartAsync(); await provider?.GetRequiredService<StartupService>().StartAsync();
provider?.GetRequiredService<CommandHandlerService>(); provider?.GetRequiredService<CommandHandlerService>();
DiscordBot.HandleCommandCallback = handleCommandCallback;
try try
{ {
// Prevent the application from closing // Prevent the application from closing