From 2c97b9245d04161eb5f2960efa7d4461ff456dd6 Mon Sep 17 00:00:00 2001 From: tec <tec@ucc.gu.uwa.edu.au> Date: Sat, 1 Feb 2020 19:47:24 +0800 Subject: [PATCH] Improve !logreact function --- Cargo.toml | 1 + src/main.rs | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0dda58f..02f571c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ log = "^0.4.8" simplelog = "^0.7.4" toml = "^0.5.6" serde = "^1.0.104" +chrono = "^0.4.10" diff --git a/src/main.rs b/src/main.rs index d20a544..90497c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ extern crate simplelog; use simplelog::*; use std::fs::{read_to_string, File}; +use chrono::prelude::Utc; use serenity::{ model::{channel, channel::Message, gateway::Ready, guild::Member}, prelude::*, @@ -61,10 +62,13 @@ impl EventHandler for Handler { "cowsay" => { voting::Commands::cowsay(ctx, msg.clone(), message_content[1]); } - "logreact" => e!( - "Error sending message {:?}", - msg.channel_id.say(&ctx.http, "React to this to log the ID") - ), + "logreact" => { + e!("Error deleting logreact prompt: {:?}", msg.delete(&ctx)); + e!( + "Error sending message {:?}", + msg.channel_id.say(&ctx.http, "React to this to log the ID") + ) + } "help" => { let mut message = MessageBuilder::new(); message.push_line(format!( @@ -103,11 +107,30 @@ impl EventHandler for Handler { voting::reaction_add(ctx, add_reaction); } "logreact" => { + let react_user = add_reaction.user(&ctx).unwrap(); + if Utc::now().timestamp() - message.timestamp.timestamp() > 300 { + warn!( + "The logreact message {} just tried to use is too old", + react_user.name + ); + return; + } info!( - "The react {:?} just added is {:?}", - add_reaction.user(&ctx).unwrap().name, + "The react {} just added is {:?}", + react_user.name, add_reaction.emoji.as_data() ); + let mut msg = MessageBuilder::new(); + msg.push_italic(react_user.name); + msg.push(format!( + " wanted to know that {} is represented by ", + add_reaction.emoji, + )); + msg.push_mono(add_reaction.emoji.as_data()); + e!( + "Error sending message: {:?}", + message.channel_id.say(&ctx.http, msg.build()) + ); } _ => {} } -- GitLab