This commit is contained in:
2026-03-12 01:03:44 +01:00
parent 2f6f8414d0
commit 8ed01f2049
15 changed files with 180 additions and 15 deletions
+1
View File
@@ -1,2 +1,3 @@
pub mod ping;
pub mod play;
pub mod testnats;
+65
View File
@@ -0,0 +1,65 @@
use std::path::PathBuf;
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, JobResponse, Jobs, PlayJob, SearchJob},
misc::{new_uuid_v4, parse_url_or_default},
};
use url::Url;
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()
{
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(),
)
.await
{
Ok(resp) => resp,
Err(_why) => return Err(serenity::Error::Other("send error")),
};
}
}
Ok(())
}
pub fn register() -> CreateCommand {
CreateCommand::new("play")
.description("Play a song")
.add_option(
CreateCommandOption::new(
CommandOptionType::String,
"song",
"Name or url of the song to play",
)
.required(false),
)
}
+7 -5
View File
@@ -15,8 +15,9 @@ use serenity::{
};
use types::{
jobs::{DownloadJob, JobResponse, Jobs, PlayJob, SearchJob},
misc::new_uuid_v4,
misc::{new_uuid_v4, parse_url_or_default},
};
use url::Url;
pub async fn run(
ctx: &Context,
@@ -40,8 +41,9 @@ pub async fn run(
)
.await?;
let url: String;
let url: Url;
let is_url = value.starts_with("https://");
if !is_url {
let search_job = SearchJob {
uuid: new_uuid_v4(),
@@ -64,7 +66,7 @@ pub async fn run(
.await?;
return Err(serenity::Error::Other("Search error"));
} else if let Some(Jobs::Search(content)) = search_response.content {
url = content.url;
url = content.song.url;
} else {
interaction
.edit_response(ctx, EditInteractionResponse::new().content("unknown error"))
@@ -72,7 +74,7 @@ pub async fn run(
return Err(serenity::Error::Other("unknown error"));
}
} else {
url = value.to_string();
url = parse_url_or_default(value.to_string());
}
let download_job = DownloadJob {
@@ -154,7 +156,7 @@ pub async fn run(
}
pub fn register() -> CreateCommand {
CreateCommand::new("play")
CreateCommand::new("testnats")
.description("Play a song")
.add_option(
CreateCommandOption::new(
+17 -2
View File
@@ -2,6 +2,7 @@ mod commands;
use std::env;
use async_nats::Client;
use serenity::{
Client,
all::{Context, EventHandler, GatewayIntents},
@@ -25,12 +26,18 @@ impl EventHandler for Handler {
let content = match command.data.name.as_str() {
"play" => {
commands::testnats::run(&ctx, &command, &self.nats_client)
commands::play::run(&ctx, &command, &self.nats_client)
.await
.unwrap();
None
}
"ping" => Some(commands::ping::run(&command.data.options())),
"testnats" => {
commands::testnats::run(&ctx, &command, &self.nats_client)
.await
.unwrap();
None
}
_ => Some("not implemented :(".to_string()),
};
@@ -49,7 +56,11 @@ impl EventHandler for Handler {
if let Err(why) = Command::set_global_commands(
&ctx.http,
vec![commands::ping::register(), commands::testnats::register()],
vec![
commands::ping::register(),
commands::play::register(),
commands::testnats::register(),
],
)
.await
{
@@ -58,6 +69,10 @@ impl EventHandler for Handler {
}
}
impl A for Client {
}
#[tokio::main]
async fn main() {
// Configure the client with your Discord bot token in the environment.