pub struct RestBuilder { /* private fields */ }Expand description
Configures and builds a Rest client.
Defaults match @discordjs/rest: API https://discord.com/api, version 10, 15 s timeout,
3 retries, 50 requests per second globally, 50 ms rate-limit offset.
discord.js counterpart: RESTOptions.
Implementations§
Source§impl RestBuilder
impl RestBuilder
Sourcepub fn token(self, token: impl Into<String>) -> Self
pub fn token(self, token: impl Into<String>) -> Self
Sets the bot token (without the Bot prefix).
Without a token, only unauthenticated endpoints (webhooks with tokens, interaction
callbacks) can be used. Replaces a bearer_token.
discord.js counterpart: REST#setToken (@discordjs/rest).
Sourcepub fn bearer_token(self, token: impl Into<String>) -> Self
pub fn bearer_token(self, token: impl Into<String>) -> Self
Authenticates with an OAuth2 access token (Authorization: Bearer …) instead of a
bot token (D-108), to act for the user who authorized the application — e.g. for
Edit Application Command Permissions.
Replaces a token.
Discord limits requests per token, so use one client per access token: its rate-limit state is its own.
discord.js counterpart: RESTOptions#authPrefix set to 'Bearer'.
§Examples
use djs_rest::Rest;
let user_rest = Rest::builder().bearer_token("an-oauth2-access-token").build();
assert!(!format!("{user_rest:?}").contains("an-oauth2-access-token"));Sourcepub fn cdn(self, cdn: impl Into<String>) -> Self
pub fn cdn(self, cdn: impl Into<String>) -> Self
Sets the CDN base URL of Rest::cdn (default https://cdn.discordapp.com, D-137).
discord.js counterpart: the cdn option of RESTOptions (@discordjs/rest).
Sourcepub fn media_proxy(self, media_proxy: impl Into<String>) -> Self
pub fn media_proxy(self, media_proxy: impl Into<String>) -> Self
Sets the media proxy base URL of Rest::cdn (default https://media.discordapp.net).
discord.js counterpart: the mediaProxy option of RESTOptions (@discordjs/rest).
Sourcepub fn api(self, api: impl Into<String>) -> Self
pub fn api(self, api: impl Into<String>) -> Self
Sets the API base URL, without the version (default https://discord.com/api).
Useful to route requests through a proxy. Plain http:// URLs are allowed for that
purpose.
discord.js counterpart: the api option of RESTOptions (@discordjs/rest).
Sourcepub const fn version(self, version: u8) -> Self
pub const fn version(self, version: u8) -> Self
Sets the API version (default 10).
discord.js counterpart: the version option of RESTOptions (@discordjs/rest).
Sourcepub const fn timeout(self, timeout: Duration) -> Self
pub const fn timeout(self, timeout: Duration) -> Self
Sets how long to wait for a complete response before giving up (default 15 s).
discord.js counterpart: the timeout option of RESTOptions (@discordjs/rest).
Sourcepub const fn retries(self, retries: u8) -> Self
pub const fn retries(self, retries: u8) -> Self
Sets how many times a request is retried after a server error, transport error or timeout (default 3). Rate-limited requests are always retried and do not count.
discord.js counterpart: the retries option of RESTOptions (@discordjs/rest).
Sourcepub const fn global_requests_per_second(self, limit: u32) -> Self
pub const fn global_requests_per_second(self, limit: u32) -> Self
Sets the maximum number of requests per second across all routes (default 50).
The limit is strict: no sliding second ever contains more requests (the window is
extended by the offset), while a burst of up to limit requests is sent
at once. Unlike discord.js’ fixed one-second windows, this never lets twice the limit
through around a window boundary.
0 disables the proactive global limit; global rate-limit responses from Discord are
still honoured.
Sourcepub const fn offset(self, offset: Duration) -> Self
pub const fn offset(self, offset: Duration) -> Self
Sets the extra delay added to every rate-limit wait to absorb clock skew and latency (default 50 ms).
discord.js counterpart: the offset option of RESTOptions (@discordjs/rest).
Sourcepub const fn invalid_request_limit(self, limit: u32) -> Self
pub const fn invalid_request_limit(self, limit: u32) -> Self
Makes requests fail immediately with ErrorKind::InvalidRequestLimit once the client
made limit invalid requests (401, 403, 429) in the last 10 minutes (default 0: no
limit). Discord temporarily bans the IP at 10 000, so a limit below that (for example
9 000) stops a misbehaving bot before it is locked out (D-085).
The count is shared by every clone of the client; see Rest::invalid_requests.
discord.js counterpart: none (@discordjs/rest only warns, with invalidRequestWarningInterval).
Sourcepub fn user_agent_appendix(self, appendix: impl Into<String>) -> Self
pub fn user_agent_appendix(self, appendix: impl Into<String>) -> Self
Appends text to the User-Agent header, e.g. your bot’s name.
discord.js counterpart: the userAgentAppendix option of RESTOptions (@discordjs/rest).
Sourcepub fn transport(self, transport: impl Transport) -> Self
pub fn transport(self, transport: impl Transport) -> Self
Sets what sends the requests (D-404): needed where the default hyper transport is
not available (without the hyper feature, for example on Cloudflare Workers).
With a custom transport, the TLS options of this crate (native-roots) do not apply.
See Transport for an example.
discord.js counterpart: the makeRequest option of RESTOptions (@discordjs/rest).