diff --git a/PluralKit.Bot/CommandSystem/Context.cs b/PluralKit.Bot/CommandSystem/Context.cs index d79e5693..ef68229d 100644 --- a/PluralKit.Bot/CommandSystem/Context.cs +++ b/PluralKit.Bot/CommandSystem/Context.cs @@ -7,7 +7,6 @@ using App.Metrics; using Autofac; - using DSharpPlus; using DSharpPlus.Entities; using DSharpPlus.Exceptions; @@ -241,43 +240,6 @@ namespace PluralKit.Bot return $"Member not found. Note that a member ID is 5 characters long."; } - public Context CheckSystem() - { - if (_senderSystem == null) - throw Errors.NoSystemError; - return this; - } - - public Context CheckNoSystem() - { - if (_senderSystem != null) - throw Errors.ExistingSystemError; - return this; - } - - public Context CheckOwnMember(PKMember member) - { - if (member.System != _senderSystem.Id) - throw Errors.NotOwnMemberError; - return this; - } - - public Context CheckAuthorPermission(Permissions neededPerms, string permissionName) - { - // TODO: can we always assume Author is a DiscordMember? I would think so, given they always come from a - // message received event... - var hasPerms = Channel.PermissionsInSync(Author); - if ((hasPerms & neededPerms) != neededPerms) - throw new PKError($"You must have the \"{permissionName}\" permission in this server to use this command."); - return this; - } - - public Context CheckGuildContext() - { - if (Channel.Guild != null) return this; - throw new PKError("This command can not be run in a DM."); - } - public LookupContext LookupContextFor(PKSystem target) => System?.Id == target.Id ? LookupContext.ByOwner : LookupContext.ByNonOwner; @@ -287,12 +249,6 @@ namespace PluralKit.Bot public LookupContext LookupContextFor(PKMember target) => System?.Id == target.System ? LookupContext.ByOwner : LookupContext.ByNonOwner; - public Context CheckSystemPrivacy(PKSystem target, PrivacyLevel level) - { - if (level.CanAccess(LookupContextFor(target))) return this; - throw new PKError("You do not have permission to access this information."); - } - public async Task MatchChannel() { if (!MentionUtils.TryParseChannel(PeekArgument(), out var channel)) diff --git a/PluralKit.Bot/CommandSystem/ContextChecks.cs b/PluralKit.Bot/CommandSystem/ContextChecks.cs new file mode 100644 index 00000000..54bb45a6 --- /dev/null +++ b/PluralKit.Bot/CommandSystem/ContextChecks.cs @@ -0,0 +1,54 @@ +using System.Threading.Channels; + +using DSharpPlus; + +using PluralKit.Core; + +namespace PluralKit.Bot +{ + public static class ContextChecks + { + public static Context CheckGuildContext(this Context ctx) + { + if (ctx.Channel.Guild != null) return ctx; + throw new PKError("This command can not be run in a DM."); + } + + public static Context CheckSystemPrivacy(this Context ctx, PKSystem target, PrivacyLevel level) + { + if (level.CanAccess(ctx.LookupContextFor(target))) return ctx; + throw new PKError("You do not have permission to access this information."); + } + + public static Context CheckOwnMember(this Context ctx, PKMember member) + { + if (member.System != ctx.System?.Id) + throw Errors.NotOwnMemberError; + return ctx; + } + + public static Context CheckSystem(this Context ctx) + { + if (ctx.System == null) + throw Errors.NoSystemError; + return ctx; + } + + public static Context CheckNoSystem(this Context ctx) + { + if (ctx.System != null) + throw Errors.ExistingSystemError; + return ctx; + } + + public static Context CheckAuthorPermission(this Context ctx, Permissions neededPerms, string permissionName) + { + // TODO: can we always assume Author is a DiscordMember? I would think so, given they always come from a + // message received event... + var hasPerms = ctx.Channel.PermissionsInSync(ctx.Author); + if ((hasPerms & neededPerms) != neededPerms) + throw new PKError($"You must have the \"{permissionName}\" permission in this server to use this command."); + return ctx; + } + } +} \ No newline at end of file