diff --git a/src/NadekoBot/Common/Medusa/Adapters/FilterAdapter.cs b/src/NadekoBot/Common/Medusa/Adapters/FilterAdapter.cs new file mode 100644 index 000000000..c5118396b --- /dev/null +++ b/src/NadekoBot/Common/Medusa/Adapters/FilterAdapter.cs @@ -0,0 +1,31 @@ +namespace Nadeko.Medusa.Adapters; + +public class FilterAdapter : PreconditionAttribute +{ + private readonly FilterAttribute _filterAttribute; + private readonly IMedusaStrings _strings; + + public FilterAdapter(FilterAttribute filterAttribute, + IMedusaStrings strings) + { + _filterAttribute = filterAttribute; + _strings = strings; + } + + public override async Task CheckPermissionsAsync( + ICommandContext context, + CommandInfo command, + IServiceProvider services) + { + var medusaContext = ContextAdapterFactory.CreateNew(context, + _strings, + services); + + var result = await _filterAttribute.CheckAsync(medusaContext); + + if (!result) + return PreconditionResult.FromError($"Precondition '{_filterAttribute.GetType().Name}' failed."); + + return PreconditionResult.FromSuccess(); + } +} \ No newline at end of file diff --git a/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs b/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs index 85b708e2d..7b6ab6c3d 100644 --- a/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs +++ b/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs @@ -1,5 +1,6 @@ using Discord.Commands.Builders; using Microsoft.Extensions.DependencyInjection; +using Nadeko.Medusa.Adapters; using NadekoBot.Common.ModuleBehaviors; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; @@ -382,6 +383,11 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, { var m = mb.WithName(snekInfo.Name); + foreach (var f in snekInfo.Filters) + { + m.AddPrecondition(new FilterAdapter(f, strings)); + } + foreach (var cmd in snekInfo.Commands) { m.AddCommand(cmd.Aliases.First(), @@ -390,7 +396,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, new(cmd), new(medusaServices), strings), - CreateCommandFactory(medusaName, cmd)); + CreateCommandFactory(medusaName, cmd, strings)); } foreach (var subInfo in snekInfo.Subsneks) @@ -399,7 +405,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, private static readonly RequireContextAttribute _reqGuild = new RequireContextAttribute(ContextType.Guild); private static readonly RequireContextAttribute _reqDm = new RequireContextAttribute(ContextType.DM); - private Action CreateCommandFactory(string medusaName, SnekCommandData cmd) + private Action CreateCommandFactory(string medusaName, SnekCommandData cmd, IMedusaStrings strings) => (cb) => { cb.AddAliases(cmd.Aliases.Skip(1).ToArray()); @@ -408,6 +414,9 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, cb.AddPrecondition(_reqGuild); else if (cmd.ContextType == CommandContextType.Dm) cb.AddPrecondition(_reqDm); + + foreach (var f in cmd.Filters) + cb.AddPrecondition(new FilterAdapter(f, strings)); cb.WithPriority(cmd.Priority); @@ -750,8 +759,8 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, var cmds = new List(); foreach (var method in methodInfos) { - var filters = method.GetCustomAttributes().ToArray(); - var prio = method.GetCustomAttribute()?.Priority ?? 0; + var filters = method.GetCustomAttributes(true).ToArray(); + var prio = method.GetCustomAttribute(true)?.Priority ?? 0; var paramInfos = method.GetParameters(); var cmdParams = new List(); @@ -828,7 +837,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, } - var cmdAttribute = method.GetCustomAttribute()!; + var cmdAttribute = method.GetCustomAttribute(true)!; var aliases = cmdAttribute.Aliases; if (aliases.Length == 0) aliases = new[] { method.Name.ToLowerInvariant() };