queue wip
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use nats_libs::job::JobClient;
|
use nats_libs::{job::JobClient, kv::KVClient};
|
||||||
use serenity::{
|
use serenity::{
|
||||||
all::{CommandInteraction, Context, CreateCommandOption},
|
all::{CommandInteraction, Context, CreateCommandOption},
|
||||||
builder::CreateCommand,
|
builder::CreateCommand,
|
||||||
@@ -9,6 +9,7 @@ use types::{
|
|||||||
error::{CorroError, CorroErrorType},
|
error::{CorroError, CorroErrorType},
|
||||||
jobs::{DownloadJob, JobsMap, JobsResponseMap, PlayJob, SearchJob},
|
jobs::{DownloadJob, JobsMap, JobsResponseMap, PlayJob, SearchJob},
|
||||||
misc::{new_uuid_v4, parse_url_or_default},
|
misc::{new_uuid_v4, parse_url_or_default},
|
||||||
|
queue::{Queue, YoutubeSong},
|
||||||
};
|
};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
@@ -26,6 +27,31 @@ pub async fn run(
|
|||||||
..
|
..
|
||||||
}) = options.first()
|
}) = options.first()
|
||||||
{
|
{
|
||||||
|
let guild_id = interaction.guild_id.unwrap();
|
||||||
|
|
||||||
|
let mut queue = match services.jetstream_kv.get_queue(guild_id.into()).await {
|
||||||
|
Ok(queue) => queue,
|
||||||
|
Err(_why) => {
|
||||||
|
let queue = Queue {
|
||||||
|
guild_id: guild_id.into(),
|
||||||
|
uuid: new_uuid_v4(),
|
||||||
|
songs: vec![],
|
||||||
|
};
|
||||||
|
match services
|
||||||
|
.jetstream_kv
|
||||||
|
.set_queue(guild_id.into(), &queue)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_sequence) => queue,
|
||||||
|
Err(why) => {
|
||||||
|
return Err(why);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{:?}", queue);
|
||||||
|
|
||||||
match interaction
|
match interaction
|
||||||
.create_text_response(ctx, format!("Searching: {value}..."))
|
.create_text_response(ctx, format!("Searching: {value}..."))
|
||||||
.await
|
.await
|
||||||
@@ -83,7 +109,19 @@ pub async fn run(
|
|||||||
Err(why) => return Err(why),
|
Err(why) => return Err(why),
|
||||||
};
|
};
|
||||||
|
|
||||||
let guild_id = interaction.guild_id.unwrap();
|
queue.songs.push(YoutubeSong {
|
||||||
|
title: download_response.test.clone(),
|
||||||
|
thumbnail_url: Url::parse("https://example.com").unwrap(),
|
||||||
|
url: Url::parse("https://example.com").unwrap(),
|
||||||
|
artist: download_response.test,
|
||||||
|
});
|
||||||
|
|
||||||
|
match services.jetstream_kv.set_queue(guild_id.into(), &queue).await {
|
||||||
|
Ok(_sequence) => (),
|
||||||
|
Err(why) => {
|
||||||
|
return Err(why);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let channel_id = guild_id
|
let channel_id = guild_id
|
||||||
.get_user_voice_state(&ctx.http, interaction.user.id)
|
.get_user_voice_state(&ctx.http, interaction.user.id)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ struct Handler {
|
|||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||||
if let Interaction::Command(command) = interaction {
|
if let Interaction::Command(command) = interaction {
|
||||||
println!("Received command interaction: {command:?}");
|
// println!("Received command interaction: {command:?}");
|
||||||
|
|
||||||
if let Err(why) = match command.data.name.as_str() {
|
if let Err(why) = match command.data.name.as_str() {
|
||||||
"play" => queue_lock(&self.services, &ctx, &command, commands::play::run).await,
|
"play" => queue_lock(&self.services, &ctx, &command, commands::play::run).await,
|
||||||
@@ -78,7 +78,7 @@ async fn main() {
|
|||||||
|
|
||||||
let jetstream_kv = jetstream_client
|
let jetstream_kv = jetstream_client
|
||||||
.create_key_value(async_nats::jetstream::kv::Config {
|
.create_key_value(async_nats::jetstream::kv::Config {
|
||||||
bucket: "profiles".to_string(),
|
bucket: "queues".to_string(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
use std::num::NonZeroU64;
|
||||||
|
|
||||||
|
use async_nats::jetstream::kv::Store;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use postcard::{from_bytes, to_stdvec};
|
||||||
|
use types::{
|
||||||
|
error::{CorroError, CorroErrorType},
|
||||||
|
queue::Queue,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn get_key(id: NonZeroU64) -> String {
|
||||||
|
format!("corro-dj.{id}")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait KVClient {
|
||||||
|
async fn get_queue(&self, id: NonZeroU64) -> Result<Queue, CorroError>;
|
||||||
|
async fn set_queue(&self, id: NonZeroU64, queue: &Queue) -> Result<u64, CorroError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl KVClient for Store {
|
||||||
|
async fn get_queue(&self, id: NonZeroU64) -> Result<Queue, CorroError> {
|
||||||
|
match self.get(get_key(id)).await {
|
||||||
|
Ok(result) => {
|
||||||
|
if let Some(result) = result {
|
||||||
|
Ok(from_bytes(&result).unwrap())
|
||||||
|
} else {
|
||||||
|
Err(CorroError {
|
||||||
|
error_type: CorroErrorType::KVError,
|
||||||
|
message: format!("key {} is undefined", get_key(id)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(why) => Err(CorroError {
|
||||||
|
error_type: CorroErrorType::KVError,
|
||||||
|
message: why.to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_queue(&self, id: NonZeroU64, queue: &Queue) -> Result<u64, CorroError> {
|
||||||
|
match self
|
||||||
|
.put(get_key(id), to_stdvec(&queue).unwrap().into())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(sequence) => Ok(sequence),
|
||||||
|
Err(why) => Err(CorroError {
|
||||||
|
error_type: CorroErrorType::KVError,
|
||||||
|
message: why.to_string(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod job;
|
pub mod job;
|
||||||
|
pub mod kv;
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ pub enum CorroErrorType {
|
|||||||
JobError,
|
JobError,
|
||||||
ParseError,
|
ParseError,
|
||||||
NatsError,
|
NatsError,
|
||||||
|
KVError,
|
||||||
YtdlpError,
|
YtdlpError,
|
||||||
CommandError,
|
CommandError,
|
||||||
SerenityError,
|
SerenityError,
|
||||||
|
|||||||
Reference in New Issue
Block a user