From 6704dcb51c9de5c66102600d06ffe637fd2db7c1 Mon Sep 17 00:00:00 2001 From: Nathan Ringo Date: Sun, 24 Nov 2024 11:04:29 -0600 Subject: Adds plugin to encourage the quitting of Twitter. Eh, it's a tradition at this point. --- src/lib.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3b11a48..d24d386 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,11 @@ mod labwatch; +mod quit_twitter; use serenity::{ - all::{ChannelId, Context, EventHandler, Ready}, + all::{ChannelId, Context, EventHandler, Message, Ready}, async_trait, }; -use std::{path::PathBuf, sync::Arc}; +use std::{ops::ControlFlow, path::PathBuf, sync::Arc}; pub struct Handler(Arc); @@ -24,4 +25,20 @@ impl EventHandler for Handler { async fn ready(&self, ctx: Context, _data_about_bot: Ready) { tokio::spawn(labwatch::start(self.0.clone(), ctx)); } + + async fn message(&self, ctx: Context, msg: Message) { + // Apologies for the macro, but Rust does some control-flow analysis on async functions + // that it's much easier to pass when all your calls are direct calls. + macro_rules! handlers { + ($($handler:expr),* $(,)?) => { + $(match $handler(&self.0, &ctx, &msg).await { + Ok(ControlFlow::Break(())) => return, + Ok(ControlFlow::Continue(())) => (), + Err(err) => log::error!("{err:?}"), + })* + }; + } + + handlers![quit_twitter::on_message]; + } } -- cgit v1.2.3