cleaner job send function
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use postcard::{from_bytes, to_stdvec};
|
||||
use nats::functions::JobClient;
|
||||
use serenity::{
|
||||
all::{
|
||||
CommandInteraction, Context, CreateCommandOption, CreateInteractionResponse,
|
||||
CreateInteractionResponseMessage, EditInteractionResponse,
|
||||
CommandInteraction,
|
||||
Context,
|
||||
CreateCommandOption,
|
||||
CreateInteractionResponse,
|
||||
CreateInteractionResponseMessage,
|
||||
EditInteractionResponse,
|
||||
},
|
||||
builder::CreateCommand,
|
||||
model::application::{CommandOptionType, ResolvedOption, ResolvedValue},
|
||||
};
|
||||
use types::{
|
||||
jobs::{DownloadJob, JobResponse, Jobs, PlayJob, SearchJob},
|
||||
jobs::{DownloadJob, JobsMap, JobsResponseMap, PlayJob, SearchJob},
|
||||
misc::{new_uuid_v4, parse_url_or_default},
|
||||
};
|
||||
use url::Url;
|
||||
@@ -27,26 +29,85 @@ pub async fn run(
|
||||
..
|
||||
}) = options.first()
|
||||
{
|
||||
interaction
|
||||
.create_response(
|
||||
ctx,
|
||||
CreateInteractionResponse::Message(
|
||||
CreateInteractionResponseMessage::new()
|
||||
.content(format!("Searching: {value}...")),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let url: Url;
|
||||
let is_url = value.starts_with("https://") || value.starts_with("http://");
|
||||
|
||||
if !is_url {
|
||||
let response = match nats_client
|
||||
.request(
|
||||
"corro-dj.search",
|
||||
to_stdvec(&SearchJob {
|
||||
uuid: new_uuid_v4(),
|
||||
query: value.to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
let search_response = match nats_client
|
||||
.send_job(JobsMap::Search(SearchJob {
|
||||
uuid: new_uuid_v4(),
|
||||
query: value.to_string(),
|
||||
}))
|
||||
.await
|
||||
{
|
||||
Ok(resp) => resp,
|
||||
Ok(resp) => match resp {
|
||||
JobsResponseMap::Search(resp) => resp,
|
||||
_ => return Err(serenity::Error::Other("Unexpected return type")),
|
||||
},
|
||||
Err(_why) => return Err(serenity::Error::Other("send error")),
|
||||
};
|
||||
|
||||
println!("{:?}", &search_response);
|
||||
|
||||
url = search_response.song.url;
|
||||
} else {
|
||||
url = parse_url_or_default(value.to_string());
|
||||
}
|
||||
|
||||
let download_response = match nats_client
|
||||
.send_job(JobsMap::Download(DownloadJob {
|
||||
uuid: new_uuid_v4(),
|
||||
url,
|
||||
}))
|
||||
.await
|
||||
{
|
||||
Ok(resp) => match resp {
|
||||
JobsResponseMap::Download(resp) => resp,
|
||||
_ => return Err(serenity::Error::Other("Unexpected return type")),
|
||||
},
|
||||
Err(_why) => return Err(serenity::Error::Other("send error")),
|
||||
};
|
||||
|
||||
println!("{:?}", &download_response);
|
||||
|
||||
let guild_id = interaction.guild_id.unwrap();
|
||||
|
||||
let channel_id = guild_id
|
||||
.get_user_voice_state(&ctx.http, interaction.user.id)
|
||||
.await
|
||||
.unwrap()
|
||||
.channel_id
|
||||
.unwrap();
|
||||
|
||||
let _ = match nats_client
|
||||
.send_job(JobsMap::Play(PlayJob {
|
||||
uuid: new_uuid_v4(),
|
||||
path: download_response.path,
|
||||
channel_id: channel_id.into(),
|
||||
guild_id: guild_id.into(),
|
||||
}))
|
||||
.await
|
||||
{
|
||||
Ok(resp) => match resp {
|
||||
JobsResponseMap::Play(resp) => resp,
|
||||
_ => return Err(serenity::Error::Other("Unexpected return type")),
|
||||
},
|
||||
Err(_why) => return Err(serenity::Error::Other("send error")),
|
||||
};
|
||||
|
||||
interaction
|
||||
.edit_response(ctx, EditInteractionResponse::new().content("Playing..."))
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -60,6 +121,6 @@ pub fn register() -> CreateCommand {
|
||||
"song",
|
||||
"Name or url of the song to play",
|
||||
)
|
||||
.required(false),
|
||||
.required(true),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use serenity::{
|
||||
model::application::{CommandOptionType, ResolvedOption, ResolvedValue},
|
||||
};
|
||||
use types::{
|
||||
jobs::{DownloadJob, JobResponse, Jobs, PlayJob, SearchJob},
|
||||
jobs::{DownloadJob, JobResponse, JobsResponseMap, PlayJob, SearchJob},
|
||||
misc::{new_uuid_v4, parse_url_or_default},
|
||||
};
|
||||
use url::Url;
|
||||
@@ -65,7 +65,7 @@ pub async fn run(
|
||||
.edit_response(ctx, EditInteractionResponse::new().content(error))
|
||||
.await?;
|
||||
return Err(serenity::Error::Other("Search error"));
|
||||
} else if let Some(Jobs::Search(content)) = search_response.content {
|
||||
} else if let Some(JobsResponseMap::Search(content)) = search_response.content {
|
||||
url = content.song.url;
|
||||
} else {
|
||||
interaction
|
||||
@@ -102,7 +102,7 @@ pub async fn run(
|
||||
let text_response: String;
|
||||
if let Some(error) = job_response.error {
|
||||
text_response = error;
|
||||
} else if let Some(Jobs::Download(content)) = job_response.content {
|
||||
} else if let Some(JobsResponseMap::Download(content)) = job_response.content {
|
||||
text_response = content.path.display().to_string();
|
||||
} else {
|
||||
text_response = "unkown".to_string();
|
||||
|
||||
@@ -2,7 +2,6 @@ mod commands;
|
||||
|
||||
use std::env;
|
||||
|
||||
use async_nats::Client;
|
||||
use serenity::{
|
||||
Client,
|
||||
all::{Context, EventHandler, GatewayIntents},
|
||||
@@ -69,10 +68,6 @@ impl EventHandler for Handler {
|
||||
}
|
||||
}
|
||||
|
||||
impl A for Client {
|
||||
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Configure the client with your Discord bot token in the environment.
|
||||
|
||||
Reference in New Issue
Block a user