From f98be52f7c2763a75b40bb5c4e377e5db48a07ca Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Tue, 18 Feb 2025 22:35:55 +0100 Subject: [PATCH] add note list to hs command --- src/commands/helpstart.rs | 116 +++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 20 deletions(-) diff --git a/src/commands/helpstart.rs b/src/commands/helpstart.rs index 983ff3b..4cdc7a5 100644 --- a/src/commands/helpstart.rs +++ b/src/commands/helpstart.rs @@ -3,43 +3,119 @@ use crate::data::helpstart_api::ListType::*; use crate::data::mojang::name; use crate::error::Error; use crate::Context; +use poise::serenity_prelude::ButtonStyle; +use poise::serenity_prelude::ComponentInteractionCollector; +use poise::serenity_prelude::CreateActionRow; +use poise::serenity_prelude::CreateButton; +use poise::serenity_prelude::CreateEmbed; +use poise::serenity_prelude::CreateInteractionResponse; +use poise::serenity_prelude::CreateInteractionResponseMessage; +use poise::serenity_prelude::EditMessage; use poise::CreateReply; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; #[poise::command( slash_command, install_context = "Guild|User", - interaction_context = "Guild|BotDm", - ephemeral = "true" + interaction_context = "Guild|BotDm|PrivateChannel", + ephemeral = "false" )] // Check for bots available to you. -pub(crate) async fn helpstart(ctx: Context<'_>) -> Result<(), Error> { +pub(crate) async fn helpstart( + ctx: Context<'_>, + user: Option, + ephemeral: Option, +) -> Result<(), Error> { + ctx.defer().await?; + let ephemeral = ephemeral.unwrap_or(true); let links = super::accountv2::get_link(ctx.author(), &ctx.data().sqlite_pool).await?; - let mc_accounts = links - .minecraft_accounts - .into_iter() - .map(|a| name(&ctx.data().caches, &ctx.data().clients.general, a.uuid)) - .collect::>(); - let mc_accounts = futures::future::try_join_all(mc_accounts).await?; + let mc_accounts = match user { + None => { + futures::future::try_join_all( + links + .minecraft_accounts + .into_iter() + .map(|a| name(&ctx.data().caches, &ctx.data().clients.general, a.uuid)) + .collect::>(), + ) + .await? + } + Some(name) => vec![name], + }; + let bots = fetch_all(&ctx.data().clients.local_api_client).await?; - let usable = bots + let usable = bots.iter().filter(|b| match b.list_type() { + Whitelist => b.list().iter().any(|w| mc_accounts.contains(w)), + Blacklist => mc_accounts.iter().any(|m| !b.list().contains(m)), + }).collect::>(); + + let s: String = usable + .iter() + .map(|b| b.username().as_str()) + .collect::>() + .join(", "); + + let ready: String = usable .iter() .filter_map(|b| { - if match b.list_type() { - Whitelist => b.list().iter().any(|w| mc_accounts.contains(w)), - Blacklist => mc_accounts.iter().any(|m| !b.list().contains(m)), - } { + if !*b.in_party() + && *b.last_updated() + < (SystemTime::now().duration_since(UNIX_EPOCH).ok()? - Duration::from_secs(5)) + .as_secs_f64() + { Some(b.username().as_str()) } else { + println!( + "{}, {}", + *b.last_updated(), + (SystemTime::now().duration_since(UNIX_EPOCH).ok()? - Duration::from_secs(5)) + .as_secs_f64() + ); None } }) - .collect::>(); - let s: String = usable.join(", "); + .collect::>() + .join(", "); + let bid = ctx.id(); + let components = vec![CreateActionRow::Buttons(vec![CreateButton::new( + bid.to_string(), + ) + .style(ButtonStyle::Primary) + .label("📜")])]; - let reply = CreateReply::default().content(format!( - "Accounts you can use: {s}\nTotal registered bots: {}", - bots.len() - )); + let reply = CreateReply::default() + .content(format!( + "Bots that are ready for use: {ready}\nBots you can use: {s}\nTotal registered bots: {}", + bots.len() + )) + .ephemeral(ephemeral) + .components(components); ctx.send(reply).await?; + + while let Some(mut i) = ComponentInteractionCollector::new(ctx) + .author_id(ctx.author().id) + .channel_id(ctx.channel_id()) + .timeout(std::time::Duration::from_secs(60)) + .filter(move |i| i.data.custom_id == bid.to_string()) + .await + { + let iter = bots + .iter() + .skip_while(|&b| b.note().trim().is_empty() || usable.iter().any(|&u| std::ptr::eq(u,b))) + .map(|b| (b.username(), b.note(), true)); + let embed = CreateEmbed::new().fields(iter).title("Notes").description( + "Below is the note of each bot that you cannot use. It might help you get whitelisted.", + ); + i.create_response( + ctx, + CreateInteractionResponse::Message( + CreateInteractionResponseMessage::new().embed(embed), + ), + ) + .await?; + i.message + .edit(ctx, EditMessage::default().components(vec![])) + .await?; + } Ok(()) }