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.

RequireBotPermissionAttribute.cs 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. namespace Discord.Commands
  6. {
  7. /// <summary>
  8. /// This attribute requires that the bot has a speicifed permission in the channel a command is invoked in.
  9. /// </summary>
  10. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  11. public class RequireBotPermissionAttribute : PreconditionAttribute
  12. {
  13. public GuildPermission? GuildPermission { get; }
  14. public ChannelPermission? ChannelPermission { get; }
  15. /// <summary>
  16. /// Require that the bot account 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 bot must have. Multiple permissions can be specified by ORing or ANDing the permissions together.</param>
  20. public RequireBotPermissionAttribute(GuildPermission permission)
  21. {
  22. GuildPermission = permission;
  23. ChannelPermission = null;
  24. }
  25. /// <summary>
  26. /// Require that the bot account has a specified ChannelPermission.
  27. /// </summary>
  28. /// <param name="permission">The ChannelPermission that the bot must have. Multiple permissions can be specified by ORing or ANDing the permissions together.</param>
  29. /// <example>
  30. /// <code language="c#">
  31. /// [Command("permission")]
  32. /// [RequireBotPermission(ChannelPermission.ManageMessages)]
  33. /// public async Task Purge()
  34. /// {
  35. /// }
  36. /// </code>
  37. /// </example>
  38. public RequireBotPermissionAttribute(ChannelPermission permission)
  39. {
  40. ChannelPermission = permission;
  41. GuildPermission = null;
  42. }
  43. public override async Task<PreconditionResult> CheckPermissions(CommandContext context, CommandInfo command, IDependencyMap map)
  44. {
  45. var guildUser = await context.Guild.GetCurrentUserAsync();
  46. if (GuildPermission.HasValue)
  47. {
  48. if (guildUser == null)
  49. return PreconditionResult.FromError("Command must be used in a guild channel");
  50. if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
  51. return PreconditionResult.FromError($"Command requires guild permission {GuildPermission.Value}");
  52. }
  53. if (ChannelPermission.HasValue)
  54. {
  55. var guildChannel = context.Channel as IGuildChannel;
  56. ChannelPermissions perms;
  57. if (guildChannel != null)
  58. perms = guildUser.GetPermissions(guildChannel);
  59. else
  60. perms = ChannelPermissions.All(guildChannel);
  61. if (!perms.Has(ChannelPermission.Value))
  62. return PreconditionResult.FromError($"Command requires channel permission {ChannelPermission.Value}");
  63. }
  64. return PreconditionResult.FromSuccess();
  65. }
  66. }
  67. }