initial/old version

This commit is contained in:
kageru 2020-08-28 09:45:19 +02:00
commit 4de942bc45
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 81 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "basedbot"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
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"

68
src/main.rs Normal file
View File

@ -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<Instant> = 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");
}