Global Settings - Discord Bot section - Added a whitelist to allow bots to send commands to the server manager.

This commit is contained in:
Brett Hewitson 2021-12-16 12:56:26 +10:00
parent 213a90e072
commit 461221294a
27 changed files with 413 additions and 15 deletions

View file

@ -1,7 +1,9 @@
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using ServerManagerTool.DiscordBot.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace ServerManagerTool.DiscordBot.Services
@ -12,14 +14,15 @@ namespace ServerManagerTool.DiscordBot.Services
private readonly CommandService _commands;
private readonly IConfigurationRoot _config;
private readonly IServiceProvider _provider;
private readonly DiscordBotWhitelistConfig _botWhitelist;
public CommandHandlerService(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider)
public CommandHandlerService(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider, DiscordBotWhitelistConfig botWhitelist)
{
_discord = discord;
_commands = commands;
_config = config;
_provider = provider;
_botWhitelist = botWhitelist ?? new DiscordBotWhitelistConfig();
_discord.MessageReceived += OnMessageReceivedAsync;
}
@ -38,8 +41,8 @@ namespace ServerManagerTool.DiscordBot.Services
return;
}
//Tell bot to ignore itself.
if (msg.Author.IsBot)
// Tell bot to ignore itself, unless on the whitelist
if (msg.Author.IsBot && !_botWhitelist.DiscordBotWhitelists.Any(b => b.BotId.Equals(msg.Author.Id)))
{
return;
}