You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreconditionAttribute.Overwrites.md 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ---
  2. uid: Discord.Commands.PreconditionAttribute
  3. remarks: *content
  4. ---
  5. This precondition attribute can be applied on module-level or
  6. method-level for a command.
  7. [!include[Additional Remarks](PreconditionAttribute.Remarks.Inclusion.md)]
  8. ---
  9. uid: Discord.Commands.ParameterPreconditionAttribute
  10. remarks: *content
  11. ---
  12. This precondition attribute can be applied on parameter-level for a
  13. command.
  14. [!include[Additional Remarks](PreconditionAttribute.Remarks.Inclusion.md)]
  15. ---
  16. uid: Discord.Commands.PreconditionAttribute
  17. example: [*content]
  18. ---
  19. The following example creates a precondition to see if the user has
  20. sufficient role required to access the command.
  21. ```cs
  22. public class RequireRoleAttribute : PreconditionAttribute
  23. {
  24. private readonly ulong _roleId;
  25. public RequireRoleAttribute(ulong roleId)
  26. {
  27. _roleId = roleId;
  28. }
  29. public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context,
  30. CommandInfo command, IServiceProvider services)
  31. {
  32. var guildUser = context.User as IGuildUser;
  33. if (guildUser == null)
  34. return PreconditionResult.FromError("This command cannot be executed outside of a guild.");
  35. var guild = guildUser.Guild;
  36. if (guild.Roles.All(r => r.Id != _roleId))
  37. return PreconditionResult.FromError(
  38. $"The guild does not have the role ({_roleId}) required to access this command.");
  39. return guildUser.RoleIds.Any(rId => rId == _roleId)
  40. ? PreconditionResult.FromSuccess()
  41. : PreconditionResult.FromError("You do not have the sufficient role required to access this command.");
  42. }
  43. }
  44. ```
  45. ---
  46. uid: Discord.Commands.ParameterPreconditionAttribute
  47. example: [*content]
  48. ---
  49. The following example creates a precondition on a parameter-level to
  50. see if the targeted user has a lower hierarchy than the user who
  51. executed the command.
  52. ```cs
  53. public class RequireHierarchyAttribute : ParameterPreconditionAttribute
  54. {
  55. public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context,
  56. ParameterInfo parameter, object value, IServiceProvider services)
  57. {
  58. // Hierarchy is only available under the socket variant of the user.
  59. if (!(context.User is SocketGuildUser guildUser))
  60. return PreconditionResult.FromError("This command cannot be used outside of a guild.");
  61. SocketGuildUser targetUser;
  62. switch (value)
  63. {
  64. case SocketGuildUser targetGuildUser:
  65. targetUser = targetGuildUser;
  66. break;
  67. case ulong userId:
  68. targetUser = await context.Guild.GetUserAsync(userId).ConfigureAwait(false) as SocketGuildUser;
  69. break;
  70. default:
  71. throw new ArgumentOutOfRangeException();
  72. }
  73. if (targetUser == null)
  74. return PreconditionResult.FromError("Target user not found.");
  75. if (guildUser.Hierarchy < targetUser.Hierarchy)
  76. return PreconditionResult.FromError("You cannot target anyone else whose roles are higher than yours.");
  77. var currentUser = await context.Guild.GetCurrentUserAsync().ConfigureAwait(false) as SocketGuildUser;
  78. if (currentUser?.Hierarchy < targetUser.Hierarchy)
  79. return PreconditionResult.FromError("The bot's role is lower than the targeted user.");
  80. return PreconditionResult.FromSuccess();
  81. }
  82. }
  83. ```