From 98ecde21b80463b16875b49619d86e11158238af Mon Sep 17 00:00:00 2001 From: TOwInOK <60252419+TOwInOK@users.noreply.github.com> Date: Sat, 14 Sep 2024 23:06:17 +0700 Subject: [PATCH] add share button #2 return Ru localization for attrs and fn --- src/boop.rs | 2 +- src/image_board.rs | 25 ++++++------ src/paginate.rs | 99 +++++++++++++++++++++++++++++----------------- src/phrazes.rs | 2 +- 4 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/boop.rs b/src/boop.rs index 118f30d..e24e605 100644 --- a/src/boop.rs +++ b/src/boop.rs @@ -2,7 +2,7 @@ use crate::{Context, Error}; use poise::{serenity_prelude as serenity, CreateReply}; /// Boop the bot! -#[poise::command(prefix_command, track_edits, slash_command)] +#[poise::command(prefix_command, track_edits, slash_command, nsfw_only)] pub async fn boop(ctx: Context<'_>) -> Result<(), Error> { let uuid_boop = ctx.id(); diff --git a/src/image_board.rs b/src/image_board.rs index 3aa1d4c..56b81de 100644 --- a/src/image_board.rs +++ b/src/image_board.rs @@ -11,28 +11,28 @@ static LEN_MAX: usize = 30; guild_only, nsfw_only, description_localized("ru", "Получить дозу эмоций"), - // name_localized("ru", "порно"), + name_localized("ru", "порно"), ephemeral )] pub async fn porno( ctx: Context<'_>, #[description = "let me your favorite tags!"] #[description_localized("ru", "Дай мне свои любимые теги")] - // #[name_localized("ru", "позитивные теги")] + #[name_localized("ru", "позитивные_теги")] // #[name_localized("us", "positive tags")] positive_tags: Option, #[description = "let me your unfavorite tags!"] #[description_localized("ru", "Что не нравится?")] - // #[name_localized("ru", "негативные теги")] + #[name_localized("ru", "негативные_теги")] // #[name_localized("us", "negative tags")] negative_tags: Option, - #[description = "Do you wana some special?!"] + #[description = "Do you wanna some special?!"] #[description_localized("ru", "Позвони и узнай, как там с деньгами!")] - // #[name_localized("ru", "пробив по номеру")] + #[name_localized("ru", "пробив_по_номеру")] id: Option, #[description = "How many do you want?"] #[description_localized("ru", "Сколько выдать?")] - // #[name_localized("ru", "количество")] + #[name_localized("ru", "количество")] size: Option, ) -> Result<(), Error> { let mut posts = vec![]; @@ -68,11 +68,14 @@ pub async fn porno( } } else { info!("Nothing addition params found"); - for _ in 0..size { - match R34!(R; D) { - Ok(data) => posts.push(data.data()[0].clone()), - Err(_) => continue, - } + match shuller::rules::rule34::params::R34Params::init() + .page(random_usize!(u16::MAX as usize) as u16) + .limit(size as u16) + .download() + .await + { + Ok(data) => posts.append(&mut data.data()), + Err(_) => return Err("**Error: Posts** not found".into()), } } diff --git a/src/paginate.rs b/src/paginate.rs index 95c4cb6..036f2ba 100644 --- a/src/paginate.rs +++ b/src/paginate.rs @@ -1,18 +1,17 @@ -use crate::{phrazes::PHRAZES, Context}; -// use phrazes; +use crate::{phrazes::PHRASES, Context}; +use ::serenity::all::{CreateMessage, ReactionType}; use poise::{serenity_prelude as serenity, CreateReply}; use shuller::{prelude::Post, random_usize}; use tracing::info; pub async fn paginate(ctx: Context<'_>, posts: &[Post]) -> Result<(), crate::Error> { - // Define some unique identifiers for the navigation buttons let ctx_id = ctx.id(); let mut current_page = 0; - let buttons = |x: &usize| { + let buttons = |current_num: &usize| { let mut vec = vec![]; if posts.len() != 1 { - for num in 0..posts.len() { - vec.push(if x == &num { + posts.iter().enumerate().for_each(|(num, _)| { + vec.push(if current_num == &num { serenity::CreateButton::new(format!("{}-{}", ctx_id, num)) .label(format!("{}", num + 1)) .style(serenity::ButtonStyle::Success) @@ -21,7 +20,7 @@ pub async fn paginate(ctx: Context<'_>, posts: &[Post]) -> Result<(), crate::Err .label(format!("{}", num + 1)) .style(serenity::ButtonStyle::Secondary) }) - } + }) } else { vec.push( serenity::CreateButton::new(format!("{}-{}", ctx_id, 0)) @@ -29,59 +28,87 @@ pub async fn paginate(ctx: Context<'_>, posts: &[Post]) -> Result<(), crate::Err .style(serenity::ButtonStyle::Success), ); } + vec.push( + serenity::CreateButton::new(format!("{}-send_to_chat", ctx_id)) + .emoji(ReactionType::Unicode("🚀".to_string())) + .style(serenity::ButtonStyle::Primary), + ); vec }; - let post = |page: &usize| { + let post = |page: &usize, shared: bool| { info!("page is: {:#?}", &posts[*page]); - serenity::CreateEmbed::default() - .image(&posts[*page].sample_url) - .url(&posts[*page].file_url) - .title(PHRAZES[random_usize!(PHRAZES.len())]) - .color(serenity::Colour::from_rgb( - random_usize!(255) as u8, - random_usize!(255) as u8, - random_usize!(255) as u8, - )) - .description(format!( - "**Tags:** + match shared { + true => serenity::CreateEmbed::default() + .image(&posts[*page].sample_url) + .url(&posts[*page].file_url) + .title(PHRASES[random_usize!(PHRASES.len())]) + .color(serenity::Colour::from_rgb( + random_usize!(255) as u8, + random_usize!(255) as u8, + random_usize!(255) as u8, + )) + .description(format!( + "**Tags:** {}", - &posts[*page].tags - )) - .author(serenity::CreateEmbedAuthor::new(&posts[*page].owner)) + &posts[*page].tags + )) + .author(serenity::CreateEmbedAuthor::new(&posts[*page].owner)) + .footer( + serenity::CreateEmbedFooter::new(format!("shared by: {}", ctx.author().name)) + .icon_url(ctx.author().avatar_url().unwrap_or_default()), + ), + false => serenity::CreateEmbed::default() + .image(&posts[*page].sample_url) + .url(&posts[*page].file_url) + .title(PHRASES[random_usize!(PHRASES.len())]) + .color(serenity::Colour::from_rgb( + random_usize!(255) as u8, + random_usize!(255) as u8, + random_usize!(255) as u8, + )) + .description(format!( + "**Tags:** + {}", + &posts[*page].tags + )) + .author(serenity::CreateEmbedAuthor::new(&posts[*page].owner)), + } }; - // Send the embed with the first page as content let reply = { let components = serenity::CreateActionRow::Buttons(buttons(¤t_page)); CreateReply::default() - .embed(post(¤t_page)) + .embed(post(¤t_page, false)) .components(vec![components]) }; ctx.send(reply).await?; - // Loop through incoming interactions with the navigation buttons 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 1 hours - .timeout(std::time::Duration::from_secs(3600)) + .timeout(std::time::Duration::from_secs(60 * 60)) .await { - // Depending on which button was pressed, go to next or previous page - current_page = { - let temp: Vec<&str> = press.data.custom_id.split("-").collect(); - temp[1].parse().unwrap_or(0) - }; - // Update the message with the new page contents + if press.data.custom_id == format!("{}-send_to_chat", ctx_id) { + ctx.channel_id() + .send_message( + &ctx.http(), + CreateMessage::new().embed(post(¤t_page, true)), + ) + .await?; + } else { + current_page = { + let temp: Vec<&str> = press.data.custom_id.split("-").collect(); + temp[1].parse().unwrap_or(0) + }; + } press .create_response( ctx.serenity_context(), serenity::CreateInteractionResponse::UpdateMessage( serenity::CreateInteractionResponseMessage::new() - .embed(post(¤t_page)) + .embed(post(¤t_page, false)) .components(vec![serenity::CreateActionRow::Buttons(buttons( ¤t_page, ))]), diff --git a/src/phrazes.rs b/src/phrazes.rs index 8813e9a..96136e0 100644 --- a/src/phrazes.rs +++ b/src/phrazes.rs @@ -1,4 +1,4 @@ -pub const PHRAZES: [&str; 1690] = [ +pub const PHRASES: [&str; 1690] = [ "Look at full picture!", "Be honor, check this!", "Ohhh, that what I'm wating for",