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
impl MessageSender
Sourcepub fn detached() -> Self
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)));Sourcepub fn command<T: GatewayCommand>(&self, command: &T) -> Result<(), SendError>
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.
Sourcepub fn update_presence(
&self,
presence: &UpdatePresence,
) -> Result<(), SendError>
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();