thumbnails

This commit is contained in:
2025-02-15 18:53:58 +01:00
parent 9b5f1bc9d5
commit fa5d391a9f
3 changed files with 53 additions and 25 deletions
+15 -13
View File
@@ -329,7 +329,7 @@ pub async fn bk_week_update(
let r_data = get_reddit_data(ctx).await.unwrap();
let c_id = get_c_id(ctx).await.unwrap_or_else(|| 0);
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), format!("Done!\nReading messages in <#{}>...", c_id)).await;
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), format!("\nReading messages in <#{}>...", c_id)).await;
if c_id == 0 {
send_msg(ctx, "Could not find bk_week_channel in data!\nHint: Run `/bk_week_bind` in a (preferably read-only) channel.".to_string(), true, true).await;
@@ -338,17 +338,13 @@ pub async fn bk_week_update(
let msgs = read_msgs(ctx, c_id).await;
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "Done!\nParsing messages to JSON...".to_string()).await;
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\nParsing messages to JSON...".to_string()).await;
let msgs_json = msgs_to_json(ctx, msgs, &r_data).await;
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "Done!\nAdding new posts...".to_string()).await;
// TODO: parse to JSON
// TODO: parse to JSON ^^^^^^^^^^^^
// json should be {"added": [], "updated": [], "removed": []}
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\nAdding new posts...".to_string()).await;
// TODO: add new posts to channel
let weekly_art = r_data["bk_weekly_art_posts"].as_object().unwrap();
for url in weekly_art.keys() {
@@ -358,15 +354,21 @@ pub async fn bk_week_update(
send_embed(ctx, messages::embed_post(&weekly_art[url], url, false), false).await;
}
if only_add.unwrap_or_else(|| false) {
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\n## Done!".to_string()).await;
return Ok(());
}
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\nEditing updated posts...".to_string()).await;
// TODO: edit outdated posts
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\nRemoving removed posts...".to_string()).await;
// TODO: remove removed posts
/* MSG FORMAT:
{json as spoiler}
{embed}
*/
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\nRemoving duplicate posts...".to_string()).await;
// TODO: remove duplicates
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "\n## Done!".to_string()).await;
return Ok(());
}
+7 -3
View File
@@ -3,7 +3,7 @@ use std::process;
use crate::data::dc_add_server;
use crate::websocket::send_cmd_json;
use crate::{data, Context, Error};
use crate::messages::{send_embed, send_msg, edit_msg, EmbedOptions};
use crate::messages::{edit_msg, send_embed, send_msg, Author, EmbedOptions};
use poise::serenity_prelude::{OnlineStatus, Timestamp, UserId};
use rand::{seq::IteratorRandom, Rng};
@@ -74,7 +74,9 @@ pub async fn embed(
#[description = "Timestamp at bottom (best to leave empty)."] timestamp: Option<Timestamp>,
#[description = "Empheral (only visible to you)."] empheral: Option<bool>,
#[description = "Shows \"used {Command}\" reply text."] reply: Option<bool>,
#[description = "Text that appears above and outside of the embed"] message: Option<String>
#[description = "Text that appears above and outside of the embed."] message: Option<String>,
#[description = "A URL for a thumbnail image."] thumbnail: Option<String>,
#[description = "Sets yourself as the author."] author: Option<bool>
) -> Result<(), Error>
{
let reply_unwrap = reply.unwrap_or_else(|| false);
@@ -88,7 +90,9 @@ pub async fn embed(
url,
ts: timestamp,
empheral: empheral.unwrap_or_else(|| false),
message
message,
thumbnail,
author: if author.unwrap_or_else(|| false) { Some(Author { name: ctx.author().name.clone(), url: "".to_string(), icon_url: ctx.author().avatar_url().unwrap() }) } else { None }
},
reply_unwrap
).await;
+29 -7
View File
@@ -2,9 +2,15 @@ use crate::Context;
use poise::serenity_prelude::json::Value;
use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle};
use poise::serenity_prelude::{Color, CreateEmbed, Timestamp};
use poise::serenity_prelude::{Color, CreateEmbed, CreateEmbedAuthor, Timestamp};
use serde_json::json;
pub struct Author {
pub name: String,
pub url: String,
pub icon_url: String
}
pub struct EmbedOptions {
pub desc: String,
pub title: Option<String>,
@@ -12,7 +18,9 @@ pub struct EmbedOptions {
pub url: Option<String>,
pub ts: Option<Timestamp>,
pub empheral: bool,
pub message: Option<String>
pub message: Option<String>,
pub author: Option<Author>,
pub thumbnail: Option<String>
}
impl Default for EmbedOptions {
fn default() -> Self {
@@ -23,7 +31,9 @@ impl Default for EmbedOptions {
url: None,
ts: None,
empheral: false,
message: None
message: None,
author: None,
thumbnail: None
};
}
}
@@ -67,12 +77,19 @@ pub async fn send_embed(
reply: bool
) -> Option<ReplyHandle<'_>>
{
let mut author: Option<CreateEmbedAuthor> = None;
if let Some(o_author) = options.author {
author = Some(CreateEmbedAuthor::new(o_author.name).url(o_author.url).icon_url(o_author.icon_url))
}
let mut embed = CreateEmbed::new()
.title (none_to_empty(options.title))
.description(options.desc)
.colour (Color::new(options.col.unwrap_or_else(|| DEFAULT_DC_COL)))
.url (none_to_empty(options.url));
if let Some(a) = author { embed = embed.author(a); }
if options.thumbnail.is_some() { embed = embed.thumbnail(options.thumbnail.unwrap()); }
if options.ts.is_some() { embed = embed.timestamp(options.ts.unwrap()); }
if reply {
@@ -118,7 +135,6 @@ pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions
**URL:** ||<{}>||
**Media URLS:**
{}
## Listing Data:
**Added by:** `{{ human: {}, bot: {} }}`
**Approved by:** `{{ human: {}, bot: [not implemented] }}`"#,
@@ -137,13 +153,19 @@ pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions
.collect::<Vec<_>>()
.join("\n");
let json_min = json!({"post_data": post_data["post_data"]["upvotes"], "added": post_data["added"], "approved": post_data["approved"]});
let media_urls = post_data["post_data"]["media_urls"].as_array().unwrap();
return EmbedOptions {
title: Some(post_data["post_data"]["title"].as_str().unwrap().to_string()),
desc: trimmed,
desc: format!("{}\nJSON: ||`{}`||", trimmed, serde_json::to_string(&json_min).unwrap()),
col: Some(DEFAULT_DC_COL),
url: Some(url.to_string()),
ts: Some(Timestamp::from_unix_timestamp(post_data["post_data"]["date_unix"].as_i64().unwrap()).unwrap()),
message: Some(format!("||`{{\"{}\":{}}}`||", url, serde_json::to_string(post_data).unwrap())),
empheral
empheral,
thumbnail: media_urls.get(0)
.and_then(|url| url.as_str().map(|s| s.to_string()))
.or_else(|| None),
..Default::default()
};
}