From feed4fd752cf3ce33aa7643f20675a758d745d3f Mon Sep 17 00:00:00 2001 From: Joe4evr Date: Fri, 4 Jan 2019 17:56:57 +0100 Subject: [PATCH] docs: Replace obsolete Precondition sample with something new (#1230) * Replace obsolete Precondition sample with something new * Feedback Whoops. :ok_hand: Co-Authored-By: Joe4evr --- docs/guides/commands/preconditions.md | 2 +- .../samples/preconditions/require_owner.cs | 28 ---------------- .../samples/preconditions/require_role.cs | 32 +++++++++++++++++++ 3 files changed, 33 insertions(+), 29 deletions(-) delete mode 100644 docs/guides/commands/samples/preconditions/require_owner.cs create mode 100644 docs/guides/commands/samples/preconditions/require_role.cs diff --git a/docs/guides/commands/preconditions.md b/docs/guides/commands/preconditions.md index 44b10c513..8e8298b86 100644 --- a/docs/guides/commands/preconditions.md +++ b/docs/guides/commands/preconditions.md @@ -76,7 +76,7 @@ necessary. ### Example - Creating a Custom Precondition -[!code-csharp[Custom Precondition](samples/preconditions/require_owner.cs)] +[!code-csharp[Custom Precondition](samples/preconditions/require_role.cs)] [CheckPermissionsAsync]: xref:Discord.Commands.PreconditionAttribute.CheckPermissionsAsync* [PreconditionResult.FromSuccess]: xref:Discord.Commands.PreconditionResult.FromSuccess* diff --git a/docs/guides/commands/samples/preconditions/require_owner.cs b/docs/guides/commands/samples/preconditions/require_owner.cs deleted file mode 100644 index f17b7d9a7..000000000 --- a/docs/guides/commands/samples/preconditions/require_owner.cs +++ /dev/null @@ -1,28 +0,0 @@ -// (Note: This precondition is obsolete, it is recommended to use the -// RequireOwnerAttribute that is bundled with Discord.Commands) - -using Discord.Commands; -using Discord.WebSocket; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Threading.Tasks; - -// Inherit from PreconditionAttribute -public class RequireOwnerAttribute : PreconditionAttribute -{ - // Override the CheckPermissions method - public async override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) - { - // Get the client via Depedency Injection - var client = services.GetRequiredService(); - // Get the ID of the bot's owner - var appInfo = await client.GetApplicationInfoAsync().ConfigureAwait(false); - var ownerId = appInfo.Owner.Id; - // If this command was executed by that user, return a success - if (context.User.Id == ownerId) - return PreconditionResult.FromSuccess(); - // Since it wasn't, fail - else - return PreconditionResult.FromError("You must be the owner of the bot to run this command."); - } -} diff --git a/docs/guides/commands/samples/preconditions/require_role.cs b/docs/guides/commands/samples/preconditions/require_role.cs new file mode 100644 index 000000000..77d09b525 --- /dev/null +++ b/docs/guides/commands/samples/preconditions/require_role.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading.Tasks; +using Discord.Commands; +using Discord.WebSocket; + +// Inherit from PreconditionAttribute +public class RequireRoleAttribute : PreconditionAttribute +{ + // Create a field to store the specified name + private readonly string _name; + + // Create a constructor so the name can be specified + public RequireRoleAttribute(string name) => _name = name; + + // Override the CheckPermissions method + public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + { + // Check if this user is a Guild User, which is the only context where roles exist + if (context.User is SocketGuildUser gUser) + { + // If this command was executed by a user with the appropriate role, return a success + if (gUser.Roles.Any(r => r.Name == _name)) + // Since no async work is done, the result has to be wrapped with `Task.FromResult` to avoid compiler errors + return Task.FromResult(PreconditionResult.FromSuccess()); + // Since it wasn't, fail + else + return Task.FromResult(PreconditionResult.FromError($"You must have a role named {_name} to run this command.")); + } + else + return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command.")); + } +}