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.6 kB

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