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.

require_owner.cs 1.2 kB

12345678910111213141516171819202122232425262728
  1. // (Note: This precondition is obsolete, it is recommended to use the
  2. // RequireOwnerAttribute that is bundled with Discord.Commands)
  3. using Discord.Commands;
  4. using Discord.WebSocket;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.Threading.Tasks;
  8. // Inherit from PreconditionAttribute
  9. public class RequireOwnerAttribute : PreconditionAttribute
  10. {
  11. // Override the CheckPermissions method
  12. public async override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
  13. {
  14. // Get the client via Depedency Injection
  15. var client = services.GetRequiredService<DiscordSocketClient>();
  16. // Get the ID of the bot's owner
  17. var appInfo = await client.GetApplicationInfoAsync().ConfigureAwait(false);
  18. var ownerId = appInfo.Owner.Id;
  19. // If this command was executed by that user, return a success
  20. if (context.User.Id == ownerId)
  21. return PreconditionResult.FromSuccess();
  22. // Since it wasn't, fail
  23. else
  24. return PreconditionResult.FromError("You must be the owner of the bot to run this command.");
  25. }
  26. }