From 0395da0a5cc27124529733a9de1dc9a9a6ed2b9d Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 16 Oct 2020 18:05:39 +0200 Subject: [PATCH] implement democracy --- src/main.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 868c3c3..e044e2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,11 +12,12 @@ lazy_static! { static ref SERVER_ID: GuildId = GuildId(std::env::args().nth(1).unwrap().parse().unwrap()); static ref MEME_CHANNEL: ChannelId = ChannelId(std::env::args().nth(2).unwrap().parse().unwrap()); + static ref DELET_THIS_ID: EmojiId = EmojiId(323238442869719040); } impl EventHandler for Handler { fn message(&self, ctx: Context, message: Message) { - if message.guild_id != Some(*SERVER_ID) { + if message.guild_id != Some(*SERVER_ID) || message.channel_id != *MEME_CHANNEL { return; } if is_meme(&message) { @@ -31,6 +32,40 @@ impl EventHandler for Handler { react(&ctx, &message, 748564944819060856, "cringe"); } } + + fn reaction_add(&self, ctx: Context, added_reaction: Reaction) { + if is_delet_this(&added_reaction.emoji) { + let msg = added_reaction.message(&ctx).unwrap(); + if msg.channel_id != *MEME_CHANNEL + && is_meme(&msg) + && msg + .reactions + .iter() + .find(|r| is_delet_this(&r.reaction_type)) + .map(|r| r.count >= 5) + .unwrap_or(false) + { + println!( + "Deleting message {:?} with content {} and attachments {:?}", + msg, msg.content, msg.attachments + ); + if let Err(e) = msg.delete(&ctx) { + println!("Could not delete, error was: {:?}", e); + } + } + } + } +} + +fn is_delet_this(t: &ReactionType) -> bool { + match t { + ReactionType::Custom { + animated: _, + id, + name: _, + } => id == &*DELET_THIS_ID, + _ => false, + } } fn react(ctx: &Context, msg: &Message, emoji: u64, name: &str) { @@ -45,8 +80,7 @@ fn react(ctx: &Context, msg: &Message, emoji: u64, name: &str) { } fn is_meme(msg: &Message) -> bool { - msg.channel_id == *MEME_CHANNEL - && (!msg.attachments.is_empty() || msg.content.to_lowercase().contains("http")) + !msg.attachments.is_empty() || msg.content.to_lowercase().contains("http") } fn is_cringe(s: &str) -> bool {