moover_rust/src/commands/voice/radio/radio_utils.rs

84 lines
2.8 KiB
Rust
Raw Normal View History

2025-01-24 20:52:13 +00:00
use poise::{ChoiceParameter, CreateReply};
use radiobrowser::StationSearchBuilder;
use serenity::{all::{CreateActionRow, CreateButton, CreateEmbed, CreateInteractionResponse, CreateInteractionResponseMessage}, futures::SinkExt};
use crate::types::Context;
#[derive(ChoiceParameter)]
pub enum WelcomeChoice {
#[name = "5"]
A,
#[name = "10"]
B,
#[name = "15"]
C
}
pub async fn paginate_search(
ctx: Context<'_>,
search_builder: StationSearchBuilder,
) -> Result<(), serenity::Error> {
// Define some unique identifiers for the navigation buttons
let ctx_id = ctx.id();
let prev_button_id = format!("{}prev", ctx_id);
let next_button_id = format!("{}next", ctx_id);
let Ok(stations) = search_builder.send().await else {
let _ = ctx.reply("Something went wrong, try searching again").await;
return Ok(())
};
// Send the embed with the first page as content
let reply = {
let components = CreateActionRow::Buttons(vec![
CreateButton::new(&prev_button_id).emoji('◀'),
CreateButton::new(&next_button_id).emoji('▶'),
]);
CreateReply::default()
.embed(CreateEmbed::new())
.components(vec![components])
};
ctx.send(reply).await?;
// Loop through incoming interactions with the navigation buttons
let mut current_page = 0;
while let Some(press) = serenity::collector::ComponentInteractionCollector::new(ctx)
// We defined our button IDs to start with `ctx_id`. If they don't, some other command's
// button was pressed
.filter(move |press| press.data.custom_id.starts_with(&ctx_id.to_string()))
// Timeout when no navigation button has been pressed for 24 hours
.timeout(std::time::Duration::from_secs(3600 * 24))
.await
{
// Depending on which button was pressed, go to next or previous page
if press.data.custom_id == next_button_id {
current_page += 1;
// TODO find a way to check end
// if current_page >= pages.len() {
// current_page = 0;
// }
} else if press.data.custom_id == prev_button_id {
// TODO find a way to get pages len
// current_page = current_page.checked_sub(1).unwrap_or(pages.len() - 1);
} else {
// This is an unrelated button interaction
continue;
}
// Update the message with the new page contents
press
.create_response(
ctx.serenity_context(),
CreateInteractionResponse::UpdateMessage(
CreateInteractionResponseMessage::new()
.embed(CreateEmbed::new()),
),
)
.await?;
}
Ok(())
}