diff --git a/PluralKit.Bot/CommandSystem/ContextChecksExt.cs b/PluralKit.Bot/CommandSystem/ContextChecksExt.cs index 5a41908a..25355837 100644 --- a/PluralKit.Bot/CommandSystem/ContextChecksExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextChecksExt.cs @@ -1,3 +1,7 @@ +using System.Linq; + +using Autofac; + using Myriad.Types; using PluralKit.Core; @@ -52,5 +56,19 @@ namespace PluralKit.Bot throw new PKError($"You must have the \"{permissionName}\" permission in this server to use this command."); return ctx; } + + public static bool CheckBotAdmin(this Context ctx) + { + var botConfig = ctx.Services.Resolve(); + return botConfig.AdminRole != null && ctx.Member != null && ctx.Member.Roles.Contains(botConfig.AdminRole.Value); + } + + public static Context AssertBotAdmin(this Context ctx) + { + if (!ctx.CheckBotAdmin()) + throw new PKError("This command is only usable by bot admins."); + + return ctx; + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Admin.cs b/PluralKit.Bot/Commands/Admin.cs index 9a5ad56e..d47b0fe2 100644 --- a/PluralKit.Bot/Commands/Admin.cs +++ b/PluralKit.Bot/Commands/Admin.cs @@ -21,7 +21,7 @@ namespace PluralKit.Bot public async Task UpdateSystemId(Context ctx) { - AssertBotAdmin(ctx); + ctx.AssertBotAdmin(); var target = await ctx.MatchSystem(); if (target == null) @@ -44,7 +44,7 @@ namespace PluralKit.Bot public async Task UpdateMemberId(Context ctx) { - AssertBotAdmin(ctx); + ctx.AssertBotAdmin(); var target = await ctx.MatchMember(); if (target == null) @@ -67,7 +67,7 @@ namespace PluralKit.Bot public async Task UpdateGroupId(Context ctx) { - AssertBotAdmin(ctx); + ctx.AssertBotAdmin(); var target = await ctx.MatchGroup(); if (target == null) @@ -90,7 +90,7 @@ namespace PluralKit.Bot public async Task SystemMemberLimit(Context ctx) { - AssertBotAdmin(ctx); + ctx.AssertBotAdmin(); var target = await ctx.MatchSystem(); if (target == null) @@ -120,7 +120,7 @@ namespace PluralKit.Bot public async Task SystemGroupLimit(Context ctx) { - AssertBotAdmin(ctx); + ctx.AssertBotAdmin(); var target = await ctx.MatchSystem(); if (target == null) @@ -147,16 +147,5 @@ namespace PluralKit.Bot }); await ctx.Reply($"{Emojis.Success} Group limit updated."); } - - private void AssertBotAdmin(Context ctx) - { - if (!IsBotAdmin(ctx)) - throw new PKError("This command is only usable by bot admins."); - } - - private bool IsBotAdmin(Context ctx) - { - return _botConfig.AdminRole != null && ctx.Member.Roles.Contains(_botConfig.AdminRole.Value); - } } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Checks.cs b/PluralKit.Bot/Commands/Checks.cs index 43dce925..084f0df4 100644 --- a/PluralKit.Bot/Commands/Checks.cs +++ b/PluralKit.Bot/Commands/Checks.cs @@ -263,10 +263,12 @@ namespace PluralKit.Bot if ((_botConfig.Prefixes ?? BotConfig.DefaultPrefixes).Any(p => msg.Content.StartsWith(p))) await ctx.Reply("This message starts with the bot's prefix, and was parsed as a command."); + if (msg.Author.Bot) + throw new PKError("You cannot check messages sent by a bot."); if (msg.WebhookId != null) - await ctx.Reply("You cannot check messages sent by a webhook."); - if (msg.Author.Id != ctx.Author.Id) - await ctx.Reply("You can only check your own messages."); + throw new PKError("You cannot check messages sent by a webhook."); + if (msg.Author.Id != ctx.Author.Id && !ctx.CheckBotAdmin()) + throw new PKError("You can only check your own messages."); // get the channel info var channel = _cache.GetChannel(channelId.Value);