Skip to main content

ClientBuilder

Struct ClientBuilder 

Source
pub struct ClientBuilder<B: Backend = DashMapBackend> { /* private fields */ }
Expand description

Configures and builds a Client. Created by Client::builder.

B is the cache backend (D-172), switched with cache_backend.

discord.js counterpart: ClientOptions.

Implementations§

Source§

impl<B: Backend> ClientBuilder<B>

Source

pub const fn intents(self, intents: Intents) -> Self

Sets the gateway intents — the groups of events to receive (default: none).

Only request what the bot needs. Privileged intents (Intents::PRIVILEGED) must be enabled in the Developer Portal first.

discord.js counterpart: the intents option of ClientOptions.

Source

pub fn rest(self, rest: Rest) -> Self

Uses a preconfigured REST client (for example with a proxy or custom timeouts) instead of one built from the token.

discord.js counterpart: the rest option of ClientOptions.

Source

pub const fn compression(self, compression: Compression) -> Self

Sets the gateway transport compression (default: zstd-stream when the feature is enabled).

discord.js counterpart: the ws.compression option of ClientOptions.

Source

pub fn gateway_url(self, url: impl Into<String>) -> Self

Connects to this gateway URL instead of the one from Get Gateway Bot (useful for proxies and tests).

Get Gateway Bot is still called for Shards::Auto; with a fixed shard count too, the client does not call it at all and shards identify one at a time (max_concurrency 1).

discord.js counterpart: none (discord.js always asks Get Gateway Bot).

Source

pub fn shards(self, shards: Shards) -> Self

Sets which shards to run (default Shards::Auto: as many as Discord recommends).

discord.js counterpart: the shards and shardCount options of ClientOptions.

§Panics

Panics on a shard count of 0, or a range that is empty or reaches past total.

§Examples
use djs_rs::{Client, Shards};

// Shards 0–7 of 16 run in this process; another process runs 8–15.
let client = Client::builder("my-token")
    .shards(Shards::Range { range: 0..8, total: 16 })
    .build();
Source

pub fn cluster(self, assignment: ClusterAssignment) -> Self

Makes this client the one of a cluster process started by a ClusterManager (D-300): it runs the assignment’s shards (Shards::Range), connects to the manager when it logs in, and shuts down when the manager asks or goes away. Get the assignment from sharding::process.

The client then does not call Get Gateway Bot: the gateway URL and max_concurrency come from the manager (D-305, a gateway_url set here still wins), and its shards wait for their IDENTIFY turns from the manager (D-299).

discord.js counterpart: what new Client() reads from the environment in a shard process (client.shard).

Source

pub fn presence(self, presence: UpdatePresence) -> Self

Sets the presence (status and activities) every shard starts with (default: none, the bot appears online without an activity). Change it later with Client::set_presence.

discord.js counterpart: the presence client option.

§Examples
use djs_rs::{
    Client,
    model::gateway::{Activity, Status, payload::UpdatePresence},
};

let client = Client::builder("my-token")
    .presence(UpdatePresence::new(Status::Online, [Activity::playing("with Rust")]))
    .build();
Source

pub const fn handler_mode(self, mode: HandlerMode) -> Self

Sets how handlers are executed (default HandlerMode::Concurrent).

discord.js counterpart: none (discord.js runs every listener as its event comes).

Source

pub fn cache(self, config: CacheConfig) -> Self

Gives the client a Cache configured by config (default: no cache, D-170).

The client applies every event to the cache before running its handlers (D-171), and runs the periodic sweeps of CacheConfig::sweepers while it is logged in. Read it with Client::cache and Context::cache. What the cache can fill depends on the intents: members need Intents::GUILD_MEMBERS, presences Intents::GUILD_PRESENCES, messages Intents::GUILD_MESSAGES or Intents::DIRECT_MESSAGES, and so on.

discord.js counterpart: the makeCache and sweepers client options. discord.js always caches; CacheConfig::discord_js caches what it caches by default.

§Examples
use djs_rs::{
    Client,
    cache::{CacheConfig, Limit, Resource},
    model::gateway::Intents,
};

let config = CacheConfig::builder()
    .members(Limit::Unlimited)
    .messages(Limit::Max(50))
    .build();
let client = Client::builder("my-token")
    .intents(Intents::GUILDS | Intents::GUILD_MEMBERS | Intents::GUILD_MESSAGES)
    .cache(config)
    .build();
let cache = client.cache().unwrap();
assert!(cache.config().is_enabled(Resource::Member));
assert!(!cache.config().is_enabled(Resource::Presence));
Source

pub fn cache_backend<C: Backend>(self, backend: C) -> ClientBuilder<C>

Keeps the cache in stores created by backend instead of the default DashMapBackend (D-172), and enables the cache: with CacheConfig::default unless cache is called too (in any order).

discord.js counterpart: the makeCache option of ClientOptions.

§Examples
use djs_rs::{Client, cache::DashMapBackend};

// Any `djs_cache::Backend` works; this is the default one, named explicitly.
let client: Client<DashMapBackend> = Client::builder("my-token")
    .cache_backend(DashMapBackend::new())
    .build();
assert!(client.cache().is_some());
Source

pub fn build(self) -> Client<B>

Builds the client. It does not connect until Client::login is called.

discord.js counterpart: new Client(options).

Trait Implementations§

Source§

impl<B: Debug + Backend> Debug for ClientBuilder<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B = DashMapBackend> !RefUnwindSafe for ClientBuilder<B>

§

impl<B = DashMapBackend> !UnwindSafe for ClientBuilder<B>

§

impl<B> Freeze for ClientBuilder<B>
where B: Freeze,

§

impl<B> Send for ClientBuilder<B>

§

impl<B> Sync for ClientBuilder<B>

§

impl<B> Unpin for ClientBuilder<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for ClientBuilder<B>
where B: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Classify for T

§

type Classified = T

§

fn classify(self) -> T

§

impl<T> Declassify for T

§

type Declassified = T

§

fn declassify(self) -> T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more