fixed bugs when not using shorturl

This commit is contained in:
2025-06-27 16:46:32 +02:00
parent 8f90ea74d4
commit 6ff9cafc34
9 changed files with 67 additions and 26 deletions
+5 -2
View File
@@ -4,7 +4,7 @@ use std::path::Path;
use serde_json::{self, Value, json};
use tokio::sync::Mutex;
use crate::{errln, rs_println, Data, Error, CFG_DATA_RE, LANG, LANG_NAME};
use crate::{errln, rs_println, rs_warnln, Data, Error, CFG_DATA_RE, LANG, LANG_NAME};
use crate::websocket::send_cmd_json;
@@ -94,7 +94,10 @@ fn generate_re_data() {
let mut preset_json: Value = serde_json::from_str(&preset_str).unwrap();
if let Some(bk_week) = preset_json[CFG_DATA_RE].as_object_mut() {
bk_week.remove("EXAMPLE VALUE");
bk_week.remove("EXAMPLE URL");
}
else {
rs_warnln!("Couldn't find key \"{}\" in the Reddit data file ({})!", CFG_DATA_RE, DATA_PATH_RE);
}
let json_str = serde_json::to_string_pretty(&preset_json).unwrap();
+25
View File
@@ -24,6 +24,19 @@ macro_rules! rs_errln {
}
#[macro_export]
macro_rules! rs_warnln {
($($arg:tt)*) => {
println!("{}WARNING{} RS - {}{}",
"\x1b[33m",
"\x1b[0m\x1b[31m",
format!($($arg)*),
"\x1b[0m"
);
};
}
#[macro_export]
macro_rules! errln {
($($arg:tt)*) => {
@@ -37,6 +50,18 @@ macro_rules! errln {
}
#[macro_export]
macro_rules! warnln {
($($arg:tt)*) => {
println!("{}WARNING{} - {}",
"\x1b[33m",
"\x1b[0m",
format!($($arg)*)
);
};
}
#[macro_export]
macro_rules! lang {
($key:expr) => {
+4 -2
View File
@@ -58,5 +58,7 @@ async def main():
await bot.stop()
asyncio.run(main())
try:
asyncio.run(main())
except KeyboardInterrupt:
raise SystemExit
+1 -1
View File
@@ -98,7 +98,7 @@ def read_data(bot: botPy.Bot) -> bool:
with open(os.path.join(DEFAULT_PATH, "re_data_preset.json"), "r") as f:
data_preset_json: dict[str, Any] = json.load(f)
data_preset_json[botPy.RE_DATA_POSTS].pop("EXAMPLE VALUE", None)
data_preset_json[botPy.RE_DATA_POSTS].pop("EXAMPLE URL", None)
with open(r_path, "w") as f:
json.dump(data_preset_json, f, indent = 2)
+3 -4
View File
@@ -1,6 +1,6 @@
use serde_json::json;
use crate::data::{get_mutex_data, update_re_data};
use crate::data::{get_mutex_data};
use crate::messages::send_msg;
use crate::re_cmds::get::get_post_from_data;
use crate::{data, websocket::send_cmd_json, Context, Error, CFG_DATA_RE};
@@ -37,7 +37,7 @@ pub async fn cmd(
send_msg(
ctx,
r#"Unknown error!
Error trace: `bk_week_cmds.rs -> bk_week_add() -> Unknown error`.
Error trace: `re_cmds/add.rs -> cmd() -> Unknown error`.
Common reasons: The URL provided was likely invalid or 403: forbidden (e.g a private subreddit)."#.to_string(),
true,
true
@@ -55,8 +55,7 @@ pub async fn cmd(
if a { send_msg(ctx, lang!("dc_msg_re_also_approved"), true, true).await; }
}
update_re_data(ctx.data()).await;
if let Some(post) = get_post_from_data(ctx, &reddit_data, &url).await? {
if let Some(post) = get_post_from_data(ctx, &reddit_data, &shorturl).await? {
send_embed_for_post(ctx, post, &url).await?;
}
+5 -2
View File
@@ -1,6 +1,6 @@
use serde_json::{json, Value};
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::is_bk_mod_msg, websocket, Context, Error, CFG_DATA_RE};
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::{is_bk_mod_msg, to_shorturl}, websocket, Context, Error, CFG_DATA_RE};
use super::generic_fns::send_embed_for_removed;
@@ -20,10 +20,13 @@ pub async fn cmd(
{
if !is_bk_mod_msg(ctx).await { return Ok(()); }
let shorturl_u = to_shorturl(&url);
let shorturl = &shorturl_u.unwrap_or(url.clone());
data::update_re_data(ctx.data()).await;
let reddit_data = get_mutex_data(&ctx.data().reddit_data).await?;
approve_cmd(ctx, &url, &reddit_data, !disapprove.unwrap_or(false)).await;
approve_cmd(ctx, &shorturl, &reddit_data, !disapprove.unwrap_or(false)).await;
return Ok(());
}
+6 -3
View File
@@ -1,6 +1,6 @@
use serde_json::Value;
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::send_embed_for_post, rs_println, Context, Error, CFG_DATA_RE};
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::{send_embed_for_post, to_shorturl}, rs_println, Context, Error, CFG_DATA_RE};
use super::generic_fns::send_embed_for_removed;
@@ -19,10 +19,13 @@ pub async fn cmd(
{
data::update_re_data(ctx.data()).await;
let shorturl_u = to_shorturl(&url);
let shorturl = &shorturl_u.unwrap_or(url.clone());
let reddit_data = get_mutex_data(&ctx.data().reddit_data).await?;
if let Some(post) = get_post_from_data(ctx, &reddit_data, &url).await? {
send_embed_for_post(ctx, post, &url).await?;
if let Some(post) = get_post_from_data(ctx, &reddit_data, &shorturl).await? {
send_embed_for_post(ctx, post, &shorturl).await?;
}
return Ok(());
+9 -6
View File
@@ -1,6 +1,6 @@
use serde_json::json;
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::{generic_fns::{is_bk_mod_msg, send_embed_for_removed}, get::get_post_from_data}, websocket::send_cmd_json, Context, Error};
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::{generic_fns::{is_bk_mod_msg, send_embed_for_removed, to_shorturl}, get::get_post_from_data}, websocket::send_cmd_json, Context, Error};
#[poise::command(
slash_command,
@@ -18,27 +18,30 @@ pub async fn cmd(
{
if !is_bk_mod_msg(ctx).await { return Ok(()); }
let shorturl_u = to_shorturl(&url);
let shorturl = &shorturl_u.unwrap_or(url.clone());
let auth = &ctx.author().name;
let r = send_cmd_json("remove_post_url", Some(json!([&url, &auth, &reason])), true).await.unwrap();
let r = send_cmd_json("remove_post_url", Some(json!([&shorturl, &auth, &reason])), true).await.unwrap();
if r["value"].as_bool().unwrap() {
send_msg(
ctx,
lang!("dc_msg_re_post_remove_success", &url),
lang!("dc_msg_re_post_remove_success", &shorturl),
true,
true
).await;
}
else {
send_msg(ctx, lang!("dc_msg_re_post_404"), false, false).await;
send_msg(ctx, lang!("dc_msg_re_post_404"), true, true).await;
}
data::update_re_data(ctx.data()).await;
let reddit_data = get_mutex_data(&ctx.data().reddit_data).await?;
if let Some(post) = get_post_from_data(ctx, &reddit_data, &url).await? {
if let Some(post) = get_post_from_data(ctx, &reddit_data, &shorturl).await? {
if post["removed"]["removed"].as_bool().unwrap() {
send_embed_for_removed(ctx, &url, &post).await;
send_embed_for_removed(ctx, &shorturl, &post).await;
}
}
+9 -6
View File
@@ -1,6 +1,6 @@
use serde_json::json;
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::{is_bk_mod, send_embed_for_removed}, websocket::send_cmd_json, Context, Error, CFG_DATA_RE};
use crate::{data::{self, get_mutex_data}, lang, messages::send_msg, re_cmds::generic_fns::{is_bk_mod, send_embed_for_removed, to_shorturl}, websocket::send_cmd_json, Context, Error, CFG_DATA_RE};
#[poise::command(
slash_command,
@@ -21,17 +21,20 @@ pub async fn cmd(
let re_data = get_mutex_data(&ctx.data().reddit_data).await?;
let post_data = re_data[CFG_DATA_RE].clone();
let unw_vote = un_vote.unwrap_or(false);
let shorturl_u = to_shorturl(&url);
let shorturl = &shorturl_u.unwrap_or(url.clone());
if post_data.get(&url).is_none() {
if post_data.get(&shorturl).is_none() {
send_msg(ctx, lang!("dc_msg_re_post_404"), false, false).await;
return Ok(());
}
if post_data[&url]["removed"]["removed"].as_bool().unwrap() {
send_embed_for_removed(ctx, &url, &post_data[&url]).await;
if post_data[&shorturl]["removed"]["removed"].as_bool().unwrap() {
send_embed_for_removed(ctx, &shorturl, &post_data[&shorturl]).await;
return Ok(());
}
let url_data = &post_data[&url];
let url_data = &post_data[&shorturl];
let is_mod = is_bk_mod(ctx.data().bk_mods.clone(), ctx.author().id.get());
let voters_dc = url_data["votes"]["voters_dc"].as_array().unwrap();
@@ -47,7 +50,7 @@ pub async fn cmd(
return Ok(());
}
let r = send_cmd_json("set_vote_post", Some(json!([url, uid, is_mod, true, unw_vote])), true).await.unwrap();
let r = send_cmd_json("set_vote_post", Some(json!([shorturl, uid, is_mod, true, unw_vote])), true).await.unwrap();
let unw_r = r["value"].as_bool().unwrap();
if unw_r && !unw_vote && is_mod {