You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CDN.cs 6.5 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. namespace Discord
  3. {
  4. /// <summary>
  5. /// Represents a class containing the strings related to various Content Delivery Networks (CDNs).
  6. /// </summary>
  7. public static class CDN
  8. {
  9. /// <summary>
  10. /// Returns an application icon URL.
  11. /// </summary>
  12. /// <param name="appId">The application identifier.</param>
  13. /// <param name="iconId">The icon identifier.</param>
  14. /// <returns>
  15. /// A URL pointing to the application's icon.
  16. /// </returns>
  17. public static string GetApplicationIconUrl(ulong appId, string iconId)
  18. => iconId != null ? $"{DiscordConfig.CDNUrl}app-icons/{appId}/{iconId}.jpg" : null;
  19. /// <summary>
  20. /// Returns a user avatar URL.
  21. /// </summary>
  22. /// <param name="userId">The user snowflake identifier.</param>
  23. /// <param name="avatarId">The avatar identifier.</param>
  24. /// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048.</param>
  25. /// <param name="format">The format to return.</param>
  26. /// <returns>
  27. /// A URL pointing to the user's avatar in the specified size.
  28. /// </returns>
  29. public static string GetUserAvatarUrl(ulong userId, string avatarId, ushort size, ImageFormat format)
  30. {
  31. if (avatarId == null)
  32. return null;
  33. string extension = FormatToExtension(format, avatarId);
  34. return $"{DiscordConfig.CDNUrl}avatars/{userId}/{avatarId}.{extension}?size={size}";
  35. }
  36. /// <summary>
  37. /// Returns the default user avatar URL.
  38. /// </summary>
  39. /// <param name="discriminator">The discriminator value of a user.</param>
  40. /// <returns>
  41. /// A URL pointing to the user's default avatar when one isn't set.
  42. /// </returns>
  43. public static string GetDefaultUserAvatarUrl(ushort discriminator)
  44. {
  45. return $"{DiscordConfig.CDNUrl}embed/avatars/{discriminator % 5}.png";
  46. }
  47. /// <summary>
  48. /// Returns an icon URL.
  49. /// </summary>
  50. /// <param name="guildId">The guild snowflake identifier.</param>
  51. /// <param name="iconId">The icon identifier.</param>
  52. /// <returns>
  53. /// A URL pointing to the guild's icon.
  54. /// </returns>
  55. public static string GetGuildIconUrl(ulong guildId, string iconId)
  56. => iconId != null ? $"{DiscordConfig.CDNUrl}icons/{guildId}/{iconId}.jpg" : null;
  57. /// <summary>
  58. /// Returns a guild splash URL.
  59. /// </summary>
  60. /// <param name="guildId">The guild snowflake identifier.</param>
  61. /// <param name="splashId">The splash icon identifier.</param>
  62. /// <returns>
  63. /// A URL pointing to the guild's icon.
  64. /// </returns>
  65. public static string GetGuildSplashUrl(ulong guildId, string splashId)
  66. => splashId != null ? $"{DiscordConfig.CDNUrl}splashes/{guildId}/{splashId}.jpg" : null;
  67. /// <summary>
  68. /// Returns a channel icon URL.
  69. /// </summary>
  70. /// <param name="channelId">The channel snowflake identifier.</param>
  71. /// <param name="iconId">The icon identifier.</param>
  72. /// <returns>
  73. /// A URL pointing to the channel's icon.
  74. /// </returns>
  75. public static string GetChannelIconUrl(ulong channelId, string iconId)
  76. => iconId != null ? $"{DiscordConfig.CDNUrl}channel-icons/{channelId}/{iconId}.jpg" : null;
  77. /// <summary>
  78. /// Returns an emoji URL.
  79. /// </summary>
  80. /// <param name="emojiId">The emoji snowflake identifier.</param>
  81. /// <param name="animated">Whether this emoji is animated.</param>
  82. /// <returns>
  83. /// A URL pointing to the custom emote.
  84. /// </returns>
  85. public static string GetEmojiUrl(ulong emojiId, bool animated)
  86. => $"{DiscordConfig.CDNUrl}emojis/{emojiId}.{(animated ? "gif" : "png")}";
  87. /// <summary>
  88. /// Returns a Rich Presence asset URL.
  89. /// </summary>
  90. /// <param name="appId">The application identifier.</param>
  91. /// <param name="assetId">The asset identifier.</param>
  92. /// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048.</param>
  93. /// <param name="format">The format to return.</param>
  94. /// <returns>
  95. /// A URL pointing to the asset image in the specified size.
  96. /// </returns>
  97. public static string GetRichAssetUrl(ulong appId, string assetId, ushort size, ImageFormat format)
  98. {
  99. string extension = FormatToExtension(format, "");
  100. return $"{DiscordConfig.CDNUrl}app-assets/{appId}/{assetId}.{extension}?size={size}";
  101. }
  102. /// <summary>
  103. /// Returns a Spotify album URL.
  104. /// </summary>
  105. /// <param name="albumArtId">The identifier for the album art (e.g. 6be8f4c8614ecf4f1dd3ebba8d8692d8ce4951ac).</param>
  106. /// <returns>
  107. /// A URL pointing to the Spotify album art.
  108. /// </returns>
  109. public static string GetSpotifyAlbumArtUrl(string albumArtId)
  110. => $"https://i.scdn.co/image/{albumArtId}";
  111. /// <summary>
  112. /// Returns a Spotify direct URL for a track.
  113. /// </summary>
  114. /// <param name="trackId">The identifier for the track (e.g. 4uLU6hMCjMI75M1A2tKUQC).</param>
  115. /// <returns>
  116. /// A URL pointing to the Spotify track.
  117. /// </returns>
  118. public static string GetSpotifyDirectUrl(string trackId)
  119. => $"https://open.spotify.com/track/{trackId}";
  120. private static string FormatToExtension(ImageFormat format, string imageId)
  121. {
  122. if (format == ImageFormat.Auto)
  123. format = imageId.StartsWith("a_") ? ImageFormat.Gif : ImageFormat.Png;
  124. switch (format)
  125. {
  126. case ImageFormat.Gif:
  127. return "gif";
  128. case ImageFormat.Jpeg:
  129. return "jpeg";
  130. case ImageFormat.Png:
  131. return "png";
  132. case ImageFormat.WebP:
  133. return "webp";
  134. default:
  135. throw new ArgumentException(nameof(format));
  136. }
  137. }
  138. }
  139. }