Skip to content

Commit

Permalink
proxied message signature support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruthenic committed Sep 3, 2023
1 parent 3db4b58 commit 891f79f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/commands/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub async fn mate(
#[description = "the new avatar to use when proxying"] avatar: Option<serenity::Attachment>,
#[description = "the mate's bio"] bio: Option<String>,
#[description = "the mate's pronouns"] pronouns: Option<String>,
#[description = "a signature to add to any proxied messages (ie `💙- text`)"] signature: Option<
String,
>,
) -> Result<()> {
let database = &ctx.data().database;

Expand Down Expand Up @@ -67,6 +70,7 @@ pub async fn mate(
selector,
publicity,
avatar_url,
signature,
)
.await?;

Expand Down
11 changes: 10 additions & 1 deletion src/commands/mate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use poise::serenity_prelude::{self as serenity};

use super::{autocomplete::mate as mate_autocomplete, CommandContext};
use crate::{
models::{DBCollective, DBMate, DBMate__new},
models::{DBCollective, DBMate, DBMate__new, Signature},
utils::{
collectives::{get_or_create_collective, update_switch_logs},
messages::parse_selector,
Expand All @@ -28,6 +28,9 @@ pub async fn create(
>,
#[description = "the mate's bio"] bio: Option<String>,
#[description = "the mate's pronouns"] pronouns: Option<String>,
#[description = "a signature to add to any proxied messages (ie `💙- text`)"] signature: Option<
String,
>,
) -> Result<()> {
let database = &ctx.data().database;
let mates_collection = database.collection::<DBMate>("mates");
Expand Down Expand Up @@ -58,6 +61,11 @@ pub async fn create(
} else {
avatar_url = envvar("DEFAULT_AVATAR_URL");
}
let signature = if let Some(signature) = signature {
Some(Signature::parse(signature))
} else {
None
};

let mate = DBMate__new! {
user_id = ctx.author().id.get() as i64,
Expand All @@ -69,6 +77,7 @@ pub async fn create(
bio,
pronouns,
display_name,
signature,
autoproxy = false,
};

Expand Down
36 changes: 35 additions & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,41 @@ pub struct DBMate {
pub prefix: Option<String>,
pub postfix: Option<String>,
pub pronouns: Option<String>,
pub signature: Option<Signature>,
pub display_name: Option<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Signature {
pub prefix: String,
pub postfix: String,
}

impl Signature {
pub fn parse(signature: String) -> Self {
let mut split_sig = signature.split("text");

let split_sig = (split_sig.next(), split_sig.next());

let split_sig = match split_sig {
(None, None) => (String::new(), String::new()),
(None, Some(r)) => (String::new(), r.to_string()),
(Some(l), None) => (l.to_string(), String::new()),
(Some(l), Some(r)) => (l.to_string(), r.to_string()),
};

Signature {
prefix: split_sig.0,
postfix: split_sig.1,
}
}
}

#[impl_orderless]
impl DBMate {
#[make_orderless(
public = true,
defs(bio = None, prefix = None, postfix = None, pronouns = None, display_name = None, id = None),
defs(bio = None, prefix = None, postfix = None, pronouns = None, display_name = None, signature = None, id = None),
)]
pub fn new(
user_id: i64,
Expand All @@ -42,6 +69,7 @@ impl DBMate {
postfix: Option<String>,
pronouns: Option<String>,
display_name: Option<String>,
signature: Option<Signature>,
id: Option<ObjectId>,
) -> DBMate {
DBMate {
Expand All @@ -55,6 +83,7 @@ impl DBMate {
postfix,
pronouns,
display_name,
signature,
id,
}
}
Expand All @@ -69,6 +98,7 @@ impl DBMate {
selector: Option<String>,
publicity: Option<bool>,
avatar: Option<String>,
signature: Option<String>,
) -> Result<()> {
let current_name = self.name.clone();

Expand Down Expand Up @@ -104,6 +134,10 @@ impl DBMate {
self.avatar = avatar;
}

if let Some(signature) = signature {
self.signature = Some(Signature::parse(signature))
}

collection
.find_one_and_replace(
doc! { "user_id": self.user_id, "name": current_name },
Expand Down
6 changes: 5 additions & 1 deletion src/utils/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn send_proxied_message(
get_webhook_or_create(http, &channels_collection, message.channel_id).await?;

let new_content = message.content.clone();
let new_content = message
let mut new_content = message
.content
.clone()
.strip_prefix(&mate.prefix.clone().unwrap_or_default())
Expand All @@ -49,6 +49,10 @@ pub async fn send_proxied_message(
.unwrap_or(&new_content)
.to_string();

if let Some(sig) = &mate.signature {
new_content = format!("{}{}{}", sig.prefix, new_content, sig.postfix)
}

let mut builder = ExecuteWebhook::new();

builder = builder
Expand Down

0 comments on commit 891f79f

Please sign in to comment.