mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Global Settings - Discord Bot section - Added a whitelist to allow bots to send commands to the server manager.
This commit is contained in:
parent
213a90e072
commit
461221294a
27 changed files with 413 additions and 15 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using ServerManagerTool.DiscordBot.Delegates;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -8,6 +9,6 @@ namespace ServerManagerTool.DiscordBot.Interfaces
|
|||
{
|
||||
CancellationToken Token { get; }
|
||||
|
||||
Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
|
||||
Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
src/ServerManager.Discord/Models/DiscordBotWhitelist.cs
Normal file
7
src/ServerManager.Discord/Models/DiscordBotWhitelist.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace ServerManagerTool.DiscordBot.Models
|
||||
{
|
||||
public class DiscordBotWhitelist
|
||||
{
|
||||
public string BotId { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace ServerManagerTool.DiscordBot.Models
|
||||
{
|
||||
public class DiscordBotWhitelistConfig
|
||||
{
|
||||
public List<DiscordBotWhitelist> DiscordBotWhitelists { get; set; } = new List<DiscordBotWhitelist>();
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ using Microsoft.Extensions.Configuration;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ServerManagerTool.DiscordBot.Delegates;
|
||||
using ServerManagerTool.DiscordBot.Interfaces;
|
||||
using ServerManagerTool.DiscordBot.Models;
|
||||
using ServerManagerTool.DiscordBot.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -27,7 +28,7 @@ namespace ServerManagerTool.DiscordBot
|
|||
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)
|
||||
public async Task StartAsync(string discordToken, string commandPrefix, string dataDirectory, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token)
|
||||
{
|
||||
if (Started)
|
||||
{
|
||||
|
|
@ -91,6 +92,11 @@ namespace ServerManagerTool.DiscordBot
|
|||
#endif
|
||||
};
|
||||
|
||||
var discordBotWhitelistConfig = new DiscordBotWhitelistConfig
|
||||
{
|
||||
DiscordBotWhitelists = new List<DiscordBotWhitelist> ( botWhitelist.Select(i => new DiscordBotWhitelist { BotId = i }) )
|
||||
};
|
||||
|
||||
// Build the service provider
|
||||
var services = new ServiceCollection()
|
||||
// Add the discord client to the service provider
|
||||
|
|
@ -105,6 +111,7 @@ namespace ServerManagerTool.DiscordBot
|
|||
.AddSingleton<ShutdownService>()
|
||||
.AddSingleton<Random>()
|
||||
.AddSingleton(config)
|
||||
.AddSingleton(discordBotWhitelistConfig)
|
||||
.AddSingleton(handleCommandCallback)
|
||||
.AddSingleton(handleTranslationCallback)
|
||||
.AddSingleton<IServerManagerBot>(this);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue