2024-01-20 17:30:24 +00:00
|
|
|
|
using VpSharp.Entities;
|
|
|
|
|
|
|
|
|
|
namespace VpSharp.Commands.Attributes.ExecutionChecks;
|
2022-12-05 01:43:40 +00:00
|
|
|
|
|
2023-05-08 16:00:00 +01:00
|
|
|
|
#pragma warning disable CA1019 // Define accessors for attribute arguments
|
|
|
|
|
|
2022-12-05 01:43:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Specifies that this command can only be run by bots.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class RequireUserNameAttribute : PreExecutionCheckAttribute
|
|
|
|
|
{
|
2023-05-08 16:00:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RequireUserNameAttribute" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="names">An enumerable collection of allowed user names.</param>
|
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="names" /> is <see langword="null" />.</exception>
|
|
|
|
|
public RequireUserNameAttribute(IEnumerable<string> names)
|
|
|
|
|
{
|
2024-02-15 22:38:48 +00:00
|
|
|
|
if (names is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(names));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 16:00:00 +01:00
|
|
|
|
Names = names.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 01:43:40 +00:00
|
|
|
|
/// <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>
|
2023-05-08 16:00:00 +01:00
|
|
|
|
[CLSCompliant(false)]
|
2022-12-05 01:43:40 +00:00
|
|
|
|
public RequireUserNameAttribute(params string[] names)
|
|
|
|
|
{
|
2024-02-15 22:38:48 +00:00
|
|
|
|
if (names is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(names));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 01:43:40 +00:00
|
|
|
|
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 />
|
2024-06-11 17:08:45 +01:00
|
|
|
|
protected internal override async Task<bool> PerformAsync(CommandContext context)
|
2022-12-05 01:43:40 +00:00
|
|
|
|
{
|
2024-02-15 22:38:48 +00:00
|
|
|
|
if (context is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(context));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 17:08:45 +01:00
|
|
|
|
VirtualParadiseUser user = await context.Avatar.GetUserAsync().ConfigureAwait(false);
|
|
|
|
|
return Names.Contains(user.Name);
|
2022-12-05 01:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|