[][src]Struct twilight_gateway::shard::Shard

pub struct Shard(_);

Implementations

impl Shard[src]

pub fn new(config: impl Into<ShardConfig>) -> Self[src]

Creates a new shard, which will automatically connect to the gateway.

Examples

Create a new shard, wait a second, and then print its current connection stage:

use twilight_gateway::Shard;
use std::{env, time::Duration};
use tokio::time as tokio_time;

let mut shard = Shard::new(env::var("DISCORD_TOKEN")?);
shard.start().await?;

tokio_time::delay_for(Duration::from_secs(1)).await;

let info = shard.info().await?;
println!("Shard stage: {}", info.stage());

pub fn config(&self) -> &ShardConfig[src]

Returns an immutable reference to the configuration used for this client.

pub async fn start<'_>(&'_ mut self) -> Result<()>[src]

Start the shard, connecting it to the gateway and starting the process of receiving and processing events.

Errors

Errors if the ShardProcessor could not be started.

pub async fn events<'_>(&'_ self) -> impl Stream<Item = Event>[src]

Creates a new stream of events from the shard.

There can be multiple streams of events. All events will be broadcast to all streams of events.

All event types except for EventType::SHARD_PAYLOAD are enabled. If you need to enable it, consider calling some_events instead.

pub async fn some_events<'_>(
    &'_ self,
    event_types: EventTypeFlags
) -> impl Stream<Item = Event>
[src]

Creates a new filtered stream of events from the shard.

Only the events specified in the bitflags will be sent over the stream.

Examples

Filter the events so that you only receive the Event::ShardConnected and Event::ShardDisconnected events:

use twilight_gateway::{EventTypeFlags, Event, Shard};
use futures::StreamExt;
use std::env;

let mut shard = Shard::new(env::var("DISCORD_TOKEN")?);
shard.start().await?;

let event_types = EventTypeFlags::SHARD_CONNECTED | EventTypeFlags::SHARD_DISCONNECTED;
let mut events = shard.some_events(event_types).await;

while let Some(event) = events.next().await {
    match event {
        Event::ShardConnected(_) => println!("Shard is now connected"),
        Event::ShardDisconnected(_) => println!("Shard is now disconnected"),
        // No other event will come in through the stream.
        _ => {},
    }
}

pub async fn info<'_>(&'_ self) -> Result<Information>[src]

Returns information about the running of the shard, such as the current connection stage.

Errors

Returns Error::Shutdown if the shard isn't actively running.

pub fn session(&self) -> Result<Arc<Session>>[src]

Returns a handle to the current session

Note

This session can be invalidated if it is kept around under a reconnect or resume. In consequence this call should not be cached.

Errors

Returns Error::Shutdown if the shard isn't actively running.

pub fn sink(&self) -> Result<ShardSink>[src]

Returns an interface implementing the Sink trait which can be used to send messages.

Note

This call should not be cached for too long as it will be invalidated by reconnects and resumes.

Errors

Returns Error::Shutdown if the shard isn't actively running.

pub async fn command<'_, '_>(&'_ self, com: &'_ impl Serialize) -> Result<()>[src]

Send a command over the gateway.

Errors

Fails if command could not be serialized, or if the command could not be sent.

Returns Error::Shutdown if the shard isn't actively running.

pub async fn shutdown<'_>(&'_ self)[src]

Shuts down the shard.

This will cleanly close the connection, causing discord to end the session and show the bot offline

pub async fn shutdown_resumable<'_>(&'_ self) -> (u64, Option<ResumeSession>)[src]

This will shut down the shard in a resumable way and return shard id and optional session info to resume with later if this shard is resumable

Trait Implementations

impl Clone for Shard[src]

impl Debug for Shard[src]

Auto Trait Implementations

impl !RefUnwindSafe for Shard

impl Send for Shard

impl Sync for Shard

impl Unpin for Shard

impl !UnwindSafe for Shard

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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