mirror of
https://github.com/oliverbooth/VpSharp
synced 2024-11-14 16:25:41 +00:00
Add pre-execution checks
This commit is contained in:
parent
a30f15b452
commit
d82a4d2e5e
@ -0,0 +1,13 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the base class for all execution check attributes.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class PreExecutionCheckAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Performs the execution check.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns><see langword="true" /> if the execution check has passed; otherwise, <see langword="false" />.</returns>
|
||||||
|
protected internal abstract Task<bool> PerformAsync(CommandContext context);
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies that this command can only be run by bots.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RequireAvatarNameAttribute : PreExecutionCheckAttribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RequireAvatarNameAttribute" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="names">An array of allowed user names.</param>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="names" /> is <see langword="null" />.</exception>
|
||||||
|
public RequireAvatarNameAttribute(params string[] names)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(names);
|
||||||
|
Names = names[..];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a read-only view of the user names allowed to run this command.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>A <see cref="IReadOnlyList{T}" /> of <see cref="string" /> representing the allowed user names.</value>
|
||||||
|
public IReadOnlyList<string> Names { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override Task<bool> PerformAsync(CommandContext context)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(context);
|
||||||
|
return Task.FromResult(Names.Contains(context.Avatar.Name));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies that this command can only be run by bots.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RequireBotAttribute : PreExecutionCheckAttribute
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override Task<bool> PerformAsync(CommandContext context)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(context);
|
||||||
|
return Task.FromResult(context.Avatar.IsBot);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies that this command cannot be run by bots.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RequireHumanAttribute : PreExecutionCheckAttribute
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override Task<bool> PerformAsync(CommandContext context)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(context);
|
||||||
|
return Task.FromResult(!context.Avatar.IsBot);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies that this command can only be run by bots.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RequireUserIdAttribute : PreExecutionCheckAttribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RequireUserIdAttribute" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userIds">An array of allowed user IDs.</param>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="userIds" /> is <see langword="null" />.</exception>
|
||||||
|
public RequireUserIdAttribute(params int[] userIds)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(userIds);
|
||||||
|
UserIds = userIds[..];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a read-only view of the user IDs allowed to run this command.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>A <see cref="IReadOnlyList{T}" /> of <see cref="string" /> representing the allowed user IDs.</value>
|
||||||
|
public IReadOnlyList<int> UserIds { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override Task<bool> PerformAsync(CommandContext context)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(context);
|
||||||
|
return Task.FromResult(UserIds.Contains(context.Avatar.User.Id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies that this command can only be run by bots.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="RequireUserNameAttribute" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="names">An array of allowed user names.</param>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="names" /> is <see langword="null" />.</exception>
|
||||||
|
public RequireUserNameAttribute(params string[] names)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(names);
|
||||||
|
Names = names[..];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a read-only view of the user names allowed to run this command.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>A <see cref="IReadOnlyList{T}" /> of <see cref="string" /> representing the allowed user names.</value>
|
||||||
|
public IReadOnlyList<string> Names { get; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected internal override Task<bool> PerformAsync(CommandContext context)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(context);
|
||||||
|
return Task.FromResult(Names.Contains(context.Avatar.User.Name));
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ using System.Reflection;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using VpSharp.ClientExtensions;
|
using VpSharp.ClientExtensions;
|
||||||
using VpSharp.Commands.Attributes;
|
using VpSharp.Commands.Attributes;
|
||||||
|
using VpSharp.Commands.Attributes.ExecutionChecks;
|
||||||
using VpSharp.Entities;
|
using VpSharp.Entities;
|
||||||
using VpSharp.EventData;
|
using VpSharp.EventData;
|
||||||
|
|
||||||
@ -218,8 +219,16 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
|
|||||||
}
|
}
|
||||||
|
|
||||||
var context = new CommandContext(Client, message.Author, command.Name, commandNameString, rawArguments.ToString());
|
var context = new CommandContext(Client, message.Author, command.Name, commandNameString, rawArguments.ToString());
|
||||||
object?[] arguments = {context};
|
|
||||||
|
|
||||||
|
foreach (var attribute in command.Method.GetCustomAttributes<PreExecutionCheckAttribute>())
|
||||||
|
{
|
||||||
|
if (!attribute.PerformAsync(context).ConfigureAwait(false).GetAwaiter().GetResult())
|
||||||
|
{
|
||||||
|
return base.OnMessageReceived(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object?[] arguments = {context};
|
||||||
if (rawArguments.Length > 0)
|
if (rawArguments.Length > 0)
|
||||||
{
|
{
|
||||||
spaceIndex = rawArguments.IndexOf(' ');
|
spaceIndex = rawArguments.IndexOf(' ');
|
||||||
|
Loading…
Reference in New Issue
Block a user