Refactor event handlers to allow callers to override default manifold handlers
This commit is contained in:
parent
04cd52f7e4
commit
8602c883d3
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "manifold"
|
||||
version = "6.1.1"
|
||||
version = "7.0.0"
|
||||
authors = ["Lucy Bladen <admin@lbladen.uk>"]
|
||||
edition = "2021"
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use crate::models::user::UserInfo;
|
|||
|
||||
#[async_trait]
|
||||
pub trait EventHandler {
|
||||
async fn listen(ctx: &Context, framework_ctx: FrameworkContext<'_, ManifoldData, ManifoldError>, event: &Event<'_>) -> ManifoldResult<()>;
|
||||
async fn listen(ctx: &Context, framework_ctx: FrameworkContext<'_, ManifoldData, ManifoldError>, event: &Event<'_>) -> ManifoldResult<bool>;
|
||||
}
|
||||
|
||||
pub struct Handler {
|
||||
|
|
@ -20,7 +20,7 @@ pub struct Handler {
|
|||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn listen(ctx: &Context, framework_ctx: FrameworkContext<'_, ManifoldData, ManifoldError>, event: &Event<'_>) -> ManifoldResult<()> {
|
||||
async fn listen(ctx: &Context, framework_ctx: FrameworkContext<'_, ManifoldData, ManifoldError>, event: &Event<'_>) -> ManifoldResult<bool> {
|
||||
match event {
|
||||
Event::Ready { data_about_bot } => Handler::standard_startup(&ctx, &framework_ctx, data_about_bot).await,
|
||||
Event::GuildBanAddition { guild_id, banned_user } => Handler::ban_add(&ctx, &framework_ctx, guild_id, banned_user).await,
|
||||
|
|
@ -34,7 +34,7 @@ impl EventHandler for Handler {
|
|||
Event::Message { new_message } => Handler::message(&ctx, &framework_ctx, &new_message).await,
|
||||
Event::MessageDelete { channel_id, deleted_message_id, guild_id } => Handler::message_deleted(&ctx, &framework_ctx, channel_id, deleted_message_id, guild_id).await,
|
||||
Event::MessageUpdate { old_if_available, new, event: _event } => Handler::message_edited(&ctx, &framework_ctx, old_if_available, new).await,
|
||||
_ => Ok(())
|
||||
_ => Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +47,7 @@ impl Handler {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn standard_startup(ctx: &Context, framework_ctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, data_about_bot: &Ready) -> ManifoldResult<()> {
|
||||
pub async fn standard_startup(ctx: &Context, framework_ctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, data_about_bot: &Ready) -> ManifoldResult<bool> {
|
||||
let config = &framework_ctx.user_data().await.bot_config;
|
||||
let responses = &framework_ctx.user_data().await.responses;
|
||||
|
||||
|
|
@ -68,10 +68,10 @@ impl Handler {
|
|||
|
||||
config.channels.log.say(&ctx, greeting).await.expect("Couldn't message log channel!");
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn ban_add(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, banned_user: &User) -> ManifoldResult<()> {
|
||||
async fn ban_add(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, banned_user: &User) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -85,10 +85,10 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn ban_remove(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, unbanned_user: &User) -> ManifoldResult<()> {
|
||||
async fn ban_remove(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, unbanned_user: &User) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -102,10 +102,10 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn new_member(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, new_member: &Member) -> ManifoldResult<()> {
|
||||
async fn new_member(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, new_member: &Member) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -120,10 +120,10 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn member_leave(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, user: &User, _member_data_if_available: &Option<Member>) -> ManifoldResult<()> {
|
||||
async fn member_leave(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, user: &User, _member_data_if_available: &Option<Member>) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -137,15 +137,15 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn member_update(_ctx: &Context, _fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _old_if_available: &Option<Member>, _new: &Member) -> ManifoldResult<()> {
|
||||
async fn member_update(_ctx: &Context, _fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _old_if_available: &Option<Member>, _new: &Member) -> ManifoldResult<bool> {
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn new_role(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, new: &Role) -> ManifoldResult<()> {
|
||||
async fn new_role(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, new: &Role) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -164,11 +164,11 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
||||
async fn delete_role(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, removed_role_id: &RoleId, removed_role_data_if_available: &Option<Role>) -> ManifoldResult<()> {
|
||||
async fn delete_role(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _guild_id: &GuildId, removed_role_id: &RoleId, removed_role_data_if_available: &Option<Role>) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
log_channel.send_message(ctx, |f| {
|
||||
|
|
@ -197,16 +197,16 @@ impl Handler {
|
|||
})
|
||||
}).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
||||
async fn update_role(_ctx: &Context, _fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _old_data_if_available: &Option<Role>, _new: &Role) -> ManifoldResult<()> {
|
||||
async fn update_role(_ctx: &Context, _fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, _old_data_if_available: &Option<Role>, _new: &Role) -> ManifoldResult<bool> {
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn message(_ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, msg: &Message) -> ManifoldResult<()> {
|
||||
async fn message(_ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, msg: &Message) -> ManifoldResult<bool> {
|
||||
let userinfo = &mut fctx.user_data().await.user_info.lock().await;
|
||||
let db = &fctx.user_data().await.database;
|
||||
|
||||
|
|
@ -227,10 +227,10 @@ impl Handler {
|
|||
userinfo.insert(msg.author.id.as_u64().clone(), new_user);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn message_deleted(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, channel_id: &ChannelId, deleted_message_id: &MessageId, _guild_id: &Option<GuildId>) -> ManifoldResult<()> {
|
||||
async fn message_deleted(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, channel_id: &ChannelId, deleted_message_id: &MessageId, _guild_id: &Option<GuildId>) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
let channel = match ctx.cache.guild_channel(channel_id) {
|
||||
|
|
@ -279,10 +279,10 @@ impl Handler {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn message_edited(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, original: &Option<Message>, new_message: &Option<Message>) -> ManifoldResult<()> {
|
||||
async fn message_edited(ctx: &Context, fctx: &FrameworkContext<'_, ManifoldData, ManifoldError>, original: &Option<Message>, new_message: &Option<Message>) -> ManifoldResult<bool> {
|
||||
let log_channel = fctx.user_data().await.bot_config.channels.log;
|
||||
|
||||
if let Some(new) = new_message.as_ref() {
|
||||
|
|
@ -307,6 +307,6 @@ impl Handler {
|
|||
}).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,12 +101,15 @@ pub async fn prepare_client<T: EventHandler>(arguments: ArgMatches, intents: Gat
|
|||
let framework = poise::Framework::builder()
|
||||
.options(poise::FrameworkOptions {
|
||||
event_handler: |ctx, e, fctx, _| Box::pin(async move {
|
||||
match T::listen(ctx, fctx, e).await {
|
||||
Ok(b) => if b { return Ok(()) },
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
_ = Handler::listen(ctx, fctx, e).await;
|
||||
T::listen(ctx, fctx, e).await
|
||||
Ok(())
|
||||
}),
|
||||
pre_command: |ctx: ManifoldContext<'_>| Box::pin(async move {
|
||||
info!("Received command {} from {}", ctx.command().name, ctx.author().name);
|
||||
let _ = ctx.data().bot_config.channels.log.say(ctx, t!("logging.logged_command", command = ctx.command().name, name = ctx.author().name)).await;
|
||||
debug!("Received command {} from {}", ctx.command().name, ctx.author().name);
|
||||
}),
|
||||
commands: commands::collect_commands(injected_commands),
|
||||
prefix_options: poise::PrefixFrameworkOptions {
|
||||
|
|
|
|||
Loading…
Reference in New Issue