From c5775181b300523d46ebc161443753da6bfa88d8 Mon Sep 17 00:00:00 2001 From: Brett Hewitson Date: Sat, 4 Dec 2021 14:35:07 +1000 Subject: [PATCH] Discord Bot Changes 1. Added ServerId to Delegate 2. Change Delegate storage to use DI. --- .../Delegates/HandleCommandDelegate.cs | 2 +- src/ServerManager.Discord/DiscordBot.cs | 6 ------ .../Interfaces/IServerManagerBot.cs | 2 +- .../Modules/ServerCommandModule.cs | 20 +++++++++++++------ .../Modules/ServerQueryModule.cs | 14 +++++++++---- src/ServerManager.Discord/ServerManagerBot.cs | 10 +++++----- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/ServerManager.Discord/Delegates/HandleCommandDelegate.cs b/src/ServerManager.Discord/Delegates/HandleCommandDelegate.cs index fcbbc5d2..5d7f9a1d 100644 --- a/src/ServerManager.Discord/Delegates/HandleCommandDelegate.cs +++ b/src/ServerManager.Discord/Delegates/HandleCommandDelegate.cs @@ -3,5 +3,5 @@ using System.Collections.Generic; namespace ServerManagerTool.Discord.Delegates { - public delegate IList HandleCommandDelegate(CommandType commandType, string channelId, string profileId); + public delegate IList HandleCommandDelegate(CommandType commandType, string serverId, string channelId, string profileId); } diff --git a/src/ServerManager.Discord/DiscordBot.cs b/src/ServerManager.Discord/DiscordBot.cs index 55834e42..976cc984 100644 --- a/src/ServerManager.Discord/DiscordBot.cs +++ b/src/ServerManager.Discord/DiscordBot.cs @@ -5,11 +5,5 @@ namespace ServerManagerTool.Discord public static class DiscordBot { public const string PREFIX_DELIMITER = "!"; - - internal static HandleCommandDelegate HandleCommandCallback - { - get; - set; - } } } diff --git a/src/ServerManager.Discord/Interfaces/IServerManagerBot.cs b/src/ServerManager.Discord/Interfaces/IServerManagerBot.cs index f1dd6be4..4b6714bc 100644 --- a/src/ServerManager.Discord/Interfaces/IServerManagerBot.cs +++ b/src/ServerManager.Discord/Interfaces/IServerManagerBot.cs @@ -6,6 +6,6 @@ namespace ServerManagerTool.Discord.Interfaces { 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); } } diff --git a/src/ServerManager.Discord/Modules/ServerCommandModule.cs b/src/ServerManager.Discord/Modules/ServerCommandModule.cs index 63ac7593..49921a6a 100644 --- a/src/ServerManager.Discord/Modules/ServerCommandModule.cs +++ b/src/ServerManager.Discord/Modules/ServerCommandModule.cs @@ -1,6 +1,7 @@ using Discord.Addons.Interactive; using Discord.Commands; using Microsoft.Extensions.Configuration; +using ServerManagerTool.Discord.Delegates; using ServerManagerTool.Discord.Enums; using System; using System.Threading.Tasks; @@ -11,11 +12,13 @@ namespace ServerManagerTool.Discord.Modules public sealed class ServerCommandModule : InteractiveBase { private readonly CommandService _service; + private readonly HandleCommandDelegate _handleCommandCallback; private readonly IConfigurationRoot _config; - public ServerCommandModule(CommandService service, IConfigurationRoot config) + public ServerCommandModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config) { _service = service; + _handleCommandCallback = handleCommandCallback; _config = config; } @@ -34,9 +37,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -71,9 +75,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -108,9 +113,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -145,9 +151,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -182,9 +189,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); diff --git a/src/ServerManager.Discord/Modules/ServerQueryModule.cs b/src/ServerManager.Discord/Modules/ServerQueryModule.cs index 9b5b7590..767a3413 100644 --- a/src/ServerManager.Discord/Modules/ServerQueryModule.cs +++ b/src/ServerManager.Discord/Modules/ServerQueryModule.cs @@ -1,6 +1,7 @@ using Discord.Addons.Interactive; using Discord.Commands; using Microsoft.Extensions.Configuration; +using ServerManagerTool.Discord.Delegates; using ServerManagerTool.Discord.Enums; using System; using System.Threading.Tasks; @@ -11,11 +12,13 @@ namespace ServerManagerTool.Discord.Modules public sealed class ServerQueryModule : InteractiveBase { private readonly CommandService _service; + private readonly HandleCommandDelegate _handleCommandCallback; private readonly IConfigurationRoot _config; - public ServerQueryModule(CommandService service, IConfigurationRoot config) + public ServerQueryModule(CommandService service, HandleCommandDelegate handleCommandCallback, IConfigurationRoot config) { _service = service; + _handleCommandCallback = handleCommandCallback; _config = config; } @@ -34,9 +37,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -63,9 +67,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); @@ -100,9 +105,10 @@ namespace ServerManagerTool.Discord.Modules { try { + var serverId = Context?.Guild?.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) { await ReplyAsync("No servers associated with this channel."); diff --git a/src/ServerManager.Discord/ServerManagerBot.cs b/src/ServerManager.Discord/ServerManagerBot.cs index f9530c34..72e44731 100644 --- a/src/ServerManager.Discord/ServerManagerBot.cs +++ b/src/ServerManager.Discord/ServerManagerBot.cs @@ -21,6 +21,7 @@ namespace ServerManagerTool.Discord { internal ServerManagerBot() { + Started = false; } private bool Started @@ -29,7 +30,7 @@ namespace ServerManagerTool.Discord 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) { @@ -37,7 +38,7 @@ namespace ServerManagerTool.Discord } Started = true; - if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken)) + if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken) || handleCommandCallback is null) { return; } @@ -104,7 +105,8 @@ namespace ServerManagerTool.Discord .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton(config); + .AddSingleton(config) + .AddSingleton(handleCommandCallback); // Create the service provider using (var provider = services.BuildServiceProvider()) @@ -114,8 +116,6 @@ namespace ServerManagerTool.Discord await provider?.GetRequiredService().StartAsync(); provider?.GetRequiredService(); - DiscordBot.HandleCommandCallback = handleCommandCallback; - try { // Prevent the application from closing