From 4de942bc459c8dd807654461a84da939af313c8d Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 28 Aug 2020 09:45:19 +0200 Subject: [PATCH] initial/old version --- .gitignore | 1 + Cargo.toml | 12 ++++++++++ src/main.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fd44ef1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "basedbot" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serenity = "0.8.6" +lazy_static = "1.4.0" +regex = "1.3.9" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c588fc2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,68 @@ +use serenity::model::id::GuildId; +use serenity::model::prelude::*; +use serenity::prelude::*; +use serenity::Client; +use std::sync::Mutex; +use std::time::{Duration, Instant}; +#[macro_use] +extern crate lazy_static; + +struct Handler; + +static BASED_URL: &str = + "https://cdn.discordapp.com/attachments/246368272327507979/738083492474257538/based.jpg"; +static CRINGE_URL: &str = + "https://cdn.discordapp.com/attachments/246368272327507979/738083495162806282/cringe.jpg"; +static BOTH_URL: &str = + "https://cdn.discordapp.com/attachments/300723600632053761/739835069417390110/based.jpg"; +static COOLDOWN: Duration = Duration::from_secs(3600); + +lazy_static! { + static ref LAST_SENT: Mutex = Mutex::new(Instant::now() - COOLDOWN); + static ref SERVER_ID: GuildId = GuildId(std::env::args().nth(1).unwrap().parse().unwrap()); +} + +impl EventHandler for Handler { + fn message(&self, ctx: Context, message: Message) { + if let Some(msg) = optional_reply(&message) { + if let Err(e) = message.channel_id.say(&ctx, msg) { + println!("Could not send image, error was: {:?}", e); + } + } + } +} + +fn is_cringe(s: &str) -> bool { + s.contains("cringe") +} + +fn is_based(s: &str) -> bool { + (s.contains("based") || s.contains("basiert")) && !s.contains("based on") +} + +fn optional_reply(msg: &Message) -> Option<&str> { + if msg.guild_id == Some(*SERVER_ID) { + let url = match msg.content.to_lowercase() { + c if is_cringe(&c) && is_based(&c) => BOTH_URL, + c if is_based(&c) => BASED_URL, + c if is_cringe(&c) => CRINGE_URL, + _ => return None, + }; + let mut last = LAST_SENT.lock().unwrap(); + if last.elapsed() > COOLDOWN { + println!("Sending image at {:?}", Instant::now()); + *last = Instant::now(); + return Some(url); + } + } + None +} + +pub fn main() { + let mut client = Client::new( + std::env::var("DISCORD_TOKEN").expect("no token in environment"), + Handler, + ) + .expect("Could not create client"); + client.start().expect("could not start"); +}