Replace ulong? user id of GuildEmote with Optional<IUser>. Adds a util function in EntityExtensions for getting the emote author, which requires the IGuild. Updates the GuildEmote ToEntity extension to require an IGuild as a parameter. Updates all instances of ToEntity to supply this IGuild parameter.pull/1214/head
@@ -31,19 +31,19 @@ namespace Discord | |||||
/// </returns> | /// </returns> | ||||
public IReadOnlyList<ulong> RoleIds { get; } | public IReadOnlyList<ulong> RoleIds { get; } | ||||
/// <summary> | /// <summary> | ||||
/// Gets the user Id that created this emoji. | |||||
/// Gets the User that created this emoji. | |||||
/// </summary> | /// </summary> | ||||
/// <returns> | /// <returns> | ||||
/// A user Id of the user who created this emoji, which may be null. A null value only indicates that the creator was not supplied as part of the API response. | |||||
/// </returns> | |||||
public ulong? CreatorId { get; } | |||||
/// An optional <see cref="IUser"/> who created this emoji. An unspecified value only indicates that the creator was not supplied as part of the API response. | |||||
/// </returns> | |||||
public Optional<IUser> Creator { get; } | |||||
internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds, ulong? userId) : base(id, name, animated) | |||||
internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds, Optional<IUser> creator) : base(id, name, animated) | |||||
{ | { | ||||
IsManaged = isManaged; | IsManaged = isManaged; | ||||
RequireColons = requireColons; | RequireColons = requireColons; | ||||
RoleIds = roleIds; | RoleIds = roleIds; | ||||
CreatorId = userId; | |||||
Creator = creator; | |||||
} | } | ||||
private string DebuggerDisplay => $"{Name} ({Id})"; | private string DebuggerDisplay => $"{Name} ({Id})"; | ||||
@@ -393,7 +393,7 @@ namespace Discord.Rest | |||||
public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | ||||
{ | { | ||||
var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options).ConfigureAwait(false); | var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options).ConfigureAwait(false); | ||||
return emote.ToEntity(); | |||||
return emote.ToEntity(guild); | |||||
} | } | ||||
public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles, | public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles, | ||||
RequestOptions options) | RequestOptions options) | ||||
@@ -407,7 +407,7 @@ namespace Discord.Rest | |||||
apiargs.RoleIds = roles.Value?.Select(xr => xr.Id).ToArray(); | apiargs.RoleIds = roles.Value?.Select(xr => xr.Id).ToArray(); | ||||
var emote = await client.ApiClient.CreateGuildEmoteAsync(guild.Id, apiargs, options).ConfigureAwait(false); | var emote = await client.ApiClient.CreateGuildEmoteAsync(guild.Id, apiargs, options).ConfigureAwait(false); | ||||
return emote.ToEntity(); | |||||
return emote.ToEntity(guild); | |||||
} | } | ||||
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception> | /// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception> | ||||
public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func, | public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func, | ||||
@@ -426,7 +426,7 @@ namespace Discord.Rest | |||||
apiargs.RoleIds = props.Roles.Value?.Select(xr => xr.Id).ToArray(); | apiargs.RoleIds = props.Roles.Value?.Select(xr => xr.Id).ToArray(); | ||||
var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options).ConfigureAwait(false); | var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options).ConfigureAwait(false); | ||||
return emote.ToEntity(); | |||||
return emote.ToEntity(guild); | |||||
} | } | ||||
public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options) | ||||
=> client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options); | => client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options); | ||||
@@ -109,7 +109,7 @@ namespace Discord.Rest | |||||
{ | { | ||||
var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | ||||
for (int i = 0; i < model.Emojis.Length; i++) | for (int i = 0; i < model.Emojis.Length; i++) | ||||
emotes.Add(model.Emojis[i].ToEntity()); | |||||
emotes.Add(model.Emojis[i].ToEntity(this)); | |||||
_emotes = emotes.ToImmutableArray(); | _emotes = emotes.ToImmutableArray(); | ||||
} | } | ||||
else | else | ||||
@@ -5,9 +5,16 @@ namespace Discord.Rest | |||||
{ | { | ||||
internal static class EntityExtensions | internal static class EntityExtensions | ||||
{ | { | ||||
public static GuildEmote ToEntity(this API.Emoji model) | |||||
public static GuildEmote ToEntity(this API.Emoji model, IGuild guild) | |||||
{ | { | ||||
return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), model.User.IsSpecified ? model.User.Value.Id : (ulong?)null); | |||||
return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), GetEmoteAuthor(model, guild)); | |||||
} | |||||
internal static Optional<IUser> GetEmoteAuthor(API.Emoji model, IGuild guild) | |||||
{ | |||||
if (!model.User.IsSpecified || guild == null) | |||||
return new Optional<IUser>(); | |||||
return new Optional<IUser>(guild.GetUserAsync(model.User.Value.Id).Result); | |||||
} | } | ||||
public static Embed ToEntity(this API.Embed model) | public static Embed ToEntity(this API.Embed model) | ||||
@@ -359,7 +359,7 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
var emojis = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | var emojis = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | ||||
for (int i = 0; i < model.Emojis.Length; i++) | for (int i = 0; i < model.Emojis.Length; i++) | ||||
emojis.Add(model.Emojis[i].ToEntity()); | |||||
emojis.Add(model.Emojis[i].ToEntity(this)); | |||||
_emotes = emojis.ToImmutable(); | _emotes = emojis.ToImmutable(); | ||||
} | } | ||||
else | else | ||||
@@ -409,7 +409,7 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length); | ||||
for (int i = 0; i < model.Emojis.Length; i++) | for (int i = 0; i < model.Emojis.Length; i++) | ||||
emotes.Add(model.Emojis[i].ToEntity()); | |||||
emotes.Add(model.Emojis[i].ToEntity(this)); | |||||
_emotes = emotes.ToImmutable(); | _emotes = emotes.ToImmutable(); | ||||
} | } | ||||