Browse Source

Added LoggedInAs precondition to check if current client is logged in as a user. Used for relationship endpoints.

pull/745/head
ObsidianMinor 8 years ago
parent
commit
985268f777
2 changed files with 11 additions and 0 deletions
  1. +2
    -0
      src/Discord.Net.Core/Utils/Preconditions.cs
  2. +9
    -0
      src/Discord.Net.Rest/DiscordRestApiClient.cs

+ 2
- 0
src/Discord.Net.Core/Utils/Preconditions.cs View File

@@ -192,5 +192,7 @@ namespace Discord
throw new ArgumentOutOfRangeException(name, "Messages must be younger than two weeks old.");
}
}

public static void LoggedInAs(TokenType token, TokenType existing) { if (token != existing) throw new NotSupportedException($"Must be logged in as a {token} to use this endpoint"); }
}
}

+ 9
- 0
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -1077,12 +1077,17 @@ namespace Discord.API
//Relationships
public async Task<IReadOnlyCollection<Relationship>> GetRelationshipsAsync(RequestOptions options = null)
{
Preconditions.LoggedInAs(TokenType.User, AuthTokenType);

options = RequestOptions.CreateOrClone(options);

return await SendAsync<IReadOnlyCollection<Relationship>>("GET", () => "users/@me/relationships", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task AddFriendAsync(ulong userId, RequestOptions options = null)
{
Preconditions.NotEqual(userId, 0, nameof(userId));
Preconditions.LoggedInAs(TokenType.User, AuthTokenType);

options = RequestOptions.CreateOrClone(options);

await SendJsonAsync("PUT", () => $"users/@me/relationships/{userId}", new object(), new BucketIds(), options: options).ConfigureAwait(false);
@@ -1090,6 +1095,8 @@ namespace Discord.API
public async Task BlockUserAsync(ulong userId, RequestOptions options = null)
{
Preconditions.NotEqual(userId, 0, nameof(userId));
Preconditions.LoggedInAs(TokenType.User, AuthTokenType);

options = RequestOptions.CreateOrClone(options);

await SendJsonAsync("PUT", () => $"users/@me/relationships/{userId}", new { type = 2 }, new BucketIds(), options: options).ConfigureAwait(false);
@@ -1097,6 +1104,8 @@ namespace Discord.API
public async Task RemoveRelationshipAsync(ulong userId, RequestOptions options = null)
{
Preconditions.NotEqual(userId, 0, nameof(userId));
Preconditions.LoggedInAs(TokenType.User, AuthTokenType);

options = RequestOptions.CreateOrClone(options);

await SendAsync("DELETE", () => $"users/@me/relationships/{userId}", new BucketIds(), options: options).ConfigureAwait(false);


Loading…
Cancel
Save