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 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!( simple_responder!(
YellResponder, YellResponder,

View file

@ -1,9 +1,4 @@
use crate::simple_responder; 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!( simple_responder!(
BestDoctorResponder, BestDoctorResponder,

View file

@ -1,5 +1,6 @@
use futures::future::BoxFuture; use futures::future::BoxFuture;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::trace;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use rand::thread_rng; use rand::thread_rng;
use rand::Rng; use rand::Rng;
@ -9,7 +10,9 @@ use serenity::model::channel::Message;
use serenity::prelude::{Context, EventHandler}; use serenity::prelude::{Context, EventHandler};
lazy_static! { 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(); static ref DESU_REGEX: Regex = Regex::new("(?i:desu)|(?:です)").unwrap();
} }
@ -19,9 +22,15 @@ pub(crate) struct DesuResponder;
impl EventHandler for DesuResponder { impl EventHandler for DesuResponder {
async fn message(&self, ctx: Context, message: Message) { async fn message(&self, ctx: Context, message: Message) {
let content = message.content_safe(ctx.clone()).await; let content = message.content_safe(ctx.clone()).await;
if REGEX.is_match(&content) let fufufu_match = FUFUFU_REGEX.is_match(&content);
|| (DESU_REGEX.is_match(&content) && thread_rng().gen::<f32>() < 0.1) 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 = let random_action =
DESU_ACTIONS.choose(&mut thread_rng()).unwrap().clone()(ctx, message); DESU_ACTIONS.choose(&mut thread_rng()).unwrap().clone()(ctx, message);
random_action.await.unwrap(); 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 // $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 // above issue is blocked on rustc bug, see https://github.com/rust-lang/rust/issues/43081
($name:tt, $regex:expr, $phrase:tt) => { ($name:tt, $regex:expr, $phrase:tt) => {
lazy_static! { lazy_static::lazy_static! {
static ref REGEX: Regex = Regex::new($regex).unwrap(); #[derive(Debug)]
static ref REGEX: regex::Regex = regex::Regex::new($regex).unwrap();
} }
pub(crate) struct $name; pub(crate) struct $name;
#[async_trait] #[serenity::async_trait]
impl EventHandler for $name { impl serenity::prelude::EventHandler for $name {
async fn message(&self, ctx: Context, message: Message) { async fn message(
if REGEX.is_match(&message.content_safe(ctx.clone()).await) { &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(); message.channel_id.say(ctx, $phrase).await.unwrap();
} }
} }

View file

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