1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-12 22:35:42 +00:00

Add pre-execution checks

This commit is contained in:
Oliver Booth 2022-12-05 01:43:40 +00:00
parent a30f15b452
commit d82a4d2e5e
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
7 changed files with 144 additions and 1 deletions

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -2,6 +2,7 @@ using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using VpSharp.ClientExtensions;
using VpSharp.Commands.Attributes;
using VpSharp.Commands.Attributes.ExecutionChecks;
using VpSharp.Entities;
using VpSharp.EventData;
@ -218,8 +219,16 @@ public sealed class CommandsExtension : VirtualParadiseClientExtension
}
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)
{
spaceIndex = rawArguments.IndexOf(' ');