Skip to main content

MessageSender

Struct MessageSender 

Source
pub struct MessageSender { /* private fields */ }
Expand description

Sends commands to a Shard from any task. Cheap to clone.

Commands are queued and written by the shard, in order, once its session is active.

§Send limit

Discord accepts 120 payloads per connection every 60 seconds and disconnects a client that sends more. The shard therefore sends at most 115 queued commands in any 61-second span (one second of margin for network jitter); the rest wait in the queue and go out as soon as the limit allows, so command never blocks or fails because of it. The remaining room is kept for heartbeats, identify and resume, which are never held back. A new connection starts with a fresh limit.

discord.js counterpart: WebSocketShard#send from @discordjs/ws, which paces payloads the same way.

Implementations§

Source§

impl MessageSender

Source

pub fn detached() -> Self

Returns a sender connected to no shard: every command fails with SendError::Closed.

For code that needs a sender where no shard runs, such as the context of an HTTP interaction in djs-rs (D-403).

discord.js counterpart: none.

§Examples
use djs_gateway::{MessageSender, SendError};
use djs_model::gateway::{Activity, Status, payload::UpdatePresence};

let presence = UpdatePresence::new(Status::Idle, [Activity::custom("Resting")]);
let result = MessageSender::detached().update_presence(&presence);
assert!(matches!(result, Err(SendError::Closed)));
Source

pub fn command<T: GatewayCommand>(&self, command: &T) -> Result<(), SendError>

Queues a command.

discord.js counterpart: WebSocketShard#send (@discordjs/ws).

§Errors

Returns SendError::Closed if the shard has been dropped, or SendError::Serialize if the command cannot be serialized.

Source

pub fn update_presence( &self, presence: &UpdatePresence, ) -> Result<(), SendError>

Updates the bot’s presence on this shard, and makes it the presence the shard identifies with from now on, so a new session keeps it (D-148).

Prefer this over command with an [UpdatePresence], which only sends the update: after a new session the shard would show its configured presence again.

discord.js counterpart: ClientUser#setPresence (which, unlike this method, is forgotten when a new session starts).

§Errors

Returns SendError::Closed if the shard has been dropped, or SendError::Serialize if the presence cannot be serialized.

§Examples
use djs_gateway::Shard;
use djs_model::gateway::{Activity, Intents, ShardId, Status, payload::UpdatePresence};

let shard = Shard::new(ShardId::ONE, "my-token", Intents::GUILDS);
shard
    .sender()
    .update_presence(&UpdatePresence::new(Status::Idle, [Activity::custom("Resting")]))
    .unwrap();
Source

pub fn is_closed(&self) -> bool

Returns true if the shard has been dropped.

discord.js counterpart: none (@discordjs/ws throws when sending on a destroyed shard).

Trait Implementations§

Source§

impl Clone for MessageSender

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MessageSender

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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