This commit is contained in:
Edward Shen 2020-05-02 13:45:50 -04:00
parent 9026acdc19
commit c85190b626
Signed by: edward
GPG key ID: 19182661E818369F
5 changed files with 30 additions and 20 deletions

View file

@ -1,9 +1,4 @@
use crate::simple_responder;
use lazy_static::lazy_static;
use regex::Regex;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::{Context, EventHandler};
simple_responder!(
YellResponder,

View file

@ -1,9 +1,4 @@
use crate::simple_responder;
use lazy_static::lazy_static;
use regex::Regex;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::{Context, EventHandler};
simple_responder!(
BestDoctorResponder,

View file

@ -1,5 +1,6 @@
use futures::future::BoxFuture;
use lazy_static::lazy_static;
use log::trace;
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;
@ -9,7 +10,9 @@ use serenity::model::channel::Message;
use serenity::prelude::{Context, EventHandler};
lazy_static! {
static ref REGEX: Regex = Regex::new("(?i:fu){3,}").unwrap();
#[derive(Debug)]
static ref FUFUFU_REGEX: Regex = Regex::new("(?i:fu){3,}").unwrap();
#[derive(Debug)]
static ref DESU_REGEX: Regex = Regex::new("(?i:desu)|(?:です)").unwrap();
}
@ -19,9 +22,15 @@ pub(crate) struct DesuResponder;
impl EventHandler for DesuResponder {
async fn message(&self, ctx: Context, message: Message) {
let content = message.content_safe(ctx.clone()).await;
if REGEX.is_match(&content)
|| (DESU_REGEX.is_match(&content) && thread_rng().gen::<f32>() < 0.1)
{
let fufufu_match = FUFUFU_REGEX.is_match(&content);
let desu_match = DESU_REGEX.is_match(&content) && thread_rng().gen::<f32>() < 0.1;
if fufufu_match || desu_match {
trace!(
"Responding to `{}` because one of the following matched: fufufu={}, desu={}",
content,
fufufu_match,
desu_match
);
let random_action =
DESU_ACTIONS.choose(&mut thread_rng()).unwrap().clone()(ctx, message);
random_action.await.unwrap();

View file

@ -42,16 +42,23 @@ macro_rules! simple_responder {
// $phrase should be `expr`, see https://github.com/dtolnay/async-trait/issues/46
// above issue is blocked on rustc bug, see https://github.com/rust-lang/rust/issues/43081
($name:tt, $regex:expr, $phrase:tt) => {
lazy_static! {
static ref REGEX: Regex = Regex::new($regex).unwrap();
lazy_static::lazy_static! {
#[derive(Debug)]
static ref REGEX: regex::Regex = regex::Regex::new($regex).unwrap();
}
pub(crate) struct $name;
#[async_trait]
impl EventHandler for $name {
async fn message(&self, ctx: Context, message: Message) {
if REGEX.is_match(&message.content_safe(ctx.clone()).await) {
#[serenity::async_trait]
impl serenity::prelude::EventHandler for $name {
async fn message(
&self,
ctx: serenity::prelude::Context,
message: serenity::model::channel::Message,
) {
let msg = message.content_safe(ctx.clone()).await;
if REGEX.is_match(&msg) {
log::trace!("{} matched regex {:?}", msg, REGEX);
message.channel_id.say(ctx, $phrase).await.unwrap();
}
}

View file

@ -1,3 +1,4 @@
use log::debug;
use serenity::prelude::TypeMapKey;
use sqlx::{
sqlite::{SqliteConnection, SqlitePool},
@ -45,11 +46,14 @@ async fn init_pool() -> Result<DbPool, Error> {
.execute(&pool)
.await?;
debug!("Table Heck exists or was created");
if sqlx::query!("SELECT count FROM Heck")
.fetch_all(&pool)
.await?
.is_empty()
{
debug!("No entries in Heck, inserting default one");
sqlx::query!("INSERT INTO Heck VALUES (1, 0)")
.execute(&pool)
.await?;