Skip to content

Commit 6b2f043

Browse files
committed
add edit time limit to messages
1 parent 1de3042 commit 6b2f043

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/ban.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::CommandHistory;
12
use crate::{
23
api,
34
commands::{Args, Result},
@@ -48,7 +49,7 @@ pub(crate) fn save_unban(user_id: String, guild_id: String) -> Result<()> {
4849
Ok(())
4950
}
5051

51-
pub(crate) fn start_unban_thread(cx: Context) {
52+
pub(crate) fn start_cleanup_thread(cx: Context) {
5253
use std::str::FromStr;
5354
if !UNBAN_THREAD_INITIALIZED.load(Ordering::SeqCst) {
5455
UNBAN_THREAD_INITIALIZED.store(true, Ordering::SeqCst);
@@ -69,6 +70,20 @@ pub(crate) fn start_unban_thread(cx: Context) {
6970
info!("Unbanning user {}", &row.1);
7071
guild_id.unban(&cx, u64::from_str(&row.1)?)?;
7172
}
73+
74+
let mut data = cx.data.write();
75+
let history = data.get_mut::<CommandHistory>().unwrap();
76+
77+
// always keep the last command in history
78+
if history.len() > 0 {
79+
info!("Clearing command history");
80+
let (key, value) = history.pop().unwrap();
81+
history.drain(..);
82+
history.insert(key, value);
83+
}
84+
85+
drop(data);
86+
7287
sleep(Duration::new(HOUR, 0));
7388
}
7489
});

src/main.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ use envy;
2626
use indexmap::IndexMap;
2727
use serde::Deserialize;
2828
use serenity::{model::prelude::*, prelude::*, utils::CustomMessage};
29-
use std::collections::HashMap;
29+
use std::time::Duration;
3030

31+
const MESSAGE_AGE_MAX: Duration = Duration::from_secs(3600);
3132
#[derive(Deserialize)]
3233
struct Config {
3334
tags: bool,
@@ -224,7 +225,7 @@ fn main() {
224225

225226
struct CommandHistory {}
226227
impl TypeMapKey for CommandHistory {
227-
type Value = HashMap<MessageId, MessageId>;
228+
type Value = IndexMap<MessageId, MessageId>;
228229
}
229230

230231
struct Events {
@@ -242,24 +243,30 @@ impl RawEventHandler for Events {
242243
drop(cache);
243244

244245
let mut data = cx.data.write();
245-
data.insert::<CommandHistory>(HashMap::new());
246+
data.insert::<CommandHistory>(IndexMap::new());
246247
drop(data);
247248

248-
ban::start_unban_thread(cx);
249+
ban::start_cleanup_thread(cx);
249250
}
250251
Event::MessageCreate(ev) => {
251252
self.cmds.execute(cx, &ev.message);
252253
}
253254
Event::MessageUpdate(ev) => {
254-
let mut msg = CustomMessage::new();
255-
256-
msg.id(ev.id)
257-
.channel_id(ev.channel_id)
258-
.content(ev.content.unwrap_or_else(|| String::new()));
259-
260-
let msg = msg.build();
261-
info!("sending edited message - {:?}", msg.content);
262-
self.cmds.execute(cx, &msg);
255+
let age = ev.timestamp.and_then(|create| {
256+
ev.edited_timestamp
257+
.and_then(|edit| edit.signed_duration_since(create).to_std().ok())
258+
});
259+
260+
if age.is_some() && age.unwrap() < MESSAGE_AGE_MAX {
261+
let mut msg = CustomMessage::new();
262+
msg.id(ev.id)
263+
.channel_id(ev.channel_id)
264+
.content(ev.content.unwrap_or_else(|| String::new()));
265+
266+
let msg = msg.build();
267+
info!("sending edited message - {:?}", msg.content);
268+
self.cmds.execute(cx, &msg);
269+
}
263270
}
264271
Event::MessageDelete(ev) => {
265272
let mut data = cx.data.write();

0 commit comments

Comments
 (0)