Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add Get***Url methods #1906

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 70 additions & 22 deletions src/Discord.Net.Core/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@ public static class CDN
/// </summary>
/// <param name="teamId">The team identifier.</param>
/// <param name="iconId">The icon identifier.</param>
/// <param name="size">The size of the image to return in horizontal pixels. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the team's icon.
/// </returns>
public static string GetTeamIconUrl(ulong teamId, string iconId)
=> iconId != null ? $"{DiscordConfig.CDNUrl}team-icons/{teamId}/{iconId}.jpg" : null;

public static string GetTeamIconUrl(ulong teamId, string iconId, ushort size, ImageFormat format)
{
if (iconId == null)
return null;
string extension = FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}team-icons/{teamId}/{iconId}.{extension}?size={size}";
}
/// <summary>
/// Returns an application icon URL.
/// </summary>
/// <param name="appId">The application identifier.</param>
/// <param name="iconId">The icon identifier.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the application's icon.
/// </returns>
public static string GetApplicationIconUrl(ulong appId, string iconId)
=> iconId != null ? $"{DiscordConfig.CDNUrl}app-icons/{appId}/{iconId}.jpg" : null;

public static string GetApplicationIconUrl(ulong appId, string iconId, ushort size, ImageFormat format)
{
if (iconId == null)
return null;
string extension = FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}app-icons/{appId}/{iconId}.{extension}?size={size}";
}
/// <summary>
/// Returns a user avatar URL.
/// </summary>
Expand Down Expand Up @@ -62,67 +74,101 @@ public static string GetDefaultUserAvatarUrl(ushort discriminator)
/// </summary>
/// <param name="guildId">The guild snowflake identifier.</param>
/// <param name="iconId">The icon identifier.</param>
/// <param name="size">The size of the image to return in horizontal pixels. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the guild's icon.
/// </returns>
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}";
}
/// <summary>
/// Returns a guild splash URL.
/// </summary>
/// <param name="guildId">The guild snowflake identifier.</param>
/// <param name="splashId">The splash icon identifier.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the guild's splash.
/// </returns>
public static string GetGuildSplashUrl(ulong guildId, string splashId)
=> splashId != null ? $"{DiscordConfig.CDNUrl}splashes/{guildId}/{splashId}.jpg" : null;
public static string GetGuildSplashUrl(ulong guildId, string splashId, ushort size, ImageFormat format)
{
if (splashId == null)
return null;
string extension = FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}splashes/{guildId}/{splashId}.{extension}?size={size}";
}
/// <summary>
/// Returns a guild discovery splash URL.
/// </summary>
/// <param name="guildId">The guild snowflake identifier.</param>
/// <param name="discoverySplashId">The discovery splash icon identifier.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the guild's discovery splash.
/// </returns>
public static string GetGuildDiscoverySplashUrl(ulong guildId, string discoverySplashId)
=> discoverySplashId != null ? $"{DiscordConfig.CDNUrl}discovery-splashes/{guildId}/{discoverySplashId}.jpg" : null;
public static string GetGuildDiscoverySplashUrl(ulong guildId, string discoverySplashId, ushort size, ImageFormat format)
{
if (discoverySplashId == null)
return null;
string extension = FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}discovery-splashes/{guildId}/{discoverySplashId}.{extension}?size={size}";
}
/// <summary>
/// Returns a channel icon URL.
/// </summary>
/// <param name="channelId">The channel snowflake identifier.</param>
/// <param name="iconId">The icon identifier.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the channel's icon.
/// </returns>
public static string GetChannelIconUrl(ulong channelId, string iconId)
=> iconId != null ? $"{DiscordConfig.CDNUrl}channel-icons/{channelId}/{iconId}.jpg" : null;

public static string GetChannelIconUrl(ulong channelId, string iconId, ushort size, ImageFormat format)
{
if (iconId == null)
return null;
string extension = FormatToExtension(format, iconId);
return $"{DiscordConfig.CDNUrl}channel-icons/{channelId}/{iconId}.{extension}?size={size}";
}
/// <summary>
/// Returns a guild banner URL.
/// </summary>
/// <param name="guildId">The guild snowflake identifier.</param>
/// <param name="bannerId">The banner image identifier.</param>
/// <param name="size">The size of the image to return in horizontal pixels. This can be any power of two between 16 and 2048 inclusive.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the guild's banner image.
/// </returns>
public static string GetGuildBannerUrl(ulong guildId, string bannerId, ushort? size = null)
public static string GetGuildBannerUrl(ulong guildId, string bannerId, ushort? size = null, ImageFormat? format = null)
{
if (!string.IsNullOrEmpty(bannerId))
return $"{DiscordConfig.CDNUrl}banners/{guildId}/{bannerId}.jpg" + (size.HasValue ? $"?size={size}" : string.Empty);
return null;
if (string.IsNullOrEmpty(bannerId))
return null;
string extension = format.HasValue ? FormatToExtension(format.Value, string.Empty) : "png";
return $"{DiscordConfig.CDNUrl}banners/{guildId}/{bannerId}.{extension}" + (size.HasValue ? $"?size={size}" : string.Empty);
}
/// <summary>
/// Returns an emoji URL.
/// </summary>
/// <param name="emojiId">The emoji snowflake identifier.</param>
/// <param name="animated">Whether this emoji is animated.</param>
/// <param name="size">The size of the image to return in horizontal pixels. This can be any power of two between 16 and 128.</param>
/// <param name="format">The format to return.</param>
/// <returns>
/// A URL pointing to the custom emote.
/// </returns>
public static string GetEmojiUrl(ulong emojiId, bool animated)
=> $"{DiscordConfig.CDNUrl}emojis/{emojiId}.{(animated ? "gif" : "png")}";
public static string GetEmojiUrl(ulong emojiId, bool animated, ushort size, ImageFormat format)
{
string extension = format == ImageFormat.Auto && animated ? "gif" : FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}emojis/{emojiId}.{extension}?size={size}";
}

/// <summary>
/// Returns a Rich Presence asset URL.
Expand All @@ -136,7 +182,7 @@ public static string GetEmojiUrl(ulong emojiId, bool animated)
/// </returns>
public static string GetRichAssetUrl(ulong appId, string assetId, ushort size, ImageFormat format)
{
string extension = FormatToExtension(format, "");
string extension = FormatToExtension(format, string.Empty);
return $"{DiscordConfig.CDNUrl}app-assets/{appId}/{assetId}.{extension}?size={size}";
}

Expand Down Expand Up @@ -173,6 +219,8 @@ private static string FormatToExtension(ImageFormat format, string imageId)
return "png";
case ImageFormat.WebP:
return "webp";
case ImageFormat.Lottie:
return "json";
default:
throw new ArgumentException(nameof(format));
}
Expand Down
12 changes: 11 additions & 1 deletion src/Discord.Net.Core/Entities/Emotes/Emote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class Emote : IEmote, ISnowflakeEntity
/// <returns>
/// A string that points to the URL of this emote.
/// </returns>
public string Url => CDN.GetEmojiUrl(Id, Animated);
[Obsolete("This property is obsolete. Call GetUrl instead.")]
public string Url => CDN.GetEmojiUrl(Id, Animated, 128, ImageFormat.Auto);

internal Emote(ulong id, string name, bool animated)
{
Expand All @@ -38,6 +39,15 @@ internal Emote(ulong id, string name, bool animated)
Animated = animated;
}

/// <summary>
/// Get the URL for this Emote.
/// </summary>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 128 inclusive.</param>
/// <returns>A URL pointing to the custom emote.</returns>
public string GetUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetEmojiUrl(Id, Animated, size, format);

/// <summary>
/// Determines whether the specified emote is equal to the current emote.
/// </summary>
Expand Down
48 changes: 48 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,54 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// </returns>
CultureInfo PreferredCulture { get; }

/// <summary>
/// Get the icon URL for this Guild.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Guild's icon. In event that the guild does not have a valid icon
/// (i.e. their icon identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the guild's icon URL; <c>null</c> if the guild does not have an icon in place.</returns>
string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);

/// <summary>
/// Get the splash URL for this Guild.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Guild's splash. In event that the guild does not have a valid splash
/// (i.e. their splash identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the guild's splash URL; <c>null</c> if the guild does not have an splash in place.</returns>
string GetSplashUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);

/// <summary>
/// Get the discovery splash URL for this Guild.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Guild's discovery splash. In event that the guild does not have a valid discovery splash
/// (i.e. their discovery splash identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the guild's discovery splash URL; <c>null</c> if the guild does not have a discovery splash in place.</returns>
string GetDiscoverySplashUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);

/// <summary>
/// Get the banner URL for this Guild.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Guild's banner. In event that the guild does not have a valid banner
/// (i.e. their banner identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the guild's banner URL; <c>null</c> if the guild does not have a banner in place.</returns>
string GetBannerUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);

/// <summary>
/// Modifies this guild.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IUserGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@ public interface IUserGuild : IDeletable, ISnowflakeEntity
/// Returns the current user's permissions for this guild.
/// </summary>
GuildPermissions Permissions { get; }

/// <summary>
/// Get the icon URL for this UserGuild.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this UserGuild's icon. In event that the user guild does not have a valid icon
/// (i.e. their icon identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the user guild's icon URL; <c>null</c> if the user guild does not have an icon in place.</returns>
string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
}
}
14 changes: 13 additions & 1 deletion src/Discord.Net.Core/Entities/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IApplication : ISnowflakeEntity
/// <summary>
/// Gets the icon URL of the application.
/// </summary>
string IconUrl { get; }
string IconUrl { get; }
/// <summary>
/// Gets if the bot is public.
/// </summary>
Expand All @@ -39,5 +39,17 @@ public interface IApplication : ISnowflakeEntity
/// Gets the partial user object containing info on the owner of the application.
/// </summary>
IUser Owner { get; }

/// <summary>
/// Get the icon URL for this Application.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Application's icon. In event that the application does not have a valid icon
/// (i.e. their icon identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the application's icon URL; <c>null</c> if the application does not have an icon in place.</returns>
string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
}
}
4 changes: 4 additions & 0 deletions src/Discord.Net.Core/Entities/ImageFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public enum ImageFormat
/// Use GIF.
/// </summary>
Gif,
/// <summary>
/// Use Lottie.
/// </summary>
Lottie,
}
}
11 changes: 11 additions & 0 deletions src/Discord.Net.Core/Entities/Teams/ITeam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,16 @@ public interface ITeam
/// Gets the user identifier that owns this team.
/// </summary>
ulong OwnerUserId { get; }
/// <summary>
/// Get the icon URL for this Team.
/// </summary>
/// <remarks>
/// This property retrieves a URL for this Team's icon. In event that the team does not have a valid icon
/// (i.e. their icon identifier is not set), this property will return <c>null</c>.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048 inclusive.</param>
/// <returns>A string representing the team's icon URL; <c>null</c> if the team does not have an icon in place.</returns>
string GetIconUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
}
}
Loading