Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
SilentTyping, NoReplyMention and TextReplace Rencord plugins (#30)
Matías Sánchez González123f7f7e226c
diff --git a/README.md b/README.mdindex a6b1a9e..762f376 100644--- a/README.md+++ b/README.md@@ -275,6 +275,9 @@ native hooks at the same points instead, and popular plugins are ported to them| MessageLogger | MessageLogger | Keeps deleted messages (red text or overlay) and shows edit history. Ignore lists for users, channels and servers. || ClearURLs | ClearURLs | Strips tracking parameters (`utm_*`, `fbclid`, `si`, Amazon `/ref=`, …) from links before they're sent. Custom rules supported. || MessageSelect | (none) | Hold Ctrl + Shift to select several messages at once. |+| SilentTyping | SilentTyping | Others don't see "is typing…" from you. |+| NoReplyMention | NoReplyMention | Replies start with "@" off, so they don't ping the author unless you turn it on. Ignore list for channels. |+| TextReplace | TextReplace | Your own `find => replace` rules, run on your messages before they're sent (and edits, if you like). |Plugins are switched on and configured in **⚙ → Rencord Plugins**. A plugin that crashes isturned off instead of taking RustCord down.diff --git a/apps/rustcord/src/app.rs b/apps/rustcord/src/app.rsindex ac3013b..5149b3b 100644--- a/apps/rustcord/src/app.rs+++ b/apps/rustcord/src/app.rs@@ -2858,10 +2858,13 @@ impl RustCord {pub fn start_reply(&mut self, channel: Id, msg: Id) {let Some(m) = self.store.message(channel, msg) else { return };- // Like Discord: replies ping the author, except when it's yourself.+ // Like Discord: replies ping the author, except when it's yourself+ // (or a plugin such as NoReplyMention starts them with "@" off).let mine = Some(m.author.id) == self.store.me_id();- let author = self.store.nick_in(None, Some(channel), m.author.id).unwrap_or_else(|| m.display_name().to_string());- self.reply = Some(ReplyTarget { channel, msg, author, mention: !mine });+ let author_id = m.author.id;+ let author = self.store.nick_in(None, Some(channel), author_id).unwrap_or_else(|| m.display_name().to_string());+ let mention = !mine && self.rencord.reply_mention(channel, author_id);+ self.reply = Some(ReplyTarget { channel, msg, author, mention });self.ctx.memory_mut(|mem| mem.request_focus(egui::Id::new(("composer", channel.0))));}diff --git a/crates/rencord/src/lib.rs b/crates/rencord/src/lib.rsindex 67803b6..2e2aca1 100644--- a/crates/rencord/src/lib.rs+++ b/crates/rencord/src/lib.rs@@ -97,6 +97,13 @@ pub trait Plugin: Send {true}+ /// A reply to `author`'s message in `channel` is being started. Return+ /// `false` to start it with the reply bar's "@" (ping the author) off;+ /// the bar's toggle still has the last word.+ fn reply_mention(&mut self, _cx: &Cx, _channel: Id, _author: Id) -> bool {+ true+ }+/// Extra entries for a message's context menu.fn message_actions(&self, _cx: &Cx, _msg: &Message) -> Vec<MessageAction> {Vec::new()@@ -321,6 +328,11 @@ impl Rencord {self.each(|p, cx| p.typing(cx, channel))}+ /// Whether a new reply to `author` starts with its ping on.+ pub fn reply_mention(&mut self, channel: Id, author: Id) -> bool {+ self.each(|p, cx| p.reply_mention(cx, channel, author))+ }+/// (plugin index, action) pairs for a message's context menu.pub fn message_actions(&self, msg: &Message) -> Vec<(usize, MessageAction)> {let me = self.me;diff --git a/crates/rencord/src/plugins/mod.rs b/crates/rencord/src/plugins/mod.rsindex 2ebc6cd..a6b438f 100644--- a/crates/rencord/src/plugins/mod.rs+++ b/crates/rencord/src/plugins/mod.rs@@ -4,13 +4,26 @@mod clear_urls;mod message_logger;pub mod message_select;+mod no_reply_mention;+mod silent_typing;+mod text_replace;pub use clear_urls::ClearUrls;pub use message_logger::MessageLogger;pub use message_select::MessageSelect;+pub use no_reply_mention::NoReplyMention;+pub use silent_typing::SilentTyping;+pub use text_replace::TextReplace;use crate::Plugin;pub fn builtin() -> Vec<Box<dyn Plugin>> {- vec![Box::new(MessageLogger), Box::new(ClearUrls), Box::new(MessageSelect)]+ vec![+ Box::new(MessageLogger),+ Box::new(ClearUrls),+ Box::new(MessageSelect),+ Box::new(SilentTyping),+ Box::new(NoReplyMention),+ Box::new(TextReplace),+ ]}diff --git a/crates/rencord/src/plugins/no_reply_mention.rs b/crates/rencord/src/plugins/no_reply_mention.rsnew file mode 100644index 0000000..a72c8e9--- /dev/null+++ b/crates/rencord/src/plugins/no_reply_mention.rs@@ -0,0 +1,80 @@+//! Port of Vencord's NoReplyMention: replies start with the reply bar's "@"+//! off, so they don't ping the author unless you turn it back on.++use crate::{Cx, Plugin, PluginMeta, SettingDef};+use discord::model::Id;++pub struct NoReplyMention;++impl Plugin for NoReplyMention {+ fn meta(&self) -> PluginMeta {+ PluginMeta {+ id: "NoReplyMention",+ name: "NoReplyMention",+ description: "Replies start with \"@\" off, so they don't ping the author unless you turn it on.",+ authors: &["RustCord"],+ vencord_equivalent: Some("NoReplyMention"),+ default_enabled: false,+ }+ }++ fn settings(&self) -> Vec<SettingDef> {+ vec![SettingDef::text(+ "ignoreChannels",+ "Ignored channels",+ "Channel IDs where replies still start with \"@\" on, comma separated.",+ "123456789012345678, …",+ false,+ )]+ }++ fn reply_mention(&mut self, cx: &Cx, channel: Id, _author: Id) -> bool {+ let ch = channel.to_string();+ cx.settings.list("ignoreChannels").iter().any(|x| x == &ch)+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{PluginSettings, Rencord, SettingValue};++ fn settings() -> PluginSettings {+ PluginSettings::new(NoReplyMention.settings(), Default::default())+ }++ #[test]+ fn metadata_matches_spec() {+ let meta = NoReplyMention.meta();+ assert_eq!(meta.id, "NoReplyMention");+ assert_eq!(meta.vencord_equivalent, Some("NoReplyMention"));+ assert!(!meta.default_enabled);+ }++ #[test]+ fn replies_start_without_the_ping() {+ let s = settings();+ let cx = Cx { me: None, settings: &s };+ assert!(!NoReplyMention.reply_mention(&cx, Id(10), Id(1)));+ }++ #[test]+ fn ignored_channels_keep_the_ping() {+ let mut s = settings();+ s.set("ignoreChannels", SettingValue::Text("10, 20".into()));+ let cx = Cx { me: None, settings: &s };+ assert!(NoReplyMention.reply_mention(&cx, Id(10), Id(1)));+ assert!(NoReplyMention.reply_mention(&cx, Id(20), Id(1)));+ assert!(!NoReplyMention.reply_mention(&cx, Id(30), Id(1)));+ }++ #[test]+ fn only_while_enabled() {+ let mut r = Rencord::load(None);+ let idx = r.entries().iter().position(|e| e.meta.id == "NoReplyMention").expect("NoReplyMention registered");+ assert!(!r.entries()[idx].enabled);+ assert!(r.reply_mention(Id(10), Id(1)));+ r.set_enabled(idx, true);+ assert!(!r.reply_mention(Id(10), Id(1)));+ }+}diff --git a/crates/rencord/src/plugins/silent_typing.rs b/crates/rencord/src/plugins/silent_typing.rsnew file mode 100644index 0000000..76b6454--- /dev/null+++ b/crates/rencord/src/plugins/silent_typing.rs@@ -0,0 +1,91 @@+//! Port of Vencord's SilentTyping: stops sending typing indicators when you+//! compose messages.++use crate::{Cx, Plugin, PluginMeta, SettingDef};+use discord::model::Id;++pub struct SilentTyping;++impl Plugin for SilentTyping {+ fn meta(&self) -> PluginMeta {+ PluginMeta {+ id: "SilentTyping",+ name: "SilentTyping",+ description: "Prevents sending typing indicators so others don't see when you're typing.",+ authors: &["RustCord"],+ vencord_equivalent: Some("SilentTyping"),+ default_enabled: false,+ }+ }++ fn settings(&self) -> Vec<SettingDef> {+ vec![SettingDef::text(+ "ignoreChannels",+ "Ignored channels",+ "Channel IDs to still send typing indicators in, comma separated.",+ "123456789012345678, …",+ false,+ )]+ }++ fn typing(&mut self, cx: &Cx, channel: Id) -> bool {+ let ch = channel.to_string();+ cx.settings.list("ignoreChannels").iter().any(|x| x == &ch)+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{PluginSettings, Rencord, SettingValue};++ fn settings() -> PluginSettings {+ PluginSettings::new(SilentTyping.settings(), Default::default())+ }++ #[test]+ fn metadata_matches_spec() {+ let meta = SilentTyping.meta();+ assert_eq!(meta.id, "SilentTyping");+ assert_eq!(meta.name, "SilentTyping");+ assert_eq!(meta.vencord_equivalent, Some("SilentTyping"));+ assert!(!meta.default_enabled);+ }++ #[test]+ fn suppresses_typing_by_default() {+ let s = settings();+ let cx = Cx { me: None, settings: &s };+ assert!(!SilentTyping.typing(&cx, Id(123456)));+ }++ #[test]+ fn allows_typing_in_ignored_channels() {+ let mut s = settings();+ s.set("ignoreChannels", SettingValue::Text("111, 222\n333".into()));+ let cx = Cx { me: None, settings: &s };+ assert!(SilentTyping.typing(&cx, Id(111)));+ assert!(SilentTyping.typing(&cx, Id(222)));+ assert!(SilentTyping.typing(&cx, Id(333)));+ assert!(!SilentTyping.typing(&cx, Id(444)));+ }++ #[test]+ fn works_through_rencord_manager() {+ let mut r = Rencord::load(None);+ let idx = r+ .entries()+ .iter()+ .position(|e| e.meta.id == "SilentTyping")+ .expect("SilentTyping registered");+ assert!(!r.entries()[idx].enabled);+ assert!(r.typing(Id(99)));++ r.set_enabled(idx, true);+ assert!(!r.typing(Id(99)));++ r.set_setting(idx, "ignoreChannels", SettingValue::Text("99".into()));+ assert!(r.typing(Id(99)));+ assert!(!r.typing(Id(100)));+ }+}diff --git a/crates/rencord/src/plugins/text_replace.rs b/crates/rencord/src/plugins/text_replace.rsnew file mode 100644index 0000000..90ddf90--- /dev/null+++ b/crates/rencord/src/plugins/text_replace.rs@@ -0,0 +1,186 @@+//! Port of Vencord's TextReplace: rewrites matching text in outgoing messages+//! (and optionally edits) using `find => replace` rules.++use crate::{Cx, Plugin, PluginMeta, SettingDef};+use discord::model::{Id, OutgoingMessage};++pub struct TextReplace;++struct Rule {+ find: String,+ replace: String,+}++impl Rule {+ fn parse(raw: &str) -> Option<Self> {+ let raw = raw.trim();+ if raw.is_empty() || raw.starts_with("//") {+ return None;+ }+ let (find, replace) = raw.split_once("=>")?;+ let find = find.trim();+ if find.is_empty() {+ return None;+ }+ let replace = replace.trim();+ Some(Self { find: find.to_string(), replace: replace.to_string() })+ }+}++fn rules(cx: &Cx) -> Vec<Rule> {+ cx.settings.text("rules").lines().filter_map(Rule::parse).collect()+}++fn apply_rules(text: &str, rules: &[Rule]) -> Option<String> {+ let mut out: Option<String> = None;+ for rule in rules {+ let cur = out.as_deref().unwrap_or(text);+ if cur.contains(&rule.find) {+ out = Some(cur.replace(&rule.find, &rule.replace));+ }+ }+ out+}++impl Plugin for TextReplace {+ fn meta(&self) -> PluginMeta {+ PluginMeta {+ id: "TextReplace",+ name: "TextReplace",+ description: "Automatically replaces text in your messages before they're sent using custom rules.",+ authors: &["RustCord"],+ vencord_equivalent: Some("TextReplace"),+ default_enabled: false,+ }+ }++ fn settings(&self) -> Vec<SettingDef> {+ vec![+ SettingDef::text(+ "rules",+ "Replacement rules",+ "One per line: `find => replace`. Blank lines and `//` comments are ignored. Rules see mentions and custom emoji as `<@id>` / `<:name:id>`, so a rule that matches digits can break them.",+ "shrug => ¯\\_(ツ)_/¯",+ true,+ ),+ SettingDef::bool(+ "applyOnEdit",+ "Apply on edit",+ "Also apply replacement rules when editing a message.",+ false,+ ),+ ]+ }++ fn before_send(&mut self, cx: &Cx, msg: &mut OutgoingMessage) -> bool {+ if let Some(replaced) = apply_rules(&msg.content, &rules(cx)) {+ msg.content = replaced;+ }+ true+ }++ fn before_edit(&mut self, cx: &Cx, _channel: Id, content: &mut String) -> bool {+ if cx.settings.bool("applyOnEdit") {+ if let Some(replaced) = apply_rules(content, &rules(cx)) {+ *content = replaced;+ }+ }+ true+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{PluginSettings, Rencord, SettingValue};++ fn settings(rules: &str, apply_on_edit: bool) -> PluginSettings {+ let mut s = PluginSettings::new(TextReplace.settings(), Default::default());+ s.set("rules", SettingValue::Text(rules.into()));+ s.set("applyOnEdit", SettingValue::Bool(apply_on_edit));+ s+ }++ fn out(content: &str) -> OutgoingMessage {+ OutgoingMessage { channel_id: Id(1), content: content.into(), ..Default::default() }+ }++ #[test]+ fn metadata_matches_spec() {+ let meta = TextReplace.meta();+ assert_eq!(meta.id, "TextReplace");+ assert_eq!(meta.name, "TextReplace");+ assert_eq!(meta.vencord_equivalent, Some("TextReplace"));+ assert!(!meta.default_enabled);+ }++ #[test]+ fn replaces_matching_text_and_ignores_comments_and_blanks() {+ let s = settings(+ "// comment => ignored\n\n teh => the\r\nshrug => ¯\\_(ツ)_/¯\nno_separator\n=> empty_find\n",+ false,+ );+ let cx = Cx { me: None, settings: &s };+ let mut m = out("teh cat and teh dog shrug");+ assert!(TextReplace.before_send(&cx, &mut m));+ assert_eq!(m.content, "the cat and the dog ¯\\_(ツ)_/¯");+ }++ #[test]+ fn supports_empty_replacement_and_arrow_in_replacement_and_chaining() {+ let s = settings("dropme =>\narrow => a => b\na => b\nb => c", false);+ let cx = Cx { me: None, settings: &s };+ let mut m = out("dropme arrow");+ assert!(TextReplace.before_send(&cx, &mut m));+ assert_eq!(m.content, " c => c");+ }++ #[test]+ fn leaves_unmatched_content_untouched() {+ let s = settings("foo => bar", false);+ let cx = Cx { me: None, settings: &s };+ let mut m = out("hello world");+ assert!(TextReplace.before_send(&cx, &mut m));+ assert_eq!(m.content, "hello world");++ let empty = PluginSettings::new(TextReplace.settings(), Default::default());+ let cx_empty = Cx { me: None, settings: &empty };+ let mut m2 = out("unchanged");+ assert!(TextReplace.before_send(&cx_empty, &mut m2));+ assert_eq!(m2.content, "unchanged");+ }++ #[test]+ fn apply_on_edit_respects_setting() {+ let s_off = settings("teh => the", false);+ let cx_off = Cx { me: None, settings: &s_off };+ let mut edit = "teh edit".to_string();+ assert!(TextReplace.before_edit(&cx_off, Id(1), &mut edit));+ assert_eq!(edit, "teh edit");++ let s_on = settings("teh => the", true);+ let cx_on = Cx { me: None, settings: &s_on };+ assert!(TextReplace.before_edit(&cx_on, Id(1), &mut edit));+ assert_eq!(edit, "the edit");+ }++ #[test]+ fn works_through_rencord_manager() {+ let mut r = Rencord::load(None);+ let idx = r+ .entries()+ .iter()+ .position(|e| e.meta.id == "TextReplace")+ .expect("TextReplace registered");+ assert!(!r.entries()[idx].enabled);++ r.set_setting(idx, "rules", SettingValue::Text("teh => the".into()));+ let mut m = out("teh test");+ assert!(r.before_send(&mut m));+ assert_eq!(m.content, "teh test");++ r.set_enabled(idx, true);+ assert!(r.before_send(&mut m));+ assert_eq!(m.content, "the test");+ }+}