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

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