From 683541ba24b75ca06599f7273add2f635c19b59d Mon Sep 17 00:00:00 2001 From: RogueException Date: Sat, 18 Mar 2017 08:38:24 -0300 Subject: [PATCH] Added RequireOwner support for User tokens --- .../Preconditions/RequireOwnerAttribute.cs | 17 ++++++++++++++--- src/Discord.Net.Core/IDiscordClient.cs | 1 + src/Discord.Net.Rest/BaseDiscordClient.cs | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs b/src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs index cfedcad23..0f4e8255d 100644 --- a/src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs +++ b/src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs @@ -12,9 +12,20 @@ namespace Discord.Commands { public override async Task CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) { - var application = await context.Client.GetApplicationInfoAsync(); - if (context.User.Id == application.Owner.Id) return PreconditionResult.FromSuccess(); - return PreconditionResult.FromError("Command can only be run by the owner of the bot"); + switch (context.Client.TokenType) + { + case TokenType.Bot: + var application = await context.Client.GetApplicationInfoAsync(); + if (context.User.Id != application.Owner.Id) + return PreconditionResult.FromError("Command can only be run by the owner of the bot"); + return PreconditionResult.FromSuccess(); + case TokenType.User: + if (context.User.Id != context.Client.CurrentUser.Id) + return PreconditionResult.FromError("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)}."); + } } } } diff --git a/src/Discord.Net.Core/IDiscordClient.cs b/src/Discord.Net.Core/IDiscordClient.cs index 1c5ec41c1..c434ccd7b 100644 --- a/src/Discord.Net.Core/IDiscordClient.cs +++ b/src/Discord.Net.Core/IDiscordClient.cs @@ -9,6 +9,7 @@ namespace Discord { ConnectionState ConnectionState { get; } ISelfUser CurrentUser { get; } + TokenType TokenType { get; } Task StartAsync(); Task StopAsync(); diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs index 80c4cb598..df4f180b2 100644 --- a/src/Discord.Net.Rest/BaseDiscordClient.cs +++ b/src/Discord.Net.Rest/BaseDiscordClient.cs @@ -26,6 +26,7 @@ namespace Discord.Rest internal LogManager LogManager { get; } public LoginState LoginState { get; private set; } public ISelfUser CurrentUser { get; protected set; } + public TokenType TokenType => ApiClient.AuthTokenType; /// Creates a new REST-only discord client. internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client)