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
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Discord
|
namespace Discord
|
||||||
{
|
{
|
||||||
@@ -22,8 +22,9 @@ namespace Discord
|
|||||||
ulong GuildId { get; }
|
ulong GuildId { get; }
|
||||||
/// <summary> Gets the name of the guild this invite is linked to. </summary>
|
/// <summary> Gets the name of the guild this invite is linked to. </summary>
|
||||||
string GuildName { get; }
|
string GuildName { get; }
|
||||||
|
/// <summary> Gets the approximated count of online members in the guild. </summary>
|
||||||
/// <summary> Accepts this invite and joins the target guild. This will fail on bot accounts. </summary>
|
int? PresenceCount { get; }
|
||||||
Task AcceptAsync(RequestOptions options = null);
|
/// <summary> Gets the approximated count of total members in the guild. </summary>
|
||||||
|
int? MemberCount { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Discord
|
|||||||
Task<IReadOnlyCollection<IGuild>> GetGuildsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
|
Task<IReadOnlyCollection<IGuild>> GetGuildsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
|
||||||
Task<IGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null);
|
Task<IGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null);
|
||||||
|
|
||||||
Task<IInvite> GetInviteAsync(string inviteId, RequestOptions options = null);
|
Task<IInvite> GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null);
|
||||||
|
|
||||||
Task<IUser> GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
|
Task<IUser> GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
|
||||||
Task<IUser> GetUserAsync(string username, string discriminator, RequestOptions options = null);
|
Task<IUser> GetUserAsync(string username, string discriminator, RequestOptions options = null);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Discord.API
|
namespace Discord.API
|
||||||
@@ -11,5 +11,9 @@ namespace Discord.API
|
|||||||
public InviteGuild Guild { get; set; }
|
public InviteGuild Guild { get; set; }
|
||||||
[JsonProperty("channel")]
|
[JsonProperty("channel")]
|
||||||
public InviteChannel Channel { get; set; }
|
public InviteChannel Channel { get; set; }
|
||||||
|
[JsonProperty("approximate_presence_count")]
|
||||||
|
public Optional<int?> PresenceCount { get; set; }
|
||||||
|
[JsonProperty("approximate_member_count")]
|
||||||
|
public Optional<int?> MemberCount { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/Discord.Net.Rest/API/Rest/GetInviteParams.cs
Normal file
7
src/Discord.Net.Rest/API/Rest/GetInviteParams.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Discord.API.Rest
|
||||||
|
{
|
||||||
|
internal class GetInviteParams
|
||||||
|
{
|
||||||
|
public Optional<bool?> WithCounts { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -148,7 +148,7 @@ namespace Discord.Rest
|
|||||||
Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
||||||
=> Task.FromResult<IReadOnlyCollection<IConnection>>(ImmutableArray.Create<IConnection>());
|
=> Task.FromResult<IReadOnlyCollection<IConnection>>(ImmutableArray.Create<IConnection>());
|
||||||
|
|
||||||
Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
|
Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
|
||||||
=> Task.FromResult<IInvite>(null);
|
=> Task.FromResult<IInvite>(null);
|
||||||
|
|
||||||
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
|
|||||||
@@ -50,12 +50,16 @@ namespace Discord.Rest
|
|||||||
return models.Select(x => RestConnection.Create(x)).ToImmutableArray();
|
return models.Select(x => RestConnection.Create(x)).ToImmutableArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<RestInvite> GetInviteAsync(BaseDiscordClient client,
|
public static async Task<RestInviteMetadata> GetInviteAsync(BaseDiscordClient client,
|
||||||
string inviteId, RequestOptions options)
|
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)
|
if (model != null)
|
||||||
return RestInvite.Create(client, null, null, model);
|
return RestInviteMetadata.Create(client, null, null, model);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -897,7 +897,7 @@ namespace Discord.API
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Guild Invites
|
//Guild Invites
|
||||||
public async Task<Invite> GetInviteAsync(string inviteId, RequestOptions options = null)
|
public async Task<InviteMetadata> GetInviteAsync(string inviteId, GetInviteParams args, RequestOptions options = null)
|
||||||
{
|
{
|
||||||
Preconditions.NotNullOrEmpty(inviteId, nameof(inviteId));
|
Preconditions.NotNullOrEmpty(inviteId, nameof(inviteId));
|
||||||
options = RequestOptions.CreateOrClone(options);
|
options = RequestOptions.CreateOrClone(options);
|
||||||
@@ -910,9 +910,11 @@ namespace Discord.API
|
|||||||
if (index >= 0)
|
if (index >= 0)
|
||||||
inviteId = inviteId.Substring(index + 1);
|
inviteId = inviteId.Substring(index + 1);
|
||||||
|
|
||||||
|
var withCounts = args.WithCounts.GetValueOrDefault(false);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await SendAsync<Invite>("GET", () => $"invites/{inviteId}", new BucketIds(), options: options).ConfigureAwait(false);
|
return await SendAsync<InviteMetadata>("GET", () => $"invites/{inviteId}?with_counts={withCounts}", new BucketIds(), options: options).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; }
|
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; }
|
||||||
}
|
}
|
||||||
@@ -950,13 +952,6 @@ namespace Discord.API
|
|||||||
|
|
||||||
return await SendAsync<Invite>("DELETE", () => $"invites/{inviteId}", new BucketIds(), options: options).ConfigureAwait(false);
|
return await SendAsync<Invite>("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
|
//Guild Members
|
||||||
public async Task<GuildMember> GetGuildMemberAsync(ulong guildId, ulong userId, RequestOptions options = null)
|
public async Task<GuildMember> GetGuildMemberAsync(ulong guildId, ulong userId, RequestOptions options = null)
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ namespace Discord.Rest
|
|||||||
=> ClientHelper.GetConnectionsAsync(this, options);
|
=> ClientHelper.GetConnectionsAsync(this, options);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<RestInvite> GetInviteAsync(string inviteId, RequestOptions options = null)
|
public Task<RestInviteMetadata> GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null)
|
||||||
=> ClientHelper.GetInviteAsync(this, inviteId, options);
|
=> ClientHelper.GetInviteAsync(this, inviteId, withCount, options);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<RestGuild> GetGuildAsync(ulong id, RequestOptions options = null)
|
public Task<RestGuild> GetGuildAsync(ulong id, RequestOptions options = null)
|
||||||
@@ -131,8 +131,8 @@ namespace Discord.Rest
|
|||||||
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
||||||
=> await GetConnectionsAsync(options).ConfigureAwait(false);
|
=> await GetConnectionsAsync(options).ConfigureAwait(false);
|
||||||
|
|
||||||
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
|
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
|
||||||
=> await GetInviteAsync(inviteId, options).ConfigureAwait(false);
|
=> await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
|
||||||
|
|
||||||
async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Discord.Rest
|
namespace Discord.Rest
|
||||||
{
|
{
|
||||||
internal static class InviteHelper
|
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,
|
public static async Task DeleteAsync(IInvite invite, BaseDiscordClient client,
|
||||||
RequestOptions options)
|
RequestOptions options)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Discord.API.Rest;
|
||||||
using Model = Discord.API.Invite;
|
using Model = Discord.API.Invite;
|
||||||
|
|
||||||
namespace Discord.Rest
|
namespace Discord.Rest
|
||||||
@@ -10,6 +11,8 @@ namespace Discord.Rest
|
|||||||
{
|
{
|
||||||
public string ChannelName { get; private set; }
|
public string ChannelName { get; private set; }
|
||||||
public string GuildName { 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 ChannelId { get; private set; }
|
||||||
public ulong GuildId { get; private set; }
|
public ulong GuildId { get; private set; }
|
||||||
internal IChannel Channel { get; private set; }
|
internal IChannel Channel { get; private set; }
|
||||||
@@ -36,19 +39,21 @@ namespace Discord.Rest
|
|||||||
ChannelId = model.Channel.Id;
|
ChannelId = model.Channel.Id;
|
||||||
GuildName = model.Guild.Name;
|
GuildName = model.Guild.Name;
|
||||||
ChannelName = model.Channel.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)
|
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);
|
Update(model);
|
||||||
}
|
}
|
||||||
public Task DeleteAsync(RequestOptions options = null)
|
public Task DeleteAsync(RequestOptions options = null)
|
||||||
=> InviteHelper.DeleteAsync(this, Discord, options);
|
=> InviteHelper.DeleteAsync(this, Discord, options);
|
||||||
|
|
||||||
public Task AcceptAsync(RequestOptions options = null)
|
|
||||||
=> InviteHelper.AcceptAsync(this, Discord, options);
|
|
||||||
|
|
||||||
public override string ToString() => Url;
|
public override string ToString() => Url;
|
||||||
private string DebuggerDisplay => $"{Url} ({GuildName} / {ChannelName})";
|
private string DebuggerDisplay => $"{Url} ({GuildName} / {ChannelName})";
|
||||||
|
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ namespace Discord.WebSocket
|
|||||||
public Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(RequestOptions options = null)
|
public Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(RequestOptions options = null)
|
||||||
=> ClientHelper.GetConnectionsAsync(this, options ?? RequestOptions.Default);
|
=> ClientHelper.GetConnectionsAsync(this, options ?? RequestOptions.Default);
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<RestInvite> GetInviteAsync(string inviteId, RequestOptions options = null)
|
public Task<RestInviteMetadata> GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null)
|
||||||
=> ClientHelper.GetInviteAsync(this, inviteId, options ?? RequestOptions.Default);
|
=> ClientHelper.GetInviteAsync(this, inviteId, withCount, options ?? RequestOptions.Default);
|
||||||
|
|
||||||
// IDiscordClient
|
// IDiscordClient
|
||||||
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
|
async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
|
||||||
@@ -70,8 +70,8 @@ namespace Discord.WebSocket
|
|||||||
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
||||||
=> await GetConnectionsAsync(options).ConfigureAwait(false);
|
=> await GetConnectionsAsync(options).ConfigureAwait(false);
|
||||||
|
|
||||||
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
|
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
|
||||||
=> await GetInviteAsync(inviteId, options).ConfigureAwait(false);
|
=> await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
|
||||||
|
|
||||||
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IGuild>(GetGuild(id));
|
=> Task.FromResult<IGuild>(GetGuild(id));
|
||||||
|
|||||||
@@ -327,8 +327,8 @@ namespace Discord.WebSocket
|
|||||||
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
||||||
=> await GetConnectionsAsync().ConfigureAwait(false);
|
=> await GetConnectionsAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
|
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
|
||||||
=> await GetInviteAsync(inviteId).ConfigureAwait(false);
|
=> await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
|
||||||
|
|
||||||
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IGuild>(GetGuild(id));
|
=> Task.FromResult<IGuild>(GetGuild(id));
|
||||||
|
|||||||
@@ -1796,8 +1796,8 @@ namespace Discord.WebSocket
|
|||||||
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
|
||||||
=> await GetConnectionsAsync().ConfigureAwait(false);
|
=> await GetConnectionsAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
|
async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
|
||||||
=> await GetInviteAsync(inviteId).ConfigureAwait(false);
|
=> await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
|
||||||
|
|
||||||
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
|
||||||
=> Task.FromResult<IGuild>(GetGuild(id));
|
=> Task.FromResult<IGuild>(GetGuild(id));
|
||||||
|
|||||||
Reference in New Issue
Block a user