2021-09-27 00:42:08 +00:00
|
|
|
using Autofac;
|
|
|
|
|
2021-11-11 04:46:16 +00:00
|
|
|
using Myriad.Extensions;
|
2021-08-27 15:03:47 +00:00
|
|
|
using Myriad.Types;
|
2020-12-22 15:55:13 +00:00
|
|
|
|
2020-07-01 16:18:38 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public static class ContextChecksExt
|
2020-07-01 16:18:38 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckGuildContext(this Context ctx)
|
2020-07-01 16:18:38 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx.Channel.GuildId != null) return ctx;
|
|
|
|
throw new PKError("This command can not be run in a DM.");
|
|
|
|
}
|
2021-11-03 06:01:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckDMContext(this Context ctx)
|
|
|
|
{
|
|
|
|
if (ctx.Channel.GuildId == null) return ctx;
|
|
|
|
throw new PKError("This command must be run in a DM.");
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-12-07 06:32:29 +00:00
|
|
|
public static Context CheckSystemPrivacy(this Context ctx, SystemId target, PrivacyLevel level)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2021-12-07 06:32:29 +00:00
|
|
|
if (level.CanAccess(ctx.DirectLookupContextFor(target))) return ctx;
|
|
|
|
throw Errors.LookupNotAllowed;
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-12-05 22:21:31 +00:00
|
|
|
public static Context CheckOwnSystem(this Context ctx, PKSystem system)
|
|
|
|
{
|
|
|
|
if (system.Id != ctx.System?.Id)
|
|
|
|
throw Errors.NotOwnSystemError;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckOwnMember(this Context ctx, PKMember member)
|
|
|
|
{
|
|
|
|
if (member.System != ctx.System?.Id)
|
|
|
|
throw Errors.NotOwnMemberError;
|
|
|
|
return ctx;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckOwnGroup(this Context ctx, PKGroup group)
|
|
|
|
{
|
|
|
|
if (group.System != ctx.System?.Id)
|
|
|
|
throw Errors.NotOwnGroupError;
|
|
|
|
return ctx;
|
|
|
|
}
|
2020-07-01 16:18:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckSystem(this Context ctx)
|
|
|
|
{
|
|
|
|
if (ctx.System == null)
|
|
|
|
throw Errors.NoSystemError;
|
|
|
|
return ctx;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-01-26 11:20:26 +00:00
|
|
|
public static Context CheckSystem(this Context ctx, PKSystem system)
|
|
|
|
{
|
|
|
|
if (system == null)
|
|
|
|
throw Errors.NoSystemError;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static Context CheckNoSystem(this Context ctx)
|
|
|
|
{
|
|
|
|
if (ctx.System != null)
|
|
|
|
throw Errors.ExistingSystemError;
|
|
|
|
return ctx;
|
|
|
|
}
|
2021-09-27 00:42:08 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<Context> CheckAuthorPermission(this Context ctx, PermissionSet neededPerms,
|
|
|
|
string permissionName)
|
|
|
|
{
|
|
|
|
if ((await ctx.UserPermissions & neededPerms) != neededPerms)
|
|
|
|
throw new PKError(
|
|
|
|
$"You must have the \"{permissionName}\" permission in this server to use this command.");
|
|
|
|
return ctx;
|
|
|
|
}
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<bool> CheckPermissionsInGuildChannel(this Context ctx, Channel channel,
|
|
|
|
PermissionSet neededPerms)
|
|
|
|
{
|
|
|
|
var guild = await ctx.Cache.GetGuild(channel.GuildId.Value);
|
|
|
|
if (guild == null)
|
|
|
|
return false;
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var guildMember = ctx.Member;
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx.Guild?.Id != channel.GuildId)
|
|
|
|
{
|
|
|
|
guildMember = await ctx.Rest.GetGuildMember(channel.GuildId.Value, ctx.Author.Id);
|
|
|
|
if (guildMember == null)
|
2021-11-11 04:46:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var userPermissions = PermissionExtensions.PermissionsFor(guild, channel, ctx.Author.Id, guildMember);
|
|
|
|
if ((userPermissions & neededPerms) == 0)
|
|
|
|
return false;
|
2021-09-27 00:42:08 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-09-27 00:42:08 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool CheckBotAdmin(this Context ctx)
|
|
|
|
{
|
|
|
|
var botConfig = ctx.Services.Resolve<BotConfig>();
|
|
|
|
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;
|
2020-07-01 16:18:38 +00:00
|
|
|
}
|
|
|
|
}
|