reply
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::str::from_utf8;
|
||||||
|
|
||||||
use postcard::to_stdvec;
|
use postcard::to_stdvec;
|
||||||
use serenity::{
|
use serenity::{
|
||||||
all::{
|
all::{
|
||||||
@@ -6,6 +8,7 @@ use serenity::{
|
|||||||
CreateCommandOption,
|
CreateCommandOption,
|
||||||
CreateInteractionResponse,
|
CreateInteractionResponse,
|
||||||
CreateInteractionResponseMessage,
|
CreateInteractionResponseMessage,
|
||||||
|
EditInteractionResponse,
|
||||||
},
|
},
|
||||||
builder::CreateCommand,
|
builder::CreateCommand,
|
||||||
model::application::{CommandOptionType, ResolvedOption, ResolvedValue},
|
model::application::{CommandOptionType, ResolvedOption, ResolvedValue},
|
||||||
@@ -27,7 +30,17 @@ pub async fn run(
|
|||||||
..
|
..
|
||||||
}) = options.first()
|
}) = options.first()
|
||||||
{
|
{
|
||||||
let job = &Job {
|
interaction
|
||||||
|
.create_response(
|
||||||
|
ctx,
|
||||||
|
CreateInteractionResponse::Message(
|
||||||
|
CreateInteractionResponseMessage::new()
|
||||||
|
.content(format!("Searching: {value}...")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let job = Job {
|
||||||
uuid: new_uuid_v4(),
|
uuid: new_uuid_v4(),
|
||||||
kind: JobKind::Download,
|
kind: JobKind::Download,
|
||||||
inner: InnerStruct {
|
inner: InnerStruct {
|
||||||
@@ -37,19 +50,21 @@ pub async fn run(
|
|||||||
|
|
||||||
println!("job {:?}", job);
|
println!("job {:?}", job);
|
||||||
|
|
||||||
if let Err(_why) = nats_client
|
let response = match nats_client
|
||||||
.publish("corro-dj.download", to_stdvec(job).unwrap().into())
|
.request("corro-dj.download", to_stdvec(&job).unwrap().into())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
return Err(serenity::Error::Other("send error"));
|
Ok(resp) => resp,
|
||||||
}
|
Err(_why) => return Err(serenity::Error::Other("send error")),
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("response: {:?}", from_utf8(&response.payload).unwrap());
|
||||||
|
|
||||||
interaction
|
interaction
|
||||||
.create_response(
|
.edit_response(
|
||||||
ctx,
|
ctx,
|
||||||
CreateInteractionResponse::Message(
|
EditInteractionResponse::new()
|
||||||
CreateInteractionResponseMessage::new().content(format!("Sent: {value}")),
|
.content(format!("path: {}", from_utf8(&response.payload).unwrap())),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+27
-10
@@ -1,5 +1,3 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use postcard::from_bytes;
|
use postcard::from_bytes;
|
||||||
use types::jobs::Job;
|
use types::jobs::Job;
|
||||||
@@ -11,12 +9,14 @@ async fn main() {
|
|||||||
let nats_client = async_nats::connect("nats://localhost:4222")
|
let nats_client = async_nats::connect("nats://localhost:4222")
|
||||||
.await
|
.await
|
||||||
.expect("Error creating nats client");
|
.expect("Error creating nats client");
|
||||||
|
|
||||||
let mut subscriber = nats_client
|
let mut subscriber = nats_client
|
||||||
.queue_subscribe("corro-dj.*", "download".to_string())
|
.queue_subscribe("corro-dj.*", "download".to_string())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let libraries = Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap());
|
let libraries = Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap());
|
||||||
|
|
||||||
let downloader = Downloader::builder(libraries, "output")
|
let downloader = Downloader::builder(libraries, "output")
|
||||||
.build()
|
.build()
|
||||||
.await
|
.await
|
||||||
@@ -25,23 +25,40 @@ async fn main() {
|
|||||||
// Receive and process messages
|
// Receive and process messages
|
||||||
while let Some(message) = subscriber.next().await {
|
while let Some(message) = subscriber.next().await {
|
||||||
println!("Received message {:?}", message);
|
println!("Received message {:?}", message);
|
||||||
let result: Job = from_bytes(message.payload.deref()).unwrap();
|
let result: Job = from_bytes(&message.payload).unwrap();
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
|
|
||||||
let video = downloader
|
let video = match downloader.fetch_video_infos(result.inner.str).await {
|
||||||
.fetch_video_infos(result.inner.str)
|
Ok(video) => video,
|
||||||
.await
|
Err(why) => {
|
||||||
.unwrap();
|
println!("{:?}", why);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let audio_path = downloader
|
let audio_path = match downloader
|
||||||
.download_audio_stream_with_quality(
|
.download_audio_stream_with_quality(
|
||||||
&video,
|
&video,
|
||||||
format!("{}.opus", video.id),
|
format!("{}.opus", video.id),
|
||||||
yt_dlp::model::AudioQuality::Best,
|
yt_dlp::model::AudioQuality::Best,
|
||||||
yt_dlp::model::AudioCodecPreference::Opus,
|
yt_dlp::model::AudioCodecPreference::Opus,
|
||||||
)
|
)
|
||||||
.await;
|
.await
|
||||||
|
{
|
||||||
|
Ok(path) => path,
|
||||||
|
Err(why) => {
|
||||||
|
println!("{:?}", why);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
println!("{:?}", audio_path);
|
println!("reply: {:?}", audio_path);
|
||||||
|
|
||||||
|
if let Some(reply) = message.reply {
|
||||||
|
nats_client
|
||||||
|
.publish(reply, format!("{:?}", audio_path).into())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user