docs: Replace obsolete Precondition sample with something new (#1230)
* Replace obsolete Precondition sample with something new * Feedback Whoops. 👌 Co-Authored-By: Joe4evr <jii.geugten@gmail.com>
This commit is contained in:
@@ -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*
|
||||
|
||||
@@ -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<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
// Get the client via Depedency Injection
|
||||
var client = services.GetRequiredService<DiscordSocketClient>();
|
||||
// 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.");
|
||||
}
|
||||
}
|
||||
32
docs/guides/commands/samples/preconditions/require_role.cs
Normal file
32
docs/guides/commands/samples/preconditions/require_role.cs
Normal file
@@ -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<PreconditionResult> 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."));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user