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.

RequireUserPermissionAttribute.cs 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace Discord.Commands
  4. {
  5. /// <summary>
  6. /// This attribute requires that the user invoking the command has a specified permission.
  7. /// </summary>
  8. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
  9. public class RequireUserPermissionAttribute : PreconditionAttribute
  10. {
  11. public GuildPermission? GuildPermission { get; }
  12. public ChannelPermission? ChannelPermission { get; }
  13. public override string ErrorMessage { get; set; }
  14. public string NotAGuild { get; set; }
  15. /// <summary>
  16. /// Require that the user invoking the command has a specified GuildPermission
  17. /// </summary>
  18. /// <remarks>This precondition will always fail if the command is being invoked in a private channel.</remarks>
  19. /// <param name="permission">The GuildPermission that the user must have. Multiple permissions can be specified by ORing the permissions together.</param>
  20. public RequireUserPermissionAttribute(GuildPermission permission)
  21. {
  22. GuildPermission = permission;
  23. ChannelPermission = null;
  24. }
  25. /// <summary>
  26. /// Require that the user invoking the command has a specified ChannelPermission.
  27. /// </summary>
  28. /// <param name="permission">The ChannelPermission that the user must have. Multiple permissions can be specified by ORing the permissions together.</param>
  29. /// <example>
  30. /// <code language="c#">
  31. /// [Command("permission")]
  32. /// [RequireUserPermission(ChannelPermission.ReadMessageHistory | ChannelPermission.ReadMessages)]
  33. /// public async Task HasPermission()
  34. /// {
  35. /// await ReplyAsync("You can read messages and the message history!");
  36. /// }
  37. /// </code>
  38. /// </example>
  39. public RequireUserPermissionAttribute(ChannelPermission permission)
  40. {
  41. ChannelPermission = permission;
  42. GuildPermission = null;
  43. }
  44. public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
  45. {
  46. var guildUser = context.User as IGuildUser;
  47. if (GuildPermission.HasValue)
  48. {
  49. if (guildUser == null)
  50. return Task.FromResult(PreconditionResult.FromError(NotAGuild ?? "Command must be used in a guild channel"));
  51. if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
  52. return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? $"User requires guild permission {GuildPermission.Value}"));
  53. }
  54. if (ChannelPermission.HasValue)
  55. {
  56. ChannelPermissions perms;
  57. if (context.Channel is IGuildChannel guildChannel)
  58. perms = guildUser.GetPermissions(guildChannel);
  59. else
  60. perms = ChannelPermissions.All(context.Channel);
  61. if (!perms.Has(ChannelPermission.Value))
  62. return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? $"User requires channel permission {ChannelPermission.Value}"));
  63. }
  64. return Task.FromResult(PreconditionResult.FromSuccess());
  65. }
  66. }
  67. }