-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
209 additions
and
253 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
pub async fn punishment_expiry_task( | ||
ctx: &serenity::all::client::Context, | ||
) -> Result<(), silverpelt::Error> { | ||
let data = ctx.data::<silverpelt::data::Data>(); | ||
let pool = &data.pool; | ||
|
||
let punishments = silverpelt::punishments::GuildPunishment::get_expired(pool).await?; | ||
|
||
let mut set = tokio::task::JoinSet::new(); | ||
|
||
let shard_count = data.props.shard_count().await?.try_into()?; | ||
let shards = data.props.shards().await?; | ||
|
||
for punishment in punishments { | ||
let guild_id = punishment.guild_id; | ||
|
||
// Ensure shard id | ||
let shard_id = serenity::utils::shard_id(guild_id, shard_count); | ||
|
||
if !shards.contains(&shard_id) { | ||
continue; | ||
} | ||
|
||
// Dispatch event | ||
let event = silverpelt::ar_event::AntiraidEvent::PunishmentExpire(punishment); | ||
|
||
let event_handler_context = | ||
std::sync::Arc::new(silverpelt::ar_event::EventHandlerContext { | ||
event, | ||
guild_id, | ||
data: data.clone(), | ||
serenity_context: ctx.clone(), | ||
}); | ||
|
||
// Spawn task to dispatch event | ||
set.spawn(silverpelt::ar_event::dispatch_event_to_modules( | ||
event_handler_context, | ||
)); | ||
} | ||
|
||
while let Some(res) = set.join_next().await { | ||
match res { | ||
Ok(Ok(())) => {} | ||
Ok(Err(e)) => { | ||
log::error!("Error in temporary_punishment_task: {:?}", e); | ||
} | ||
Err(e) => { | ||
log::error!("Error in temporary_punishment_task: {}", e); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use silverpelt::ar_event::{AntiraidEvent, EventHandlerContext}; | ||
|
||
/// Temporary Punishments event listener | ||
pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), silverpelt::Error> { | ||
match ectx.event { | ||
AntiraidEvent::PunishmentExpire(ref punishment) => { | ||
let target_user_id = match punishment.target { | ||
silverpelt::punishments::PunishmentTarget::User(user_id) => user_id, | ||
_ => return Ok(()), | ||
}; | ||
|
||
let cache_http = botox::cache::CacheHttpImpl::from_ctx(&ectx.serenity_context); | ||
|
||
let bot_id = ectx.serenity_context.cache.current_user().id; | ||
|
||
let mut current_user = match sandwich_driver::member_in_guild( | ||
&cache_http, | ||
&ectx.data.reqwest, | ||
punishment.guild_id, | ||
bot_id, | ||
) | ||
.await? | ||
{ | ||
Some(user) => user, | ||
None => { | ||
sqlx::query!( | ||
"UPDATE punishments SET duration = NULL, handle_log = $1 WHERE id = $2", | ||
serde_json::json!({ | ||
"error": "Bot not in guild", | ||
}), | ||
punishment.id | ||
) | ||
.execute(&ectx.data.pool) | ||
.await?; | ||
|
||
return Ok(()); | ||
} | ||
}; | ||
|
||
let permissions = current_user.permissions(&ectx.serenity_context.cache)?; | ||
|
||
// Bot doesn't have permissions to unban | ||
if !permissions.ban_members() { | ||
sqlx::query!( | ||
"UPDATE punishments SET duration = NULL, handle_log = $1 WHERE id = $2", | ||
serde_json::json!({ | ||
"error": "Bot doesn't have permissions to unban", | ||
}), | ||
punishment.id | ||
) | ||
.execute(&ectx.data.pool) | ||
.await?; | ||
} | ||
|
||
let reason = format!( | ||
"Revert expired ban with reason={}, duration={:#?}", | ||
punishment.reason, punishment.duration | ||
); | ||
|
||
let punishment_actions = silverpelt::punishments::get_punishment_actions_for_guild( | ||
punishment.guild_id, | ||
&ectx.data, | ||
) | ||
.await?; | ||
|
||
let cpa_revert = silverpelt::punishments::from_punishment_action_string( | ||
&punishment_actions, | ||
&punishment.punishment, | ||
)?; | ||
|
||
cpa_revert | ||
.revert( | ||
&silverpelt::punishments::PunishmentActionData { | ||
cache_http, | ||
pool: ectx.data.pool.clone(), | ||
reqwest: ectx.data.reqwest.clone(), | ||
object_store: ectx.data.object_store.clone(), | ||
}, | ||
target_user_id, | ||
&mut current_user, | ||
reason, | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
_ => { | ||
Ok(()) // Ignore non-discord events | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.