eplace DM/Guild preconditions with context
The new RequireContextAttribute works just like RequireDM/RequireGuild, but is more powerful as developers can specify multiple 'contexts' for their command to require using the ContextType flags.
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
public enum ContextType
|
||||||
|
{
|
||||||
|
Invalid = 0, // 00
|
||||||
|
Guild = 1, // 01
|
||||||
|
DM = 2 // 10
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
|
public class RequireContextAttribute : PreconditionAttribute
|
||||||
|
{
|
||||||
|
public ContextType Context { get; set; }
|
||||||
|
|
||||||
|
public RequireContextAttribute(ContextType context)
|
||||||
|
{
|
||||||
|
Context = context;
|
||||||
|
|
||||||
|
if (Context == ContextType.Invalid)
|
||||||
|
throw new ArgumentException("Context must be a bitfield of ContextType.Guild and ContextType.DM", "context");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
||||||
|
{
|
||||||
|
var validContext = false;
|
||||||
|
|
||||||
|
if (Context == ContextType.Invalid)
|
||||||
|
throw new InvalidOperationException("Invalid ContextType");
|
||||||
|
|
||||||
|
if (Context.HasFlag(ContextType.Guild))
|
||||||
|
validContext = validContext || context.Channel is IGuildChannel;
|
||||||
|
|
||||||
|
if (Context.HasFlag(ContextType.DM))
|
||||||
|
validContext = validContext || context.Channel is IDMChannel;
|
||||||
|
|
||||||
|
if (validContext)
|
||||||
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||||
|
else
|
||||||
|
return Task.FromResult(PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Context}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
|
||||||
public class RequireDMAttribute : PreconditionAttribute
|
|
||||||
{
|
|
||||||
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
|
||||||
{
|
|
||||||
if (context.Channel is IGuildChannel)
|
|
||||||
return Task.FromResult(PreconditionResult.FromError("Command must be used in a DM"));
|
|
||||||
|
|
||||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
|
||||||
public class RequireGuildAttribute : PreconditionAttribute
|
|
||||||
{
|
|
||||||
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
|
||||||
{
|
|
||||||
if (!(context.Channel is IGuildChannel))
|
|
||||||
return Task.FromResult(PreconditionResult.FromError("Command must be used in a guild"));
|
|
||||||
|
|
||||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Discord.Commands
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
|
||||||
public class RequireRoleAttribute : RequireGuildAttribute
|
|
||||||
{
|
|
||||||
public string Role { get; set; }
|
|
||||||
public StringComparer Comparer { get; set; }
|
|
||||||
|
|
||||||
public RequireRoleAttribute(string roleName)
|
|
||||||
{
|
|
||||||
Role = roleName;
|
|
||||||
Comparer = StringComparer.Ordinal;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RequireRoleAttribute(string roleName, StringComparer comparer)
|
|
||||||
{
|
|
||||||
Role = roleName;
|
|
||||||
Comparer = comparer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
|
||||||
{
|
|
||||||
var result = await base.CheckPermissions(context, executingCommand, moduleInstance).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!result.IsSuccess)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
var author = (context.Author as IGuildUser);
|
|
||||||
|
|
||||||
if (author != null)
|
|
||||||
{
|
|
||||||
var hasRole = author.Roles.Any(x => Comparer.Compare(x.Name, Role) == 0);
|
|
||||||
|
|
||||||
if (!hasRole)
|
|
||||||
return PreconditionResult.FromError($"User does not have the '{Role}' role.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return PreconditionResult.FromSuccess();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user