Skip to main content

Module cdn

Module cdn 

Source
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.
ImageOptions
The format and size of an image URL.

Enums§

ImageFormat
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.
ImageSize
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) % 6 for users on the new username system (discriminator 0), discriminator % 5 for the others.