From 45004bb206ab2e7cc6ac2e280f43d9547c32491a Mon Sep 17 00:00:00 2001 From: Aleksei Rybin <0xalekseirybin@gmail.com> Date: Mon, 19 Feb 2024 23:00:03 +0700 Subject: [PATCH] Added hide_usernames --- assets/config.yml | 2 ++ src/config.rs | 7 +++++++ src/handler.rs | 4 +++- src/main.rs | 1 + src/utils.rs | 10 +++++++++- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/assets/config.yml b/assets/config.yml index 0de34a8..6ddc250 100644 --- a/assets/config.yml +++ b/assets/config.yml @@ -10,3 +10,5 @@ muted_channels_ids: [] allowed_users_ids: [] muted_users_ids: [] + +hide_usernames: false diff --git a/src/config.rs b/src/config.rs index ad704c4..60aee5d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,10 @@ fn empty_ids_vec() -> Vec { vec![] } +fn default_false() -> bool { + false +} + #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Config { pub discord_token: Option, @@ -27,6 +31,9 @@ pub struct Config { pub allowed_users_ids: Vec, #[serde(default = "empty_ids_vec")] pub muted_users_ids: Vec, + + #[serde(default = "default_false")] + pub hide_usernames: bool, } pub fn parse_config(path: String) -> Config { diff --git a/src/handler.rs b/src/handler.rs index fad67c6..33719c1 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -17,6 +17,8 @@ pub struct Handler { pub allowed_users_ids: Vec, pub muted_users_ids: Vec, + + pub hide_usernames: bool, } #[async_trait] @@ -42,7 +44,7 @@ impl EventHandler for Handler { } self.bot - .send_message(self.output_chat_id.clone(), format_message(ctx, msg).await) + .send_message(self.output_chat_id.clone(), format_message(ctx, msg, self.hide_usernames).await) .parse_mode(ParseMode::MarkdownV2) .await .unwrap(); diff --git a/src/main.rs b/src/main.rs index 9f4aa1a..6362b14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ async fn main() { muted_channels_ids: config.muted_channels_ids, allowed_users_ids: config.allowed_users_ids, muted_users_ids: config.muted_users_ids, + hide_usernames: config.hide_usernames, }) .await; diff --git a/src/utils.rs b/src/utils.rs index e66891a..0ca46cd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -29,7 +29,15 @@ pub fn escape_markdownv2(text: &str) -> String { .collect() } -pub async fn format_message(ctx: Context, msg: Message) -> String { +pub async fn format_message(ctx: Context, msg: Message, hide_username: bool) -> String { + if hide_username { + return format!( + "{}\n{}", + escape_markdownv2(&msg.content_safe(ctx.cache)), + msg.embeds.into_iter().map(format_embed).collect::() + ); + } + let author_part = if msg.is_private() { escape_markdownv2(&msg.author.tag()) } else {