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
This commit is contained in:
Kwoth
2021-07-03 01:46:32 +02:00
parent e681978f83
commit aeb6f8662c
9 changed files with 77 additions and 36 deletions

View File

@@ -9,14 +9,6 @@ namespace NadekoBot.Common.ModuleBehaviors
public interface IEarlyBehavior
{
int Priority { get; }
ModuleBehaviorType BehaviorType { get; }
Task<bool> RunBehavior(IGuild guild, IUserMessage msg);
}
public enum ModuleBehaviorType
{
Blocker,
Executor,
}
}

View File

@@ -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<CommandInfo>
public sealed class CommandTypeReader : NadekoTypeReader<CommandInfo>
{
public CommandTypeReader(DiscordSocketClient client, CommandService cmds) : base(client, cmds)
{
@@ -18,16 +17,17 @@ namespace NadekoBot.Common.TypeReaders
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
var _cmds = services.GetService<CommandService>();
var _cmdHandler = services.GetService<CommandHandler>();
var cmds = services.GetRequiredService<CommandService>();
var cmdHandler = services.GetRequiredService<CommandHandler>();
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<CommandOrCrInfo>
public sealed class CommandOrCrTypeReader : NadekoTypeReader<CommandOrCrInfo>
{
private readonly DiscordSocketClient _client;
private readonly CommandService _cmds;

View File

@@ -48,8 +48,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
private CustomReaction[] _globalReactions;
private ConcurrentDictionary<ulong, CustomReaction[]> _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)

View File

@@ -26,8 +26,7 @@ namespace NadekoBot.Modules.Games.Services
public ConcurrentDictionary<ulong, Lazy<IChatterBotSession>> 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,

View File

@@ -19,8 +19,7 @@ namespace NadekoBot.Modules.Games.Services
{
public ConcurrentDictionary<ulong, PollRunner> ActivePolls { get; } = new ConcurrentDictionary<ulong, PollRunner>();
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)
{

View File

@@ -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<BlacklistEntry> _blacklist;
public int Priority => -100;
public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Blocker;
public int Priority => int.MaxValue;
private readonly TypedKey<BlacklistEntry[]> blPubKey = new TypedKey<BlacklistEntry[]>("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);

View File

@@ -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<ulong> LinkFilteringChannels { get; }
public ConcurrentHashSet<ulong> LinkFilteringServers { get; }
public int Priority => -50;
public ModuleBehaviorType BehaviorType => ModuleBehaviorType.Blocker;
public int Priority => int.MaxValue - 1;
public ConcurrentHashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
{
@@ -136,7 +135,7 @@ namespace NadekoBot.Modules.Permissions.Services
return results.Any(x => x);
}
public async Task<bool> FilterWords(IGuild guild, IUserMessage usrMsg)
private async Task<bool> 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<bool> FilterInvites(IGuild guild, IUserMessage usrMsg)
private async Task<bool> 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<bool> FilterLinks(IGuild guild, IUserMessage usrMsg)
private async Task<bool> 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);

View File

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

View File

@@ -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<ILateExecutor>();
_lateBlockers = _services.GetServices<ILateBlocker>();
_earlyBehaviors = _services.GetServices<IEarlyBehavior>();
_earlyBehaviors = _services.GetServices<IEarlyBehavior>()
.OrderByDescending(x => x.Priority);
_transformers = _services.GetServices<IInputTransformer>();
}
// todo early behaviors should print for themselves
public async Task<bool> RunEarlyBehavioursAsync(SocketGuild guild, IUserMessage usrMsg)
{
foreach (var beh in _earlyBehaviors)