95 lines
2.6 KiB
Rust
95 lines
2.6 KiB
Rust
use postcard::{from_bytes, to_stdvec};
|
|
use serenity::{
|
|
all::{
|
|
CommandInteraction,
|
|
Context,
|
|
CreateCommandOption,
|
|
CreateInteractionResponse,
|
|
CreateInteractionResponseMessage,
|
|
EditInteractionResponse,
|
|
},
|
|
builder::CreateCommand,
|
|
model::application::{CommandOptionType, ResolvedOption, ResolvedValue},
|
|
};
|
|
use types::{
|
|
jobs::{DownloadJob, DownloadResponse, JobResponse},
|
|
misc::new_uuid_v4,
|
|
};
|
|
|
|
pub async fn run(
|
|
ctx: &Context,
|
|
interaction: &CommandInteraction,
|
|
nats_client: &async_nats::Client,
|
|
) -> Result<(), serenity::Error> {
|
|
let options = interaction.data.options();
|
|
|
|
if let Some(ResolvedOption {
|
|
value: ResolvedValue::String(value),
|
|
..
|
|
}) = options.first()
|
|
{
|
|
interaction
|
|
.create_response(
|
|
ctx,
|
|
CreateInteractionResponse::Message(
|
|
CreateInteractionResponseMessage::new()
|
|
.content(format!("Searching: {value}...")),
|
|
),
|
|
)
|
|
.await?;
|
|
|
|
let job = DownloadJob {
|
|
uuid: new_uuid_v4(),
|
|
url: value.to_string(),
|
|
};
|
|
|
|
println!("job {:?}", job);
|
|
|
|
let response = match nats_client
|
|
.request("corro-dj.download", to_stdvec(&job).unwrap().into())
|
|
.await
|
|
{
|
|
Ok(resp) => resp,
|
|
Err(_why) => return Err(serenity::Error::Other("send error")),
|
|
};
|
|
|
|
let job_response: JobResponse<DownloadResponse> = from_bytes(&response.payload).unwrap();
|
|
|
|
println!("response: {:?}", job_response);
|
|
|
|
let text_response: String;
|
|
|
|
if let Some(error) = job_response.error {
|
|
text_response = error;
|
|
} else if let Some(content) = job_response.content {
|
|
text_response = content.path.display().to_string();
|
|
} else {
|
|
text_response = "unkown".to_string();
|
|
}
|
|
|
|
interaction
|
|
.edit_response(ctx, EditInteractionResponse::new().content(text_response))
|
|
.await?;
|
|
} else {
|
|
interaction
|
|
.create_response(
|
|
ctx,
|
|
CreateInteractionResponse::Message(
|
|
CreateInteractionResponseMessage::new()
|
|
.content("Please provide a valid string"),
|
|
),
|
|
)
|
|
.await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn register() -> CreateCommand {
|
|
CreateCommand::new("testnats")
|
|
.description("test nats")
|
|
.add_option(
|
|
CreateCommandOption::new(CommandOptionType::String, "str", "random string")
|
|
.required(false),
|
|
)
|
|
}
|