mod workers; use std::env; use futures::StreamExt; use postcard::{from_bytes, to_stdvec}; use types::jobs::JobResponse; use which::which; use yt_dlp::{Downloader, client::Libraries}; #[tokio::main] async fn main() { let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let mut client = Client::builder(&token, intents) .event_handler(Handler) .framework(framework) .register_songbird() // We insert our own HTTP client here to make use of in // `~play`. If we wanted, we could supply cookies and auth // details ahead of time. // // Generally, we don't want to make a new Client for every request! .type_map_insert::(HttpClient::new()) .await .expect("Err creating client"); let nats_client = async_nats::connect("nats://localhost:4222") .await .expect("Error creating nats client"); let mut subscriber = nats_client .queue_subscribe("corro-dj.*", "group1".to_string()) .await .unwrap(); let yt_downloader = Downloader::builder( Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap()), "output", ) .build() .await .unwrap(); // Receive and process messages while let Some(message) = subscriber.next().await { println!("Received message {:?}", message); let subject = message.subject.split(".").collect::>()[1]; let result = match subject { "download" => { workers::download::download(&yt_downloader, from_bytes(&message.payload).unwrap()) .await } _ => Err(format!("subject {subject} does not exists")), }; let response = match result { Ok(response) => response, Err(err) => JobResponse { content: None, error: Some(err), }, }; if let Some(reply) = message.reply { nats_client .publish(reply, to_stdvec(&response).unwrap().into()) .await .unwrap(); } } }