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.

RequireOwnerAttribute.cs 1.3 kB

123456789101112131415161718192021222324252627
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace Discord.Commands
  4. {
  5. /// <summary>
  6. /// This attribute requires that the command to be invoked by the owner of the bot.
  7. /// </summary>
  8. /// <remarks>This precondition will only work if the bot is a bot account.</remarks>
  9. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
  10. public class RequireOwnerAttribute : PreconditionAttribute
  11. {
  12. public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
  13. {
  14. switch (context.Client.TokenType)
  15. {
  16. case TokenType.Bot:
  17. var application = await context.Client.GetApplicationInfoAsync();
  18. if (context.User.Id != application.Owner.Id)
  19. return PreconditionResult.FromError("Command can only be run by the owner of the bot");
  20. return PreconditionResult.FromSuccess();
  21. default:
  22. return PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}.");
  23. }
  24. }
  25. }
  26. }