2019-04-29 17:43:09 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Discord.Commands;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot {
|
|
|
|
class MustHaveSystem : PreconditionAttribute
|
|
|
|
{
|
2019-07-21 14:43:28 +00:00
|
|
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
2019-04-29 17:43:09 +00:00
|
|
|
{
|
|
|
|
var c = context as PKCommandContext;
|
2019-07-21 14:43:28 +00:00
|
|
|
if (c == null) return Task.FromResult(PreconditionResult.FromError("Must be called on a PKCommandContext (should never happen!)")) ;
|
|
|
|
if (c.SenderSystem == null) return Task.FromResult(PreconditionResult.FromError(Errors.NoSystemError));
|
|
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
2019-04-29 17:43:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MustPassOwnMember : PreconditionAttribute
|
|
|
|
{
|
2019-07-21 14:43:28 +00:00
|
|
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
2019-04-29 17:43:09 +00:00
|
|
|
{
|
|
|
|
// OK when:
|
|
|
|
// - Sender has a system
|
|
|
|
// - Sender passes a member as a context parameter
|
|
|
|
// - Sender owns said member
|
|
|
|
|
|
|
|
var c = context as PKCommandContext;
|
2019-07-21 14:43:28 +00:00
|
|
|
if (c.SenderSystem == null) return Task.FromResult(PreconditionResult.FromError(Errors.NoSystemError));
|
|
|
|
if (c.GetContextEntity<PKMember>() == null) return Task.FromResult(PreconditionResult.FromError(Errors.MissingMemberError));
|
|
|
|
if (c.GetContextEntity<PKMember>().System != c.SenderSystem.Id) return Task.FromResult(PreconditionResult.FromError(Errors.NotOwnMemberError));
|
|
|
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
2019-04-29 17:43:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|