From d22f6955f947ea37f70056b85bf4cccb08aefb40 Mon Sep 17 00:00:00 2001 From: gab Date: Fri, 4 Jan 2019 19:07:17 +0100 Subject: [PATCH] Allow the user to define guild icon's format and size --- src/Discord.Net.Core/CDN.cs | 13 ++++++++++--- src/Discord.Net.Core/Entities/Guilds/IGuild.cs | 2 +- src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs | 3 ++- .../Entities/Guilds/SocketGuild.cs | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Discord.Net.Core/CDN.cs b/src/Discord.Net.Core/CDN.cs index 0fefc9a0d..92e8b1239 100644 --- a/src/Discord.Net.Core/CDN.cs +++ b/src/Discord.Net.Core/CDN.cs @@ -51,11 +51,18 @@ namespace Discord /// /// The guild snowflake identifier. /// The icon identifier. + /// The size of the image to return in. This can be any power of two between 16 and 2048. + /// The format to return. /// - /// A URL pointing to the guild's icon. + /// A URL pointing to the guild's icon in the specified size. /// - public static string GetGuildIconUrl(ulong guildId, string iconId) - => iconId != null ? $"{DiscordConfig.CDNUrl}icons/{guildId}/{iconId}.jpg" : null; + public static string GetGuildIconUrl(ulong guildId, string iconId, ushort size, ImageFormat format) + { + if (iconId == null) + return null; + string extension = FormatToExtension(format, iconId); + return $"{DiscordConfig.CDNUrl}icons/{guildId}/{iconId}.{extension}?size={size}"; + } /// /// Returns a guild splash URL. /// diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index 3b35796b9..826b4ff2f 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -72,7 +72,7 @@ namespace Discord /// /// A URL pointing to the guild's icon; null if none is set. /// - string IconUrl { get; } + string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128); /// /// Gets the ID of this guild's splash image. /// diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index a847998b5..1cf586a7c 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -59,7 +59,8 @@ namespace Discord.Rest [Obsolete("DefaultChannelId is deprecated, use GetDefaultChannelAsync")] public ulong DefaultChannelId => Id; /// - public string IconUrl => CDN.GetGuildIconUrl(Id, IconId); + public string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) + => CDN.GetGuildIconUrl(Id, IconId, size, format); /// public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId); diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index ca2db1a77..f22b4705c 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -97,7 +97,8 @@ namespace Discord.WebSocket /// public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); /// - public string IconUrl => CDN.GetGuildIconUrl(Id, IconId); + public string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) + => CDN.GetGuildIconUrl(Id, IconId, size, format); /// public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId); /// Indicates whether the client has all the members downloaded to the local guild cache.