From 7022149536ea1c9a92d6dce846cdf7d1df76772e Mon Sep 17 00:00:00 2001
From: Still Hsu <341464@gmail.com>
Date: Sun, 29 Apr 2018 23:11:06 +0800
Subject: [PATCH] Fix/Implement various invite-related behaviors (#1023)
* Initial support for invite member count arg
* Fix IDiscordClient#GetInviteAsync behavior
- Previously, the GetInviteAsync method would return null since it couldn't be parsed as a simple RestInvite object. The object should be a RestInviteMetadata instead.
* Fix methods that didn't comply with the interface
* Change with_counts REST behaviour
* Remove unnecessary JSON prop
* Remove AcceptAsync
---
src/Discord.Net.Core/Entities/Invites/IInvite.cs | 9 +++++----
src/Discord.Net.Core/IDiscordClient.cs | 2 +-
src/Discord.Net.Rest/API/Common/Invite.cs | 6 +++++-
src/Discord.Net.Rest/API/Rest/GetInviteParams.cs | 7 +++++++
src/Discord.Net.Rest/BaseDiscordClient.cs | 2 +-
src/Discord.Net.Rest/ClientHelper.cs | 12 ++++++++----
src/Discord.Net.Rest/DiscordRestApiClient.cs | 13 ++++---------
src/Discord.Net.Rest/DiscordRestClient.cs | 8 ++++----
.../Entities/Invites/InviteHelper.cs | 7 +------
.../Entities/Invites/RestInvite.cs | 15 ++++++++++-----
src/Discord.Net.WebSocket/BaseSocketClient.cs | 8 ++++----
src/Discord.Net.WebSocket/DiscordShardedClient.cs | 4 ++--
src/Discord.Net.WebSocket/DiscordSocketClient.cs | 4 ++--
13 files changed, 54 insertions(+), 43 deletions(-)
create mode 100644 src/Discord.Net.Rest/API/Rest/GetInviteParams.cs
diff --git a/src/Discord.Net.Core/Entities/Invites/IInvite.cs b/src/Discord.Net.Core/Entities/Invites/IInvite.cs
index 73555e453..0ebb65679 100644
--- a/src/Discord.Net.Core/Entities/Invites/IInvite.cs
+++ b/src/Discord.Net.Core/Entities/Invites/IInvite.cs
@@ -1,4 +1,4 @@
-using System.Threading.Tasks;
+using System.Threading.Tasks;
namespace Discord
{
@@ -22,8 +22,9 @@ namespace Discord
ulong GuildId { get; }
/// Gets the name of the guild this invite is linked to.
string GuildName { get; }
-
- /// Accepts this invite and joins the target guild. This will fail on bot accounts.
- Task AcceptAsync(RequestOptions options = null);
+ /// Gets the approximated count of online members in the guild.
+ int? PresenceCount { get; }
+ /// Gets the approximated count of total members in the guild.
+ int? MemberCount { get; }
}
}
diff --git a/src/Discord.Net.Core/IDiscordClient.cs b/src/Discord.Net.Core/IDiscordClient.cs
index a383c37da..083c0b512 100644
--- a/src/Discord.Net.Core/IDiscordClient.cs
+++ b/src/Discord.Net.Core/IDiscordClient.cs
@@ -27,7 +27,7 @@ namespace Discord
Task> GetGuildsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null);
- Task GetInviteAsync(string inviteId, RequestOptions options = null);
+ Task GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null);
Task GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task GetUserAsync(string username, string discriminator, RequestOptions options = null);
diff --git a/src/Discord.Net.Rest/API/Common/Invite.cs b/src/Discord.Net.Rest/API/Common/Invite.cs
index 67a318c5a..1b35da870 100644
--- a/src/Discord.Net.Rest/API/Common/Invite.cs
+++ b/src/Discord.Net.Rest/API/Common/Invite.cs
@@ -1,4 +1,4 @@
-#pragma warning disable CS1591
+#pragma warning disable CS1591
using Newtonsoft.Json;
namespace Discord.API
@@ -11,5 +11,9 @@ namespace Discord.API
public InviteGuild Guild { get; set; }
[JsonProperty("channel")]
public InviteChannel Channel { get; set; }
+ [JsonProperty("approximate_presence_count")]
+ public Optional PresenceCount { get; set; }
+ [JsonProperty("approximate_member_count")]
+ public Optional MemberCount { get; set; }
}
}
diff --git a/src/Discord.Net.Rest/API/Rest/GetInviteParams.cs b/src/Discord.Net.Rest/API/Rest/GetInviteParams.cs
new file mode 100644
index 000000000..cb8d8f7fe
--- /dev/null
+++ b/src/Discord.Net.Rest/API/Rest/GetInviteParams.cs
@@ -0,0 +1,7 @@
+namespace Discord.API.Rest
+{
+ internal class GetInviteParams
+ {
+ public Optional WithCounts { get; set; }
+ }
+}
diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs
index f8642b96c..db8e2e691 100644
--- a/src/Discord.Net.Rest/BaseDiscordClient.cs
+++ b/src/Discord.Net.Rest/BaseDiscordClient.cs
@@ -148,7 +148,7 @@ namespace Discord.Rest
Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> Task.FromResult>(ImmutableArray.Create());
- Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
+ Task IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
=> Task.FromResult(null);
Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
diff --git a/src/Discord.Net.Rest/ClientHelper.cs b/src/Discord.Net.Rest/ClientHelper.cs
index 08305f857..a1f8ece69 100644
--- a/src/Discord.Net.Rest/ClientHelper.cs
+++ b/src/Discord.Net.Rest/ClientHelper.cs
@@ -50,12 +50,16 @@ namespace Discord.Rest
return models.Select(x => RestConnection.Create(x)).ToImmutableArray();
}
- public static async Task GetInviteAsync(BaseDiscordClient client,
- string inviteId, RequestOptions options)
+ public static async Task GetInviteAsync(BaseDiscordClient client,
+ string inviteId, bool withCount, RequestOptions options)
{
- var model = await client.ApiClient.GetInviteAsync(inviteId, options).ConfigureAwait(false);
+ var args = new GetInviteParams
+ {
+ WithCounts = withCount
+ };
+ var model = await client.ApiClient.GetInviteAsync(inviteId, args, options).ConfigureAwait(false);
if (model != null)
- return RestInvite.Create(client, null, null, model);
+ return RestInviteMetadata.Create(client, null, null, model);
return null;
}
diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs
index f0c4358ad..7ec30c3aa 100644
--- a/src/Discord.Net.Rest/DiscordRestApiClient.cs
+++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs
@@ -897,7 +897,7 @@ namespace Discord.API
}
//Guild Invites
- public async Task GetInviteAsync(string inviteId, RequestOptions options = null)
+ public async Task GetInviteAsync(string inviteId, GetInviteParams args, RequestOptions options = null)
{
Preconditions.NotNullOrEmpty(inviteId, nameof(inviteId));
options = RequestOptions.CreateOrClone(options);
@@ -910,9 +910,11 @@ namespace Discord.API
if (index >= 0)
inviteId = inviteId.Substring(index + 1);
+ var withCounts = args.WithCounts.GetValueOrDefault(false);
+
try
{
- return await SendAsync("GET", () => $"invites/{inviteId}", new BucketIds(), options: options).ConfigureAwait(false);
+ return await SendAsync("GET", () => $"invites/{inviteId}?with_counts={withCounts}", new BucketIds(), options: options).ConfigureAwait(false);
}
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; }
}
@@ -950,13 +952,6 @@ namespace Discord.API
return await SendAsync("DELETE", () => $"invites/{inviteId}", new BucketIds(), options: options).ConfigureAwait(false);
}
- public async Task AcceptInviteAsync(string inviteId, RequestOptions options = null)
- {
- Preconditions.NotNullOrEmpty(inviteId, nameof(inviteId));
- options = RequestOptions.CreateOrClone(options);
-
- await SendAsync("POST", () => $"invites/{inviteId}", new BucketIds(), options: options).ConfigureAwait(false);
- }
//Guild Members
public async Task GetGuildMemberAsync(ulong guildId, ulong userId, RequestOptions options = null)
diff --git a/src/Discord.Net.Rest/DiscordRestClient.cs b/src/Discord.Net.Rest/DiscordRestClient.cs
index 8850da3a5..3a596624d 100644
--- a/src/Discord.Net.Rest/DiscordRestClient.cs
+++ b/src/Discord.Net.Rest/DiscordRestClient.cs
@@ -56,8 +56,8 @@ namespace Discord.Rest
=> ClientHelper.GetConnectionsAsync(this, options);
///
- public Task GetInviteAsync(string inviteId, RequestOptions options = null)
- => ClientHelper.GetInviteAsync(this, inviteId, options);
+ public Task GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null)
+ => ClientHelper.GetInviteAsync(this, inviteId, withCount, options);
///
public Task GetGuildAsync(ulong id, RequestOptions options = null)
@@ -131,8 +131,8 @@ namespace Discord.Rest
async Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> await GetConnectionsAsync(options).ConfigureAwait(false);
- async Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
- => await GetInviteAsync(inviteId, options).ConfigureAwait(false);
+ async Task IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
+ => await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
async Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
{
diff --git a/src/Discord.Net.Rest/Entities/Invites/InviteHelper.cs b/src/Discord.Net.Rest/Entities/Invites/InviteHelper.cs
index 80a49e34e..ebcd93777 100644
--- a/src/Discord.Net.Rest/Entities/Invites/InviteHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Invites/InviteHelper.cs
@@ -1,14 +1,9 @@
-using System.Threading.Tasks;
+using System.Threading.Tasks;
namespace Discord.Rest
{
internal static class InviteHelper
{
- public static async Task AcceptAsync(IInvite invite, BaseDiscordClient client,
- RequestOptions options)
- {
- await client.ApiClient.AcceptInviteAsync(invite.Code, options).ConfigureAwait(false);
- }
public static async Task DeleteAsync(IInvite invite, BaseDiscordClient client,
RequestOptions options)
{
diff --git a/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs b/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs
index 900d1f0ac..18698c626 100644
--- a/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs
+++ b/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs
@@ -1,6 +1,7 @@
-using System;
+using System;
using System.Diagnostics;
using System.Threading.Tasks;
+using Discord.API.Rest;
using Model = Discord.API.Invite;
namespace Discord.Rest
@@ -10,6 +11,8 @@ namespace Discord.Rest
{
public string ChannelName { get; private set; }
public string GuildName { get; private set; }
+ public int? PresenceCount { get; private set; }
+ public int? MemberCount { get; private set; }
public ulong ChannelId { get; private set; }
public ulong GuildId { get; private set; }
internal IChannel Channel { get; private set; }
@@ -36,19 +39,21 @@ namespace Discord.Rest
ChannelId = model.Channel.Id;
GuildName = model.Guild.Name;
ChannelName = model.Channel.Name;
+ MemberCount = model.MemberCount.IsSpecified ? model.MemberCount.Value : null;
+ PresenceCount = model.PresenceCount.IsSpecified ? model.PresenceCount.Value : null;
}
public async Task UpdateAsync(RequestOptions options = null)
{
- var model = await Discord.ApiClient.GetInviteAsync(Code, options).ConfigureAwait(false);
+ var args = new GetInviteParams();
+ if (MemberCount != null || PresenceCount != null)
+ args.WithCounts = true;
+ var model = await Discord.ApiClient.GetInviteAsync(Code, args, options).ConfigureAwait(false);
Update(model);
}
public Task DeleteAsync(RequestOptions options = null)
=> InviteHelper.DeleteAsync(this, Discord, options);
- public Task AcceptAsync(RequestOptions options = null)
- => InviteHelper.AcceptAsync(this, Discord, options);
-
public override string ToString() => Url;
private string DebuggerDisplay => $"{Url} ({GuildName} / {ChannelName})";
diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.cs b/src/Discord.Net.WebSocket/BaseSocketClient.cs
index 923b2c23b..fb82fe14a 100644
--- a/src/Discord.Net.WebSocket/BaseSocketClient.cs
+++ b/src/Discord.Net.WebSocket/BaseSocketClient.cs
@@ -55,8 +55,8 @@ namespace Discord.WebSocket
public Task> GetConnectionsAsync(RequestOptions options = null)
=> ClientHelper.GetConnectionsAsync(this, options ?? RequestOptions.Default);
///
- public Task GetInviteAsync(string inviteId, RequestOptions options = null)
- => ClientHelper.GetInviteAsync(this, inviteId, options ?? RequestOptions.Default);
+ public Task GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null)
+ => ClientHelper.GetInviteAsync(this, inviteId, withCount, options ?? RequestOptions.Default);
// IDiscordClient
async Task IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
@@ -70,8 +70,8 @@ namespace Discord.WebSocket
async Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> await GetConnectionsAsync(options).ConfigureAwait(false);
- async Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
- => await GetInviteAsync(inviteId, options).ConfigureAwait(false);
+ async Task IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
+ => await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetGuild(id));
diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs
index ad89067df..45d76e61a 100644
--- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs
+++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs
@@ -327,8 +327,8 @@ namespace Discord.WebSocket
async Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> await GetConnectionsAsync().ConfigureAwait(false);
- async Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
- => await GetInviteAsync(inviteId).ConfigureAwait(false);
+ async Task IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
+ => await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetGuild(id));
diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs
index 593f796c2..0188f7e9f 100644
--- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs
+++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs
@@ -1796,8 +1796,8 @@ namespace Discord.WebSocket
async Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> await GetConnectionsAsync().ConfigureAwait(false);
- async Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
- => await GetInviteAsync(inviteId).ConfigureAwait(false);
+ async Task IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
+ => await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetGuild(id));