thumbnails
This commit is contained in:
+16
-14
@@ -329,7 +329,7 @@ pub async fn bk_week_update(
|
|||||||
let r_data = get_reddit_data(ctx).await.unwrap();
|
let r_data = get_reddit_data(ctx).await.unwrap();
|
||||||
|
|
||||||
let c_id = get_c_id(ctx).await.unwrap_or_else(|| 0);
|
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 {
|
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;
|
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;
|
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;
|
let msgs_json = msgs_to_json(ctx, msgs, &r_data).await;
|
||||||
|
// TODO: parse to JSON ^^^^^^^^^^^^
|
||||||
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "Done!\nAdding new posts...".to_string()).await;
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: parse to JSON
|
|
||||||
// json should be {"added": [], "updated": [], "removed": []}
|
// 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();
|
let weekly_art = r_data["bk_weekly_art_posts"].as_object().unwrap();
|
||||||
|
|
||||||
for url in weekly_art.keys() {
|
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;
|
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
|
// 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
|
// TODO: remove removed posts
|
||||||
|
|
||||||
/* MSG FORMAT:
|
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "✅\nRemoving duplicate posts...".to_string()).await;
|
||||||
{json as spoiler}
|
// TODO: remove duplicates
|
||||||
{embed}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
p_text = update_progress(ctx, progress.clone().unwrap(), p_text.clone(), "✅\n## Done!".to_string()).await;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -3,7 +3,7 @@ use std::process;
|
|||||||
use crate::data::dc_add_server;
|
use crate::data::dc_add_server;
|
||||||
use crate::websocket::send_cmd_json;
|
use crate::websocket::send_cmd_json;
|
||||||
use crate::{data, Context, Error};
|
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 poise::serenity_prelude::{OnlineStatus, Timestamp, UserId};
|
||||||
use rand::{seq::IteratorRandom, Rng};
|
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 = "Timestamp at bottom (best to leave empty)."] timestamp: Option<Timestamp>,
|
||||||
#[description = "Empheral (only visible to you)."] empheral: Option<bool>,
|
#[description = "Empheral (only visible to you)."] empheral: Option<bool>,
|
||||||
#[description = "Shows \"used {Command}\" reply text."] reply: 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>
|
) -> Result<(), Error>
|
||||||
{
|
{
|
||||||
let reply_unwrap = reply.unwrap_or_else(|| false);
|
let reply_unwrap = reply.unwrap_or_else(|| false);
|
||||||
@@ -88,7 +90,9 @@ pub async fn embed(
|
|||||||
url,
|
url,
|
||||||
ts: timestamp,
|
ts: timestamp,
|
||||||
empheral: empheral.unwrap_or_else(|| false),
|
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
|
reply_unwrap
|
||||||
).await;
|
).await;
|
||||||
|
|||||||
+30
-8
@@ -2,9 +2,15 @@ use crate::Context;
|
|||||||
|
|
||||||
use poise::serenity_prelude::json::Value;
|
use poise::serenity_prelude::json::Value;
|
||||||
use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle};
|
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 struct EmbedOptions {
|
||||||
pub desc: String,
|
pub desc: String,
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
@@ -12,7 +18,9 @@ pub struct EmbedOptions {
|
|||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
pub ts: Option<Timestamp>,
|
pub ts: Option<Timestamp>,
|
||||||
pub empheral: bool,
|
pub empheral: bool,
|
||||||
pub message: Option<String>
|
pub message: Option<String>,
|
||||||
|
pub author: Option<Author>,
|
||||||
|
pub thumbnail: Option<String>
|
||||||
}
|
}
|
||||||
impl Default for EmbedOptions {
|
impl Default for EmbedOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@@ -23,7 +31,9 @@ impl Default for EmbedOptions {
|
|||||||
url: None,
|
url: None,
|
||||||
ts: None,
|
ts: None,
|
||||||
empheral: false,
|
empheral: false,
|
||||||
message: None
|
message: None,
|
||||||
|
author: None,
|
||||||
|
thumbnail: None
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,12 +77,19 @@ pub async fn send_embed(
|
|||||||
reply: bool
|
reply: bool
|
||||||
) -> Option<ReplyHandle<'_>>
|
) -> 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()
|
let mut embed = CreateEmbed::new()
|
||||||
.title (none_to_empty(options.title))
|
.title (none_to_empty(options.title))
|
||||||
.description(options.desc)
|
.description(options.desc)
|
||||||
.colour (Color::new(options.col.unwrap_or_else(|| DEFAULT_DC_COL)))
|
.colour (Color::new(options.col.unwrap_or_else(|| DEFAULT_DC_COL)))
|
||||||
.url (none_to_empty(options.url));
|
.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 options.ts.is_some() { embed = embed.timestamp(options.ts.unwrap()); }
|
||||||
|
|
||||||
if reply {
|
if reply {
|
||||||
@@ -118,7 +135,6 @@ pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions
|
|||||||
**URL:** ||<{}>||
|
**URL:** ||<{}>||
|
||||||
**Media URLS:**
|
**Media URLS:**
|
||||||
{}
|
{}
|
||||||
|
|
||||||
## Listing Data:
|
## Listing Data:
|
||||||
**Added by:** `{{ human: {}, bot: {} }}`
|
**Added by:** `{{ human: {}, bot: {} }}`
|
||||||
**Approved by:** `{{ human: {}, bot: [not implemented] }}`"#,
|
**Approved by:** `{{ human: {}, bot: [not implemented] }}`"#,
|
||||||
@@ -137,13 +153,19 @@ pub fn embed_post(post_data: &Value, url: &str, empheral: bool) -> EmbedOptions
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.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 {
|
return EmbedOptions {
|
||||||
title: Some(post_data["post_data"]["title"].as_str().unwrap().to_string()),
|
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),
|
col: Some(DEFAULT_DC_COL),
|
||||||
url: Some(url.to_string()),
|
url: Some(url.to_string()),
|
||||||
ts: Some(Timestamp::from_unix_timestamp(post_data["post_data"]["date_unix"].as_i64().unwrap()).unwrap()),
|
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()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user