Add API model for Reaction, implement REST methods for reactions

This commit is contained in:
Christopher F
2016-10-31 19:50:38 -04:00
parent 9fcf88b7ac
commit 838d60e2c2
9 changed files with 146 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Discord.API.Common
{
public class Reaction
{
[JsonProperty("user_id")]
public ulong UserId { get; set; }
[JsonProperty("message_id")]
public ulong MessageId { get; set; }
[JsonProperty("emoji")]
public Emoji Emoji { get; set; }
[JsonProperty("channel_id")]
public ulong ChannelId { get; set; }
}
}

View File

@@ -512,6 +512,48 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Message>("PATCH", () => $"channels/{channelId}/messages/{messageId}", args, ids, clientBucketId: ClientBucket.SendEditId, options: options).ConfigureAwait(false);
}
public async Task AddReactionAsync(ulong channelId, ulong messageId, string emoji, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));
options = RequestOptions.CreateOrClone(options);
var ids = new BucketIds(channelId: channelId);
await SendAsync("PUT", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/@me", ids, options: options).ConfigureAwait(false);
}
public async Task RemoveReactionAsync(ulong channelId, ulong messageId, ulong userId, string emoji, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));
options = RequestOptions.CreateOrClone(options);
var ids = new BucketIds(channelId: channelId);
await SendAsync("DELETE", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/{userId}", ids, options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<User>> GetReactionUsersAsync(ulong channelId, ulong messageId, string emoji, GetReactionUsersParams args, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));
Preconditions.NotNull(args, nameof(args));
Preconditions.GreaterThan(args.Limit, 0, nameof(args.Limit));
Preconditions.AtMost(args.Limit, DiscordConfig.MaxUsersPerBatch, nameof(args.Limit));
Preconditions.GreaterThan(args.AfterUserId, 0, nameof(args.AfterUserId));
options = RequestOptions.CreateOrClone(options);
int limit = args.Limit.GetValueOrDefault(int.MaxValue);
ulong afterUserId = args.AfterUserId.GetValueOrDefault(0);
var ids = new BucketIds(channelId: channelId);
Expression<Func<string>> endpoint = () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}";
return await SendAsync<IReadOnlyCollection<User>>("GET", endpoint, ids, options: options).ConfigureAwait(false);
}
public async Task AckMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));

View File

@@ -0,0 +1,8 @@
namespace Discord.API.Rest
{
public class GetReactionUsersParams
{
public Optional<int> Limit { get; set; }
public Optional<ulong> AfterUserId { get; set; }
}
}

View File

@@ -1,5 +1,6 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Discord
@@ -12,7 +13,17 @@ namespace Discord
Task PinAsync(RequestOptions options = null);
/// <summary> Removes this message from its channel's pinned messages. </summary>
Task UnpinAsync(RequestOptions options = null);
/// <summary> Adds a reaction to this message. </summary>
Task AddReactionAsync(Emoji emoji, RequestOptions options = null);
/// <summary> Adds a reaction to this message. </summary>
Task AddReactionAsync(string emoji, RequestOptions options = null);
/// <summary> Removes a reaction from message. </summary>
Task RemoveReactionAsync(Emoji emoji, IUser user, RequestOptions options = null);
/// <summary> Removes a reaction from this message. </summary>
Task RemoveReactionAsync(string emoji, IUser user, RequestOptions options = null);
Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(string emoji, Action<GetReactionUsersParams> func, RequestOptions options = null);
/// <summary> Transforms this message's text into a human readable form by resolving its tags. </summary>
string Resolve(
TagHandling userHandling = TagHandling.Name,

View File

@@ -45,6 +45,7 @@ namespace Discord
throw new ArgumentException("Argument cannot be blank.", name);
}
}
//Numerics
public static void NotEqual(sbyte obj, sbyte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); }