Expand description
CDN URLs of avatars, icons, banners, emojis, stickers and other images (D-137).
A Cdn knows the base URLs; Rest::cdn returns the client’s, and
Cdn::default works without a client. Every method returns the URL as a String; none
can fail, since formats and sizes are enums (D-139).
Without an explicit format, images are WebP, and animated ones
(hashes starting with a_) are animated WebP (.webp?animated=true, D-138).
discord.js counterpart: CDN of @discordjs/rest (REST#cdn).
§Examples
use djs_model::{id::UserId, util::ImageHash};
use djs_rest::cdn::{Cdn, ImageFormat, ImageOptions, ImageSize};
let cdn = Cdn::default();
let hash: ImageHash = "a_1269e74af4df7417b13759eae50c83dc".parse()?;
assert_eq!(
cdn.user_avatar(UserId::new(7), hash, ImageOptions::new().size(ImageSize::S256)),
"https://cdn.discordapp.com/avatars/7/a_1269e74af4df7417b13759eae50c83dc.webp?animated=true&size=256"
);
assert_eq!(
cdn.user_avatar(UserId::new(7), hash, ImageOptions::new().format(ImageFormat::Png)),
"https://cdn.discordapp.com/avatars/7/a_1269e74af4df7417b13759eae50c83dc.png"
);Structs§
- Cdn
- Builds CDN URLs (D-137). See the module documentation.
- Image
Options - The format and size of an image URL.
Enums§
- Image
Format - An image format (the file extension of the URL, D-139). Which formats an endpoint supports is listed on its method; GIF is only animated for animated hashes.
- Image
Size - The size of an image: a power of two from 16 to 4096 pixels (D-139).
Constants§
- DEFAULT_
CDN - The default CDN base URL.
- DEFAULT_
MEDIA_ PROXY - The default media proxy base URL (GIF stickers are served from there).
Functions§
- default_
avatar_ index - The index of the default avatar
of a user without one:
(user_id >> 22) % 6for users on the new username system (discriminator0),discriminator % 5for the others.