diff --git a/src/Nadeko.Medusa/Attributes/user_permAttribute.cs b/src/Nadeko.Medusa/Attributes/user_permAttribute.cs new file mode 100644 index 000000000..576517c37 --- /dev/null +++ b/src/Nadeko.Medusa/Attributes/user_permAttribute.cs @@ -0,0 +1,24 @@ +using Discord; + +namespace Nadeko.Snake; + +[AttributeUsage(AttributeTargets.Method)] +public sealed class user_permAttribute : Attribute +{ + public GuildPermission? GuildPerm { get; } + public ChannelPermission? ChannelPerm { get; } + + + public user_permAttribute(GuildPermission perm) + { + GuildPerm = perm; + ChannelPerm = null; + } + + public user_permAttribute(ChannelPermission perm) + { + ChannelPerm = perm; + GuildPerm = null; + } +} + diff --git a/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs b/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs index 7b6ab6c3d..c7d2c4c23 100644 --- a/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs +++ b/src/NadekoBot/Common/Medusa/MedusaLoaderService.cs @@ -418,6 +418,14 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, foreach (var f in cmd.Filters) cb.AddPrecondition(new FilterAdapter(f, strings)); + foreach (var up in cmd.UserPerms) + { + if (up.GuildPerm is { } gp) + cb.AddPrecondition(new UserPermAttribute(gp)); + else if (up.ChannelPerm is { } cp) + cb.AddPrecondition(new UserPermAttribute(cp)); + } + cb.WithPriority(cmd.Priority); // using summary to save method name @@ -760,6 +768,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, foreach (var method in methodInfos) { var filters = method.GetCustomAttributes(true).ToArray(); + var userPerms = method.GetCustomAttributes(false).ToArray(); var prio = method.GetCustomAttribute(true)?.Priority ?? 0; var paramInfos = method.GetParameters(); @@ -847,6 +856,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, method, instance, filters, + userPerms, cmdContext, diParams, cmdParams, diff --git a/src/NadekoBot/Common/Medusa/Models/SnekCommandData.cs b/src/NadekoBot/Common/Medusa/Models/SnekCommandData.cs index a4cafe588..2dc4afae4 100644 --- a/src/NadekoBot/Common/Medusa/Models/SnekCommandData.cs +++ b/src/NadekoBot/Common/Medusa/Models/SnekCommandData.cs @@ -11,6 +11,7 @@ public sealed class SnekCommandData MethodInfo methodInfo, Snek module, FilterAttribute[] filters, + user_permAttribute[] userPerms, CommandContextType contextType, IReadOnlyList injectedParams, IReadOnlyList parameters, @@ -21,6 +22,7 @@ public sealed class SnekCommandData MethodInfo = methodInfo; Module = module; Filters = filters; + UserPerms = userPerms; ContextType = contextType; InjectedParams = injectedParams; Parameters = parameters; @@ -28,6 +30,8 @@ public sealed class SnekCommandData OptionalStrings = strings; } + public user_permAttribute[] UserPerms { get; set; } + public CommandStrings OptionalStrings { get; set; } public IReadOnlyCollection Aliases { get; }