pub struct ClusterClient<B: Backend = DashMapBackend> { /* private fields */ }Expand description
A cluster process’s view of its cluster: its assignment, the commands it handles and the
requests it sends to the other clusters (D-301, D-307). Returned by
Client::cluster for a client built with
ClientBuilder::cluster.
Cheap to clone (a Client clone). Requests go through the manager, so they need the
client to be logged in; handlers can be registered at any time.
discord.js counterpart: ShardClientUtil (client.shard), and discord-hybrid-sharding’s
ClusterClient.
§Examples
use djs_rs::{
prelude::*,
sharding::{self, ClusterCommand},
};
/// How many guilds a cluster has in its cache.
struct GuildCount;
impl ClusterCommand for GuildCount {
const NAME: &'static str = "guild_count";
type Input = ();
type Output = usize;
}
let assignment = sharding::process()?.expect("started by a ClusterManager");
let client = Client::builder(token)
.intents(Intents::GUILDS | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT)
.cluster(assignment)
.build();
let cluster = client.cluster().expect("built with `cluster`");
cluster.handle::<GuildCount, _, _>(|client, ()| async move {
Ok(client.cache().map_or(0, |cache| cache.guilds().len()))
});
client.on(Events::MessageCreate, move |ctx, msg| {
let cluster = cluster.clone();
async move {
if msg.content == "!guilds" {
let total: usize = cluster
.broadcast::<GuildCount>(())
.await?
.into_iter()
.filter_map(|reply| reply.result.ok())
.sum();
msg.reply(&ctx, format!("{total} guilds")).await?;
}
Ok(())
}
});
client.login().await?;Implementations§
Source§impl<B: Backend> ClusterClient<B>
impl<B: Backend> ClusterClient<B>
Sourcepub fn id(&self) -> u32
pub fn id(&self) -> u32
This process’s cluster ID.
discord.js counterpart: ShardClientUtil#ids (the shards there).
Sourcepub fn shards(&self) -> Range<u32> ⓘ
pub fn shards(&self) -> Range<u32> ⓘ
The shards this process runs.
discord.js counterpart: ShardClientUtil#ids.
Sourcepub fn total_shards(&self) -> u32
pub fn total_shards(&self) -> u32
The bot’s shard count, over every process.
discord.js counterpart: ShardClientUtil#count.
Sourcepub fn handle<C, F, Fut>(&self, handler: F) -> &Self
pub fn handle<C, F, Fut>(&self, handler: F) -> &Self
Handles the command C in this cluster (D-308): handler gets a Client clone and
the sender’s input, and returns the output or an error (sent back as text, a
ClusterError::CommandFailed). Replaces an earlier handler of the same command.
Each request runs in its own task, so a slow handler holds up nothing else; a
handler that panics is reported as failed.
discord.js counterpart: the function given to broadcastEval, which runs in every
shard.
Sourcepub async fn broadcast<C: ClusterCommand>(
&self,
input: C::Input,
) -> Result<Vec<ClusterReply<C::Output>>, ClusterError>
pub async fn broadcast<C: ClusterCommand>( &self, input: C::Input, ) -> Result<Vec<ClusterReply<C::Output>>, ClusterError>
Sends the command C to every cluster, this one included, and returns each one’s
result by cluster ID (D-309); see ClusterManager::broadcast.
discord.js counterpart: ShardClientUtil#broadcastEval.
§Errors
ClusterError::NotConnected if this process is not connected to its manager (the
client is not logged in), ClusterError::Encoding if input cannot be converted
to JSON.
Sourcepub async fn send<C: ClusterCommand>(
&self,
cluster: u32,
input: C::Input,
) -> Result<C::Output, ClusterError>
pub async fn send<C: ClusterCommand>( &self, cluster: u32, input: C::Input, ) -> Result<C::Output, ClusterError>
Sends the command C to cluster cluster (this one too) and returns its result.
discord.js counterpart: ShardClientUtil#broadcastEval with the shard option.
§Errors
As ClusterManager::send, and
ClusterError::NotConnected if this process is not connected to its manager.
Sourcepub async fn stats(
&self,
) -> Result<Vec<ClusterReply<ClusterStats>>, ClusterError>
pub async fn stats( &self, ) -> Result<Vec<ClusterReply<ClusterStats>>, ClusterError>
Asks every cluster, this one included, for its ClusterStats (D-310): the built-in
Stats command, broadcast.
discord.js counterpart: ShardClientUtil#fetchClientValues.
§Errors
ClusterError::NotConnected if this process is not connected to its manager.
§Examples
let guilds: u64 = cluster
.stats()
.await?
.into_iter()
.filter_map(|reply| reply.result.ok()?.guilds)
.sum();
println!("{guilds} guilds over every cluster");Trait Implementations§
Source§impl<B: Backend> Clone for ClusterClient<B>
impl<B: Backend> Clone for ClusterClient<B>
Auto Trait Implementations§
impl<B = DashMapBackend> !RefUnwindSafe for ClusterClient<B>
impl<B = DashMapBackend> !UnwindSafe for ClusterClient<B>
impl<B> Freeze for ClusterClient<B>
impl<B> Send for ClusterClient<B>
impl<B> Sync for ClusterClient<B>
impl<B> Unpin for ClusterClient<B>
impl<B> UnsafeUnpin for ClusterClient<B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Classify for T
impl<T> Classify for T
type Classified = T
fn classify(self) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Declassify for T
impl<T> Declassify for T
type Declassified = T
fn declassify(self) -> T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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