Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Nov 29, 2024
1 parent a60daa5 commit 5ee6912
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 197 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

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

79 changes: 38 additions & 41 deletions core/rust.bot_modules.core/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use poise::serenity_prelude::FullEvent;
use silverpelt::ar_event::{AntiraidEvent, EventHandlerContext};

Expand Down Expand Up @@ -55,11 +57,11 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
event_titlename,
"Discord".to_string(),
event.snake_case_name().to_uppercase(),
serde_json::to_value(event)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::to_value(event)?)),
false,
user_id.map(|u| u.to_string()),
),
Expand All @@ -72,11 +74,11 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new(
templating::event::Event::new(
event.event_titlename.clone(),
"Custom".to_string(),
event.event_name.clone(),
event.event_data.clone(),
templating::event::ArcOrNormal::Arc(Arc::new(event.event_data.clone())),
false,
None,
),
Expand All @@ -88,11 +90,11 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
"(Anti Raid) Sting Created".to_string(),
"StingCreate".to_string(),
"StingCreate".to_string(),
serde_json::to_value(&sting)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::to_value(&sting)?)),
false,
None,
),
Expand All @@ -106,11 +108,11 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
"(Anti Raid) Sting Expired".to_string(),
"StingExpire".to_string(),
"StingExpire".to_string(),
serde_json::to_value(&sting)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::to_value(&sting)?)),
false,
None,
),
Expand All @@ -124,11 +126,11 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
"(Anti Raid) Sting Deleted".to_string(),
"StingDelete".to_string(),
"StingDelete".to_string(),
serde_json::to_value(&sting)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::to_value(&sting)?)),
false,
None,
),
Expand All @@ -142,11 +144,13 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
"(Anti Raid) Punishment Created".to_string(),
"PunishmentCreate".to_string(),
"PunishmentCreate".to_string(),
serde_json::to_value(&punishment)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(
serde_json::to_value(&punishment)?.into(),
)),
false,
None,
),
Expand All @@ -160,11 +164,13 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new_arc(
templating::event::Event::new(
"(Anti Raid) Punishment Expired".to_string(),
"PunishmentExpire".to_string(),
"PunishmentExpire".to_string(),
serde_json::to_value(&punishment)?.into(),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::to_value(
&punishment,
)?)),
false,
None,
),
Expand All @@ -178,13 +184,14 @@ pub(crate) async fn event_listener<'a>(
dispatch(
ctx,
&ectx.data,
templating::event::CreateEventArc::new(
templating::event::Event::new(
"(Anti Raid) On Startup".to_string(),
"OnStartup".to_string(),
"OnStartup".to_string(),
serde_json::json!({
"targets": modified
}),
templating::event::ArcOrNormal::Arc(Arc::new(serde_json::json!({
"targets": modified
}
))),
false,
None,
),
Expand All @@ -203,31 +210,28 @@ pub(crate) async fn event_listener<'a>(
///
/// Special cases:
/// - If event_name is MESSAGE, then it must be an exact match to be dispatched AND must have a custom template declared for it. This is to avoid spam
pub(crate) async fn should_dispatch_event(
event_name: &str,
filters: &[String],
) -> Result<bool, silverpelt::Error> {
pub(crate) fn should_dispatch_event(event_name: &str, filters: &[String]) -> bool {
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);
return false;
}

return Ok(true);
return true;
}

// If empty, always return Ok
if filters.is_empty() {
return Ok(true);
return true;
}

Ok(filters.contains(&event_name.to_string()))
filters.contains(&event_name.to_string())
}

async fn dispatch(
ctx: &serenity::all::client::Context,
data: &silverpelt::data::Data,
event: templating::event::CreateEventArc,
event: templating::event::Event,
guild_id: serenity::model::id::GuildId,
) -> Result<(), silverpelt::Error> {
let templates = templating::cache::get_all_guild_templates(guild_id, &data.pool).await?;
Expand All @@ -236,9 +240,8 @@ async fn dispatch(
return Ok(());
}

for template in templates.iter() {
// Verify event dispatch
if !should_dispatch_event(&event.name, {
for template in templates.iter().filter(|template| {
should_dispatch_event(&event.name(), {
// False positive, unwrap_or_default cannot be used here as it moves the event out of the sink
#[allow(clippy::manual_unwrap_or_default)]
if let Some(ref events) = template.events {
Expand All @@ -247,18 +250,14 @@ async fn dispatch(
&[]
}
})
.await?
{
continue;
}

}) {
match templating::execute::<Option<()>>(
guild_id,
templating::Template::Named(template.name.clone()),
data.pool.clone(),
ctx.clone(),
data.reqwest.clone(),
event.into_event(),
event.clone(),
)
.await
{
Expand All @@ -268,7 +267,6 @@ async fn dispatch(
}
}
}

Ok(())
}

Expand Down Expand Up @@ -334,15 +332,14 @@ async fn dispatch_error(
data.pool.clone(),
ctx.clone(),
data.reqwest.clone(),
templating::event::CreateEventArc::new(
templating::event::Event::new(
"Error".to_string(),
"Error".to_string(),
"Error".to_string(),
error.into(),
templating::event::ArcOrNormal::Normal(error.into()),
false,
None,
)
.into_event(),
),
)
.await?;
}
Expand Down
4 changes: 2 additions & 2 deletions core/rust.rpc_server.bot/src/templating_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ pub(crate) async fn execute_template(
data.pool.clone(),
serenity_context.clone(),
data.reqwest.clone(),
templating::event::Event::new_normal(
templating::event::Event::new(
"(Anti-Raid) Template Execution".to_string(),
"AR/Virtual_ExecTemplate".to_string(),
"AR/Virtual_ExecTemplate".to_string(),
req.args,
templating::event::ArcOrNormal::Normal(req.args),
false,
Some(user_id.to_string()),
),
Expand Down
99 changes: 8 additions & 91 deletions core/rust.templating/src/lang_lua/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,62 +36,17 @@ impl<'de, T: serde::de::Deserialize<'de>> serde::de::Deserialize<'de> for ArcOrN
}
}

/// A create event struct that can be cloned and/or turned into an Event
pub struct CreateEventArc {
pub title: String,
pub base_name: String,
pub name: String,
pub data: Arc<serde_json::Value>,
pub is_deniable: bool,
pub author: Option<String>,
}

impl CreateEventArc {
/// Creates a new CreateEventArc with data already wrapped in an Arc
pub fn new_arc(
title: String,
base_name: String,
name: String,
data: Arc<serde_json::Value>,
is_deniable: bool,
author: Option<String>,
) -> Self {
Self {
title,
base_name,
name,
data,
is_deniable,
author,
impl<T: Clone> Clone for ArcOrNormal<T> {
fn clone(&self) -> Self {
match self {
ArcOrNormal::Arc(a) => ArcOrNormal::Arc(a.clone()),
ArcOrNormal::Normal(b) => ArcOrNormal::Normal(b.clone()),
}
}

/// Creates a new CreateEventArc with data that will be wrapped in an Arc
pub fn new(
title: String,
base_name: String,
name: String,
data: serde_json::Value,
is_deniable: bool,
author: Option<String>,
) -> Self {
Self::new_arc(title, base_name, name, Arc::new(data), is_deniable, author)
}

pub fn into_event(&self) -> Event {
Event::new(
self.title.clone(),
self.base_name.clone(),
self.name.clone(),
ArcOrNormal::Arc(self.data.clone()),
self.is_deniable,
self.author.clone(),
)
}
}

/// An `Event` is an object that can be passed to a Lua template
#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Event {
/// The title name of the event
title: String,
Expand All @@ -110,45 +65,7 @@ pub struct Event {
}

impl Event {
/// Creates a new event using boxing
pub fn new_normal(
title: String,
base_name: String,
name: String,
data: serde_json::Value,
is_deniable: bool,
author: Option<String>,
) -> Self {
Self::new(
title,
base_name,
name,
ArcOrNormal::Normal(data),
is_deniable,
author,
)
}

/// Creates a new event using an Arc
pub fn new_arc(
title: String,
base_name: String,
name: String,
data: Arc<serde_json::Value>,
is_deniable: bool,
author: Option<String>,
) -> Self {
Self::new(
title,
base_name,
name,
ArcOrNormal::Arc(data),
is_deniable,
author,
)
}

/// Create from ArcOrBox
/// Create a new Event
pub fn new(
title: String,
base_name: String,
Expand Down Expand Up @@ -197,7 +114,7 @@ impl LuaUserData for Event {
});
fields.add_field_method_get("data", |lua, this| {
log::info!("Event: Serializing data");
let v = lua.to_value(&*this.data)?;
let v = lua.to_value(&this.data)?;
Ok(v)
});
fields.add_field_method_get("is_deniable", |_, this| Ok(this.is_deniable));
Expand Down
Loading

0 comments on commit 5ee6912

Please sign in to comment.