Gateway: Twilight 0.15 support (#171)

This patch changes around quite a few things.
The main entrypoint for twilight besides process will now be the
TwilightMap which concists of command senders for each shard.

This simplifies parts of the code as there is not any difference
between shards and clusters anymore.
This commit is contained in:
Erk
2023-04-10 19:12:45 +02:00
committed by Kyle Simpson
parent 3f6114c53c
commit b2507f34f1
7 changed files with 109 additions and 67 deletions

View File

@@ -11,10 +11,10 @@ symphonia = { features = ["aac", "mp3", "isomp4", "alac"], version = "0.5.2" }
tracing = "0.1"
tracing-subscriber = "0.2"
tokio = { features = ["macros", "rt-multi-thread", "sync"], version = "1" }
twilight-gateway = "0.14"
twilight-http = "0.14"
twilight-model = "0.14"
twilight-standby = "0.14"
twilight-gateway = "0.15"
twilight-http = "0.15"
twilight-model = "0.15"
twilight-standby = "0.15"
[dependencies.songbird]
default-features = false

View File

@@ -23,12 +23,18 @@
use futures::StreamExt;
use songbird::{
input::{Compose, YoutubeDl},
shards::TwilightMap,
tracks::{PlayMode, TrackHandle},
Songbird,
};
use std::{collections::HashMap, env, error::Error, future::Future, num::NonZeroU64, sync::Arc};
use tokio::sync::RwLock;
use twilight_gateway::{Cluster, Event, Intents};
use twilight_gateway::{
stream::{self, ShardEventStream},
Event,
Intents,
Shard,
};
use twilight_http::Client as HttpClient;
use twilight_model::{
channel::Message,
@@ -62,21 +68,32 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Initialize the tracing subscriber.
tracing_subscriber::fmt::init();
let (mut events, state) = {
let (mut shards, state) = {
let token = env::var("DISCORD_TOKEN")?;
let http = HttpClient::new(token.clone());
let user_id = http.current_user().await?.model().await?.id;
let intents =
Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT | Intents::GUILD_VOICE_STATES;
let (cluster, events) = Cluster::new(token, intents).await?;
cluster.up().await;
Intents::GUILD_MESSAGES | Intents::GUILD_VOICE_STATES | Intents::MESSAGE_CONTENT;
let config = twilight_gateway::Config::new(token.clone(), intents);
let songbird = Songbird::twilight(Arc::new(cluster), user_id);
let shards: Vec<Shard> =
stream::create_recommended(&http, config, |_, builder| builder.build())
.await?
.collect();
let senders = TwilightMap::new(
shards
.iter()
.map(|s| (s.id().number(), s.sender()))
.collect(),
);
let songbird = Songbird::twilight(Arc::new(senders), user_id);
(
events,
shards,
Arc::new(StateRef {
http,
trackdata: Default::default(),
@@ -86,7 +103,22 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
)
};
while let Some((_, event)) = events.next().await {
let mut stream = ShardEventStream::new(shards.iter_mut());
loop {
let event = match stream.next().await {
Some((_, Ok(event))) => event,
Some((_, Err(source))) => {
tracing::warn!(?source, "error receiving event");
if source.is_fatal() {
break;
}
continue;
},
None => break,
};
state.standby.process(&event);
state.songbird.process(&event).await;