add note list to hs command
This commit is contained in:
parent
2213436998
commit
f98be52f7c
1 changed files with 96 additions and 20 deletions
|
@ -3,43 +3,119 @@ use crate::data::helpstart_api::ListType::*;
|
||||||
use crate::data::mojang::name;
|
use crate::data::mojang::name;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::Context;
|
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 poise::CreateReply;
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
#[poise::command(
|
#[poise::command(
|
||||||
slash_command,
|
slash_command,
|
||||||
install_context = "Guild|User",
|
install_context = "Guild|User",
|
||||||
interaction_context = "Guild|BotDm",
|
interaction_context = "Guild|BotDm|PrivateChannel",
|
||||||
ephemeral = "true"
|
ephemeral = "false"
|
||||||
)]
|
)]
|
||||||
// Check for bots available to you.
|
// Check for bots available to you.
|
||||||
pub(crate) async fn helpstart(ctx: Context<'_>) -> Result<(), Error> {
|
pub(crate) async fn helpstart(
|
||||||
|
ctx: Context<'_>,
|
||||||
|
user: Option<String>,
|
||||||
|
ephemeral: Option<bool>,
|
||||||
|
) -> 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 links = super::accountv2::get_link(ctx.author(), &ctx.data().sqlite_pool).await?;
|
||||||
let mc_accounts = links
|
let mc_accounts = match user {
|
||||||
.minecraft_accounts
|
None => {
|
||||||
.into_iter()
|
futures::future::try_join_all(
|
||||||
.map(|a| name(&ctx.data().caches, &ctx.data().clients.general, a.uuid))
|
links
|
||||||
.collect::<Vec<_>>();
|
.minecraft_accounts
|
||||||
let mc_accounts = futures::future::try_join_all(mc_accounts).await?;
|
.into_iter()
|
||||||
|
.map(|a| name(&ctx.data().caches, &ctx.data().clients.general, a.uuid))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Some(name) => vec![name],
|
||||||
|
};
|
||||||
|
|
||||||
let bots = fetch_all(&ctx.data().clients.local_api_client).await?;
|
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::<Vec<_>>();
|
||||||
|
|
||||||
|
let s: String = usable
|
||||||
|
.iter()
|
||||||
|
.map(|b| b.username().as_str())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
|
||||||
|
let ready: String = usable
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|b| {
|
.filter_map(|b| {
|
||||||
if match b.list_type() {
|
if !*b.in_party()
|
||||||
Whitelist => b.list().iter().any(|w| mc_accounts.contains(w)),
|
&& *b.last_updated()
|
||||||
Blacklist => mc_accounts.iter().any(|m| !b.list().contains(m)),
|
< (SystemTime::now().duration_since(UNIX_EPOCH).ok()? - Duration::from_secs(5))
|
||||||
} {
|
.as_secs_f64()
|
||||||
|
{
|
||||||
Some(b.username().as_str())
|
Some(b.username().as_str())
|
||||||
} else {
|
} else {
|
||||||
|
println!(
|
||||||
|
"{}, {}",
|
||||||
|
*b.last_updated(),
|
||||||
|
(SystemTime::now().duration_since(UNIX_EPOCH).ok()? - Duration::from_secs(5))
|
||||||
|
.as_secs_f64()
|
||||||
|
);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>()
|
||||||
let s: String = usable.join(", ");
|
.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!(
|
let reply = CreateReply::default()
|
||||||
"Accounts you can use: {s}\nTotal registered bots: {}",
|
.content(format!(
|
||||||
bots.len()
|
"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?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue