mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Added a checkbox to allow all bots.
This commit is contained in:
parent
13713b7092
commit
06551d3c8e
22 changed files with 137 additions and 50 deletions
|
|
@ -10,6 +10,6 @@ namespace ServerManagerTool.DiscordBot.Interfaces
|
|||
{
|
||||
CancellationToken Token { get; }
|
||||
|
||||
Task StartAsync(LogLevel logLevel, string discordToken, string commandPrefix, string dataDirectory, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
|
||||
Task StartAsync(LogLevel logLevel, string discordToken, string commandPrefix, string dataDirectory, bool allowAllBots, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
namespace ServerManagerTool.DiscordBot.Models
|
||||
{
|
||||
public class DiscordBotWhitelistConfig
|
||||
public class DiscordBotConfig
|
||||
{
|
||||
public bool AllowAllBots { get; set; } = false;
|
||||
|
||||
public List<DiscordBotWhitelist> DiscordBotWhitelists { get; set; } = new List<DiscordBotWhitelist>();
|
||||
}
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ namespace ServerManagerTool.DiscordBot
|
|||
public CancellationToken Token { get; private set; }
|
||||
public bool Started { get; private set; }
|
||||
|
||||
public async Task StartAsync(LogLevel logLevel, string discordToken, string commandPrefix, string dataDirectory, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token)
|
||||
public async Task StartAsync(LogLevel logLevel, string discordToken, string commandPrefix, string dataDirectory, bool allowAllBots, IEnumerable<string> botWhitelist, HandleCommandDelegate handleCommandCallback, HandleTranslationDelegate handleTranslationCallback, CancellationToken token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(commandPrefix) || string.IsNullOrWhiteSpace(discordToken) || handleTranslationCallback is null || handleCommandCallback is null)
|
||||
{
|
||||
|
|
@ -74,9 +74,10 @@ namespace ServerManagerTool.DiscordBot
|
|||
LogLevel = LogLevelHelper.GetLogSeverity(logLevel),
|
||||
};
|
||||
|
||||
var discordBotWhitelistConfig = new DiscordBotWhitelistConfig
|
||||
var discordBotConfig = new DiscordBotConfig
|
||||
{
|
||||
DiscordBotWhitelists = new List<DiscordBotWhitelist> ( botWhitelist.Select(i => new DiscordBotWhitelist { BotId = i }) )
|
||||
AllowAllBots = allowAllBots,
|
||||
DiscordBotWhitelists = new List<DiscordBotWhitelist> ( botWhitelist.Select(i => new DiscordBotWhitelist { BotId = i }) ),
|
||||
};
|
||||
|
||||
// Build the service provider
|
||||
|
|
@ -93,7 +94,7 @@ namespace ServerManagerTool.DiscordBot
|
|||
.AddSingleton<ShutdownService>()
|
||||
.AddSingleton<Random>()
|
||||
.AddSingleton(config)
|
||||
.AddSingleton(discordBotWhitelistConfig)
|
||||
.AddSingleton(discordBotConfig)
|
||||
.AddSingleton(handleCommandCallback)
|
||||
.AddSingleton(handleTranslationCallback)
|
||||
.AddSingleton<IServerManagerBot>(this);
|
||||
|
|
|
|||
|
|
@ -17,53 +17,65 @@ namespace ServerManagerTool.DiscordBot.Services
|
|||
private readonly LoggingService _logger;
|
||||
private readonly IConfigurationRoot _config;
|
||||
private readonly IServiceProvider _provider;
|
||||
private readonly DiscordBotWhitelistConfig _botWhitelist;
|
||||
private readonly DiscordBotConfig _botConfig;
|
||||
|
||||
public CommandHandlerService(DiscordSocketClient discord, CommandService commands, LoggingService logger, IConfigurationRoot config, IServiceProvider provider, DiscordBotWhitelistConfig botWhitelist)
|
||||
public CommandHandlerService(DiscordSocketClient discord, CommandService commands, LoggingService logger, IConfigurationRoot config, IServiceProvider provider, DiscordBotConfig botConfig)
|
||||
{
|
||||
_discord = discord;
|
||||
_commands = commands;
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_provider = provider;
|
||||
_botWhitelist = botWhitelist ?? new DiscordBotWhitelistConfig();
|
||||
_botConfig = botConfig ?? new DiscordBotConfig();
|
||||
_discord.MessageReceived += OnMessageReceivedAsync;
|
||||
}
|
||||
|
||||
private async Task OnMessageReceivedAsync(SocketMessage s)
|
||||
{
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
{
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Intercepted the following message from {s.Author.Username} ({s.Author.Id}) - {s.Content}"));
|
||||
}
|
||||
|
||||
// Ensure the message is from a user/bot
|
||||
// Ensure the message is a valid user socket message
|
||||
if (!(s is SocketUserMessage msg))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore self when checking commands
|
||||
if (msg.Author == _discord.CurrentUser)
|
||||
// Ignore self
|
||||
if (msg.Author.Id == _discord.CurrentUser.Id)
|
||||
{
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Message has come from this bot, message will be ignored."));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// check if the author is a bot
|
||||
if (msg.Author.IsBot)
|
||||
{
|
||||
// check if bot is on the whitelist
|
||||
if (!_botWhitelist.DiscordBotWhitelists.Any(b => b.BotId.Equals(msg.Author.Id.ToString())))
|
||||
if (_botConfig.AllowAllBots)
|
||||
{
|
||||
// Tell bot to ignore
|
||||
return;
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Message has come from another bot, allow all bots enabled."));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Message has come from another bot, checking if bot is in the whitelist."));
|
||||
|
||||
if (!_botConfig.DiscordBotWhitelists.Any(b => b.BotId.Equals(msg.Author.Id.ToString())))
|
||||
{
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Message has come from another bot, bot is not in the whitelist, message will be ignored."));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the message has a valid command prefix
|
||||
var argPos = 0;
|
||||
if (msg.HasStringPrefix(_config["DiscordSettings:Prefix"], ref argPos) || msg.HasMentionPrefix(_discord.CurrentUser, ref argPos))
|
||||
{
|
||||
if (LogLevel.Debug.ToString().Equals(_config["DiscordSettings:LogLevel"]))
|
||||
await _logger?.OnLogAsync(new LogMessage(LogSeverity.Debug, MessageSource.System.ToString(), $"Message prefix matched, message will be processed."));
|
||||
|
||||
// Create the command context
|
||||
var context = new SocketCommandContext(_discord, msg);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue