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
{
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 const string PREFIX_DELIMITER = "!";
internal static HandleCommandDelegate HandleCommandCallback
{
get;
set;
}
}
}

View file

@ -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);
}
}

View file

@ -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.");

View file

@ -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.");

View file

@ -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<StartupService>()
.AddSingleton<ShutdownService>()
.AddSingleton<Random>()
.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<StartupService>().StartAsync();
provider?.GetRequiredService<CommandHandlerService>();
DiscordBot.HandleCommandCallback = handleCommandCallback;
try
{
// Prevent the application from closing