Skip to content

Commit

Permalink
fix(moderation): support attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Sep 20, 2024
1 parent 03adedd commit 713672b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 101 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bytesize = "1.3.0"
chrono = "0.4.38"
color-eyre = "0.6.3"
dotenvy = "0.15.7"
humansize = "2.1.3"
humantime = "2.1.0"
indexmap = { version = "2.5.0", features = ["serde"] }
nanoid = "0.4.0"
Expand Down
1 change: 0 additions & 1 deletion src/commands/fun/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod autoreply;
pub mod intelligence;
pub mod owo;
pub mod pomelo;
pub mod shiggy;
54 changes: 0 additions & 54 deletions src/commands/fun/pomelo.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn to_vec() -> Vec<
command!(fun, autoreply),
command!(fun, intelligence, ask),
command!(fun, owo),
command!(fun, pomelo),
command!(fun, shiggy),
command!(utils, ping),
command!(utils, presence),
Expand Down
113 changes: 81 additions & 32 deletions src/handlers/log.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use humansize::{format_size, FormatSizeOptions};
use poise::serenity_prelude::{self as serenity};

use color_eyre::eyre::Result;
use once_cell::sync::Lazy;
use poise::serenity_prelude::{self as serenity};

use crate::{utils::serenity::unique_username, Data};
use crate::{storage::log::MessageLog, utils::serenity::unique_username, Data};

pub async fn handle_message(message: &serenity::Message, data: &Data) -> Result<()> {
if let Some(storage) = &data.storage {
Expand Down Expand Up @@ -49,6 +51,7 @@ pub async fn edit(
author: &Option<serenity::UserId>,
prev_content: &Option<String>,
new_content: &str,
attachments: &[serenity::Attachment],
timestamp: &serenity::Timestamp,
) -> Result<()> {
if author == &Some(ctx.cache.current_user().id) {
Expand All @@ -63,24 +66,46 @@ pub async fn edit(
embed_author = embed_author.icon_url(author.to_user(&ctx).await?.face());
}

let mut embed = serenity::CreateEmbed::default()
.author(embed_author)
.field("Channel", format!("<#{channel}>"), false)
.field(
"Previous content",
prev_content.to_owned().unwrap_or("*Unknown*".to_owned()),
false,
)
.field("New content", new_content, false)
.field("Author", format_user(author.as_ref()), false)
.color(0xffd43b)
.timestamp(timestamp);

if !attachments.is_empty() {
embed = embed.field(
"Attachments",
attachments
.iter()
.map(|att| {
format!(
"[{}]({}) ({})",
att.filename,
att.url,
format_size(
att.size,
FormatSizeOptions::default().space_after_value(true)
)
)
})
.collect::<Vec<_>>()
.join("\n"),
false,
);
}

logs_channel
.send_message(
&ctx.http,
serenity::CreateMessage::default()
.embed(
serenity::CreateEmbed::default()
.author(embed_author)
.field("Channel", format!("<#{channel}>"), false)
.field(
"Previous content",
prev_content.as_ref().unwrap_or(&"*Unknown*".to_owned()),
false,
)
.field("New content", new_content, false)
.field("Author", format_user(author.as_ref()), false)
.color(0xffd43b)
.timestamp(timestamp),
)
.embed(embed)
.components(make_link_components(&link, "Jump")),
)
.await?;
Expand All @@ -96,11 +121,17 @@ pub async fn delete(
&serenity::ChannelId,
&Option<serenity::GuildId>,
),
author: &Option<serenity::UserId>,
content: &Option<String>,
log: &Option<MessageLog>,
timestamp: &serenity::Timestamp,
) -> Result<()> {
if author == &Some(ctx.cache.current_user().id) {
let content = log.as_ref().and_then(|l| l.content.clone());
let author = log.as_ref().and_then(|l| l.author);
let attachments = log
.as_ref()
.map(|l| l.attachments.clone())
.unwrap_or_default();

if author == Some(ctx.cache.current_user().id) {
return Ok(());
}

Expand All @@ -112,23 +143,41 @@ pub async fn delete(
embed_author = embed_author.icon_url(author.to_user(&ctx).await?.face());
}

let mut embed = serenity::CreateEmbed::default()
.author(embed_author)
.field("Channel", format!("<#{channel}>"), false)
.field("Content", content.unwrap_or("*Unknown*".to_owned()), false)
.field("Author", format_user(author.as_ref()), false)
.color(0xff6b6b)
.timestamp(timestamp);

if !attachments.is_empty() {
embed = embed.field(
"Attachments",
attachments
.iter()
.map(|att| {
format!(
"[{}]({}) ({})",
att.filename,
att.url,
format_size(
att.size,
FormatSizeOptions::default().space_after_value(true)
)
)
})
.collect::<Vec<_>>()
.join("\n"),
false,
);
}

logs_channel
.send_message(
&ctx.http,
serenity::CreateMessage::default()
.embed(
serenity::CreateEmbed::default()
.author(embed_author)
.field("Channel", format!("<#{channel}>"), false)
.field(
"Content",
content.as_ref().unwrap_or(&"*Unknown*".to_owned()),
false,
)
.field("Author", format_user(author.as_ref()), false)
.color(0xff6b6b)
.timestamp(timestamp),
)
.embed(embed)
.components(make_link_components(&link, "Jump")),
)
.await?;
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ async fn event_handler(

let content = event.content.clone();
let author = event.author.as_ref().map(|a| a.id);
let attachments = event
.attachments
.as_ref()
.map(|a| a.to_vec())
.unwrap_or_default();

storage
.set_message_log(
&event.id.to_string(),
&MessageLog::new(content.as_ref().map(|s| s.to_string()), author),
&MessageLog::new(
content.as_ref().map(|s| s.to_string()),
author,
attachments.clone(),
),
)
.await?;

Expand All @@ -80,6 +89,7 @@ async fn event_handler(
&content
.as_ref()
.map_or("*Unknown*".to_owned(), |s| s.to_string()),
&attachments,
&timestamp,
)
.await?;
Expand All @@ -104,8 +114,7 @@ async fn event_handler(
handlers::log::delete(
ctx.serenity_context,
(deleted_message_id, channel_id, guild_id),
&prev.as_ref().and_then(|p| p.author),
&prev.and_then(|p| p.content),
&prev,
&timestamp,
)
.await?;
Expand Down
22 changes: 12 additions & 10 deletions src/storage/log.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use poise::serenity_prelude::{Message, UserId};
use poise::serenity_prelude::{Attachment, Message, UserId};

use redis_macros::{FromRedisValue, ToRedisArgs};
use serde::{Deserialize, Serialize};

#[derive(FromRedisValue, ToRedisArgs, Serialize, Deserialize, Clone, Debug)]
pub struct MessageLog {
pub content: Option<String>,
pub author: Option<UserId>,
pub attachments: Vec<Attachment>,
}

impl MessageLog {
pub const fn new(content: Option<String>, author: Option<UserId>) -> Self {
Self { content, author }
}
}

impl From<Message> for MessageLog {
fn from(value: Message) -> Self {
pub const fn new(
content: Option<String>,
author: Option<UserId>,
attachments: Vec<Attachment>,
) -> Self {
Self {
content: Some(value.content.into_string()),
author: Some(value.author.id),
content,
author,
attachments,
}
}
}
Expand All @@ -28,6 +29,7 @@ impl From<&Message> for MessageLog {
Self {
content: Some(value.content.clone().into_string()),
author: Some(value.author.id),
attachments: value.attachments.to_vec(),
}
}
}

0 comments on commit 713672b

Please sign in to comment.