using System; using System.Threading.Tasks; namespace Discord.Commands { /// /// Requires the command to be invoked by the owner of the bot. /// /// /// This precondition will restrict the access of the command or module to the owner of the Discord application. /// If the precondition fails to be met, an erroneous will be returned with the /// message "Command can only be run by the owner of the bot." /// /// This precondition will only work if the account has a of /// ;otherwise, this precondition will always fail. /// /// /// /// The following example restricts the command to a set of sensitive commands that only the owner of the bot /// application should be able to access. /// /// [RequireOwner] /// [Group("admin")] /// public class AdminModule : ModuleBase /// { /// [Command("exit")] /// public async Task ExitAsync() /// { /// Environment.Exit(0); /// } /// } /// /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class RequireOwnerAttribute : PreconditionAttribute { /// public override string ErrorMessage { get; set; } /// public override async Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { switch (context.Client.TokenType) { case TokenType.Bot: var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); if (context.User.Id != application.Owner.Id) return PreconditionResult.FromError(ErrorMessage ?? "Command can only be run by the owner of the bot."); return PreconditionResult.FromSuccess(); default: return PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}."); } } } }