From aeb6f8662c87380d3d7242a6f0f7be79c297801c Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 3 Jul 2021 01:46:32 +0200 Subject: [PATCH] Early behavior fixups: - Priorities are now higher number -> higher priority - Priorities are now actually respected - Early behaviors now print custom messages when they perform an action (Filter trigger, blacklist, poll vote, etc) - small cleanup --- .../{IEarlyBlocker.cs => IEarlyBehavior.cs} | 8 ------ .../TypeReaders/BotCommandTypeReader.cs | 14 +++++----- .../Services/CustomReactionsService.cs | 14 ++++++++-- .../Games/Services/ChatterbotService.cs | 3 +-- .../Modules/Games/Services/PollService.cs | 16 +++++++++--- .../Permissions/Services/BlacklistService.cs | 23 +++++++++++++--- .../Permissions/Services/FilterService.cs | 26 ++++++++++++++----- .../Services/StreamNotificationService.cs | 4 +-- .../Services/Impl/BehaviorExecutor.cs | 5 ++-- 9 files changed, 77 insertions(+), 36 deletions(-) rename src/NadekoBot/Common/ModuleBehaviors/{IEarlyBlocker.cs => IEarlyBehavior.cs} (72%) diff --git a/src/NadekoBot/Common/ModuleBehaviors/IEarlyBlocker.cs b/src/NadekoBot/Common/ModuleBehaviors/IEarlyBehavior.cs similarity index 72% rename from src/NadekoBot/Common/ModuleBehaviors/IEarlyBlocker.cs rename to src/NadekoBot/Common/ModuleBehaviors/IEarlyBehavior.cs index 5f2b18c4b..17989045b 100644 --- a/src/NadekoBot/Common/ModuleBehaviors/IEarlyBlocker.cs +++ b/src/NadekoBot/Common/ModuleBehaviors/IEarlyBehavior.cs @@ -9,14 +9,6 @@ namespace NadekoBot.Common.ModuleBehaviors public interface IEarlyBehavior { int Priority { get; } - ModuleBehaviorType BehaviorType { get; } - Task RunBehavior(IGuild guild, IUserMessage msg); } - - public enum ModuleBehaviorType - { - Blocker, - Executor, - } } diff --git a/src/NadekoBot/Common/TypeReaders/BotCommandTypeReader.cs b/src/NadekoBot/Common/TypeReaders/BotCommandTypeReader.cs index 0e2259f78..d7dee463e 100644 --- a/src/NadekoBot/Common/TypeReaders/BotCommandTypeReader.cs +++ b/src/NadekoBot/Common/TypeReaders/BotCommandTypeReader.cs @@ -4,13 +4,12 @@ using System.Threading.Tasks; using Discord.Commands; using NadekoBot.Services; using NadekoBot.Modules.CustomReactions.Services; -using NadekoBot.Common.TypeReaders; using Discord.WebSocket; using Microsoft.Extensions.DependencyInjection; namespace NadekoBot.Common.TypeReaders { - public class CommandTypeReader : NadekoTypeReader + public sealed class CommandTypeReader : NadekoTypeReader { public CommandTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds) { @@ -18,16 +17,17 @@ namespace NadekoBot.Common.TypeReaders public override Task ReadAsync(ICommandContext context, string input, IServiceProvider services) { - var _cmds = services.GetService(); - var _cmdHandler = services.GetService(); + var cmds = services.GetRequiredService(); + var cmdHandler = services.GetRequiredService(); + input = input.ToUpperInvariant(); - var prefix = _cmdHandler.GetPrefix(context.Guild); + var prefix = cmdHandler.GetPrefix(context.Guild); if (!input.StartsWith(prefix.ToUpperInvariant(), StringComparison.InvariantCulture)) return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "No such command found.")); input = input.Substring(prefix.Length); - var cmd = _cmds.Commands.FirstOrDefault(c => + var cmd = cmds.Commands.FirstOrDefault(c => c.Aliases.Select(a => a.ToUpperInvariant()).Contains(input)); if (cmd is null) return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "No such command found.")); @@ -36,7 +36,7 @@ namespace NadekoBot.Common.TypeReaders } } - public class CommandOrCrTypeReader : NadekoTypeReader + public sealed class CommandOrCrTypeReader : NadekoTypeReader { private readonly DiscordSocketClient _client; private readonly CommandService _cmds; diff --git a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs index f0b55b1f7..863543cf3 100644 --- a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs +++ b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs @@ -48,8 +48,7 @@ namespace NadekoBot.Modules.CustomReactions.Services private CustomReaction[] _globalReactions; private ConcurrentDictionary _newGuildReactions; - public int Priority => -1; - public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Executor; + public int Priority => 0; private readonly DbService _db; private readonly DiscordSocketClient _client; @@ -395,6 +394,10 @@ namespace NadekoBot.Modules.CustomReactions.Services { if (_gperm.BlockedModules.Contains("ActualCustomReactions")) { + Log.Information("User {UserName} [{UserId}] tried to use a custom reaction but 'ActualCustomReactions' are globally disabled.", + msg.Author.ToString(), + msg.Author.Id); + return true; } @@ -454,6 +457,13 @@ namespace NadekoBot.Modules.CustomReactions.Services } } + Log.Information("s: {GuildId} c: {ChannelId} u: {UserId} | {UserName} executed expression {Expr}", + guild.Id, + msg.Channel.Id, + msg.Author.Id, + msg.Author.ToString(), + cr.Trigger); + return true; } catch (Exception ex) diff --git a/src/NadekoBot/Modules/Games/Services/ChatterbotService.cs b/src/NadekoBot/Modules/Games/Services/ChatterbotService.cs index ad5245c24..272c858a2 100644 --- a/src/NadekoBot/Modules/Games/Services/ChatterbotService.cs +++ b/src/NadekoBot/Modules/Games/Services/ChatterbotService.cs @@ -26,8 +26,7 @@ namespace NadekoBot.Modules.Games.Services public ConcurrentDictionary> ChatterBotGuilds { get; } - public int Priority => -1; - public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Executor; + public int Priority => 1; public ChatterBotService(DiscordSocketClient client, PermissionService perms, Bot bot, CommandHandler cmd, IBotStrings strings, IHttpClientFactory factory, diff --git a/src/NadekoBot/Modules/Games/Services/PollService.cs b/src/NadekoBot/Modules/Games/Services/PollService.cs index f19d1e93f..261d19163 100644 --- a/src/NadekoBot/Modules/Games/Services/PollService.cs +++ b/src/NadekoBot/Modules/Games/Services/PollService.cs @@ -19,8 +19,7 @@ namespace NadekoBot.Modules.Games.Services { public ConcurrentDictionary ActivePolls { get; } = new ConcurrentDictionary(); - public int Priority => -5; - public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Executor; + public int Priority => 5; private readonly DbService _db; private readonly IBotStrings _strs; @@ -115,7 +114,18 @@ namespace NadekoBot.Modules.Games.Services try { - return await poll.TryVote(msg).ConfigureAwait(false); + var voted = await poll.TryVote(msg).ConfigureAwait(false); + + if (voted) + { + Log.Information("User {UserName} [{UserId}] voted in a poll on {GuildName} [{GuildId}] server", + msg.Author.ToString(), + msg.Author.Id, + guild.Name, + guild.Id); + } + + return voted; } catch (Exception ex) { diff --git a/src/NadekoBot/Modules/Permissions/Services/BlacklistService.cs b/src/NadekoBot/Modules/Permissions/Services/BlacklistService.cs index b2ffc1c1d..4a3093324 100644 --- a/src/NadekoBot/Modules/Permissions/Services/BlacklistService.cs +++ b/src/NadekoBot/Modules/Permissions/Services/BlacklistService.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using NadekoBot.Common; using NadekoBot.Db; +using Serilog; namespace NadekoBot.Modules.Permissions.Services { @@ -17,9 +18,7 @@ namespace NadekoBot.Modules.Permissions.Services private readonly IPubSub _pubSub; private readonly IBotCredentials _creds; private IReadOnlyList _blacklist; - public int Priority => -100; - - public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Blocker; + public int Priority => int.MaxValue; private readonly TypedKey blPubKey = new TypedKey("blacklist.reload"); public BlacklistService(DbService db, IPubSub pubSub, IBotCredentials creds) @@ -43,13 +42,31 @@ namespace NadekoBot.Modules.Permissions.Services foreach (var bl in _blacklist) { if (guild != null && bl.Type == BlacklistType.Server && bl.ItemId == guild.Id) + { + Log.Information("Blocked input from blacklisted guild: {GuildName} [{GuildId}]", + guild.Name, + guild.Id); + return Task.FromResult(true); + } if (bl.Type == BlacklistType.Channel && bl.ItemId == usrMsg.Channel.Id) + { + Log.Information("Blocked input from blacklisted channel: {ChannelName} [{ChannelId}]", + usrMsg.Channel.Name, + usrMsg.Channel.Id); + return Task.FromResult(true); + } if (bl.Type == BlacklistType.User && bl.ItemId == usrMsg.Author.Id) + { + Log.Information("Blocked input from blacklisted user: {UserName} [{UserId}]", + usrMsg.Author.ToString(), + usrMsg.Author.Id); + return Task.FromResult(true); + } } return Task.FromResult(false); diff --git a/src/NadekoBot/Modules/Permissions/Services/FilterService.cs b/src/NadekoBot/Modules/Permissions/Services/FilterService.cs index a72f7869a..137f888a7 100644 --- a/src/NadekoBot/Modules/Permissions/Services/FilterService.cs +++ b/src/NadekoBot/Modules/Permissions/Services/FilterService.cs @@ -17,7 +17,7 @@ using Serilog; namespace NadekoBot.Modules.Permissions.Services { - public class FilterService : IEarlyBehavior + public sealed class FilterService : IEarlyBehavior { private readonly DbService _db; @@ -33,8 +33,7 @@ namespace NadekoBot.Modules.Permissions.Services public ConcurrentHashSet LinkFilteringChannels { get; } public ConcurrentHashSet LinkFilteringServers { get; } - public int Priority => -50; - public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Blocker; + public int Priority => int.MaxValue - 1; public ConcurrentHashSet FilteredWordsForChannel(ulong channelId, ulong guildId) { @@ -136,7 +135,7 @@ namespace NadekoBot.Modules.Permissions.Services return results.Any(x => x); } - public async Task FilterWords(IGuild guild, IUserMessage usrMsg) + private async Task FilterWords(IGuild guild, IUserMessage usrMsg) { if (guild is null) return false; @@ -153,6 +152,11 @@ namespace NadekoBot.Modules.Permissions.Services if (filteredChannelWords.Contains(word) || filteredServerWords.Contains(word)) { + Log.Information("User {UserName} [{UserId}] used a filtered word in {ChannelId} channel", + usrMsg.Author.ToString(), + usrMsg.Author.Id, + usrMsg.Channel.Id); + try { await usrMsg.DeleteAsync().ConfigureAwait(false); @@ -168,7 +172,7 @@ namespace NadekoBot.Modules.Permissions.Services return false; } - public async Task FilterInvites(IGuild guild, IUserMessage usrMsg) + private async Task FilterInvites(IGuild guild, IUserMessage usrMsg) { if (guild is null) return false; @@ -179,6 +183,11 @@ namespace NadekoBot.Modules.Permissions.Services || InviteFilteringServers.Contains(guild.Id)) && usrMsg.Content.IsDiscordInvite()) { + Log.Information("User {UserName} [{UserId}] sent a filtered invite to {ChannelId} channel", + usrMsg.Author.ToString(), + usrMsg.Author.Id, + usrMsg.Channel.Id); + try { await usrMsg.DeleteAsync().ConfigureAwait(false); @@ -193,7 +202,7 @@ namespace NadekoBot.Modules.Permissions.Services return false; } - public async Task FilterLinks(IGuild guild, IUserMessage usrMsg) + private async Task FilterLinks(IGuild guild, IUserMessage usrMsg) { if (guild is null) return false; @@ -204,6 +213,11 @@ namespace NadekoBot.Modules.Permissions.Services || LinkFilteringServers.Contains(guild.Id)) && usrMsg.Content.TryGetUrlPath(out _)) { + Log.Information("User {UserName} [{UserId}] sent a filtered link to {ChannelId} channel", + usrMsg.Author.ToString(), + usrMsg.Author.Id, + usrMsg.Channel.Id); + try { await usrMsg.DeleteAsync().ConfigureAwait(false); diff --git a/src/NadekoBot/Modules/Searches/Services/StreamNotificationService.cs b/src/NadekoBot/Modules/Searches/Services/StreamNotificationService.cs index caf79a044..e61445a04 100644 --- a/src/NadekoBot/Modules/Searches/Services/StreamNotificationService.cs +++ b/src/NadekoBot/Modules/Searches/Services/StreamNotificationService.cs @@ -11,20 +11,18 @@ using NadekoBot.Modules.Searches.Common.StreamNotifications; using NadekoBot.Services; using NadekoBot.Services.Database.Models; using NadekoBot.Extensions; -using Newtonsoft.Json; using StackExchange.Redis; using Discord; using Discord.WebSocket; using NadekoBot.Common.Collections; using NadekoBot.Common.Replacements; using NadekoBot.Db; -using NadekoBot.Modules.Administration; using NadekoBot.Db.Models; using Serilog; namespace NadekoBot.Modules.Searches.Services { - public class StreamNotificationService : INService + public sealed class StreamNotificationService : INService { private readonly DbService _db; private readonly IBotStrings _strings; diff --git a/src/NadekoBot/Services/Impl/BehaviorExecutor.cs b/src/NadekoBot/Services/Impl/BehaviorExecutor.cs index 6444e9724..fdeb3178a 100644 --- a/src/NadekoBot/Services/Impl/BehaviorExecutor.cs +++ b/src/NadekoBot/Services/Impl/BehaviorExecutor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -28,11 +29,11 @@ namespace NadekoBot.Services { _lateExecutors = _services.GetServices(); _lateBlockers = _services.GetServices(); - _earlyBehaviors = _services.GetServices(); + _earlyBehaviors = _services.GetServices() + .OrderByDescending(x => x.Priority); _transformers = _services.GetServices(); } - // todo early behaviors should print for themselves public async Task RunEarlyBehavioursAsync(SocketGuild guild, IUserMessage usrMsg) { foreach (var beh in _earlyBehaviors)