Skip to content

Commit

Permalink
remove tags and templating modules as theyre useless, add OnStartup f…
Browse files Browse the repository at this point in the history
…unction to prepare for template settings and adding back afk and inspector as templates
  • Loading branch information
www committed Nov 18, 2024
1 parent f0a0432 commit 2e6c161
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 589 deletions.
57 changes: 1 addition & 56 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/rust.binutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ log = "0.4"
silverpelt = { path = "../rust.silverpelt" }
permission_checks = { path = "../rust.permissions.checks" }
config = { path = "../rust.config" }
templating = { path = "../rust.templating" }

[dependencies.serenity]
git = "https://github.com/Anti-Raid/serenity"
Expand Down
29 changes: 29 additions & 0 deletions core/rust.binutils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
use log::{error, info};
use silverpelt::{data::Data, Context, Error};

/// Code to call on startup
pub async fn on_startup(ctx: serenity::all::Context) -> Result<(), crate::Error> {
let data = ctx.data::<Data>();
// For every GuildId with templates, fire a OnStartup event
let guilds = sqlx::query!("SELECT guild_id FROM guild_templates")
.fetch_all(&data.pool)
.await?;

for guild in guilds {
let Ok(guild_id) = guild.guild_id.parse::<serenity::all::GuildId>() else {
continue;
};

templating::cache::clear_template_cache(guild_id).await;

let _ = silverpelt::ar_event::dispatch_event_to_modules_errflatten(std::sync::Arc::new(
silverpelt::ar_event::EventHandlerContext {
guild_id,
data: data.clone(),
event: silverpelt::ar_event::AntiraidEvent::OnStartup(vec![]),
serenity_context: ctx.clone(),
},
))
.await;
}

Ok(())
}

/// Standard error handler for Anti-Raid
pub async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
match error {
Expand Down
42 changes: 41 additions & 1 deletion core/rust.bot_modules.core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ pub static GUILD_TEMPLATES: LazyLock<ConfigOption> = LazyLock::new(|| {
},
},
validator: settings_wrap(GuildTemplateValidator {}),
post_action: settings_wrap(NoOpPostAction {}),
post_action: settings_wrap(GuildTemplatePostAction {}),
}
});

Expand Down Expand Up @@ -1065,6 +1065,46 @@ impl SettingDataValidator for GuildTemplateValidator {
}
}

pub struct GuildTemplatePostAction;

#[async_trait::async_trait]
impl PostAction for GuildTemplatePostAction {
async fn post_action<'a>(&self, context: HookContext<'a> , state: &'a mut module_settings::state::State) -> Result<(), SettingsError> {
if context.operation_type == OperationType::View {
return Ok(())
}

// Dispatch a OnStartup event for the template

// Get template ID
let Some(Value::String(name)) = state.state.get("name") else {
return Err(SettingsError::MissingOrInvalidField {
field: "name".to_string(),
src: "guild_templates->name".to_string(),
});
};

templating::cache::clear_template_cache(context.guild_id).await;

silverpelt::ar_event::dispatch_event_to_modules_errflatten(std::sync::Arc::new(
silverpelt::ar_event::EventHandlerContext {
guild_id: context.guild_id,
data: silverpelt::data::Data::get_data(context.data),
event: silverpelt::ar_event::AntiraidEvent::OnStartup(vec![name.to_string()]),
serenity_context: context.data.serenity_context.clone(),
},
))
.await
.map_err(|e| SettingsError::Generic {
message: format!("Failed to dispatch OnStartup event: {:?}", e),
src: "guild_templates->post_action".to_string(),
typ: "internal".to_string(),
})?;

Ok(())
}
}

pub static GUILD_TEMPLATES_KV: LazyLock<ConfigOption> = LazyLock::new(|| {
ConfigOption {
id: "guild_templates_kv",
Expand Down
17 changes: 15 additions & 2 deletions core/rust.bot_modules.hooks/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
return Ok(());
}

// (hopefully temporary) work around to reduce spam
// Ignore ourselves
match event {
FullEvent::GuildAuditLogEntryCreate { .. } => {}
_ => match gwevent::core::get_event_user_id(event) {
Expand Down Expand Up @@ -171,6 +171,19 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil

Ok(())
}
AntiraidEvent::OnStartup(ref modified) => {
dispatch_audit_log(
ctx,
&ectx.data,
"AR/OnStartup",
"(Anti Raid) On Startup",
serde_json::json!({
"targets": modified
}),
ectx.guild_id,
)
.await
}
}
}

Expand All @@ -186,7 +199,7 @@ pub(crate) async fn should_dispatch_event(
event_name: &str,
filters: &[String],
) -> Result<bool, silverpelt::Error> {
if event_name == "MESSAGE" || event_name == "AR/CheckCommand" {
if event_name == "MESSAGE" || event_name == "AR/CheckCommand" || event_name == "AR/OnStartup" {
// Message should only be fired if the template explicitly wants the event
if !filters.contains(&event_name.to_string()) {
return Ok(false);
Expand Down
1 change: 1 addition & 0 deletions core/rust.bot_modules.hooks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl silverpelt::module::ModuleEventListeners for EventHandler {
silverpelt::ar_event::AntiraidEvent::StingDelete(_) => true,
silverpelt::ar_event::AntiraidEvent::PunishmentCreate(_) => true,
silverpelt::ar_event::AntiraidEvent::PunishmentExpire(_) => true,
silverpelt::ar_event::AntiraidEvent::OnStartup(_) => true,
}
}
}
41 changes: 0 additions & 41 deletions core/rust.bot_modules.tags/Cargo.toml

This file was deleted.

Loading

0 comments on commit 2e6c161

Please sign in to comment.