Skip to content

Commit

Permalink
Add HTM persistence, move migrations file to crate root (#510)
Browse files Browse the repository at this point in the history
* Add HTM persistence, move migrations file to crate root

* Ensure sqlx files exist

* Remove space after newline in htm persistence message
  • Loading branch information
elkowar authored Nov 23, 2023
1 parent 4484537 commit 1024b47
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 4 deletions.

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

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

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

Binary file modified base.db
Binary file not shown.
23 changes: 22 additions & 1 deletion crates/robbb/src/events/guild_member_addition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ use robbb_util::{
};
use std::time::SystemTime;

async fn handle_htm_evasion(ctx: &client::Context, new_member: &mut Member) -> Result<()> {
let (config, db) = ctx.get_config_and_db().await;
let is_htm = db.check_user_htm(new_member.user.id).await?;
if is_htm {
config
.channel_modlog
.send_embed(&ctx, |e| {
e.author(|a| a.name("HTM evasion caught").icon_url(new_member.user.face()));
e.title(new_member.user.name_with_disc_and_id());
e.description(format!(
"User {} was HTM and rejoined.\nRe-applying HTM role.",
new_member.mention()
));
})
.await?;
new_member.add_role(&ctx, config.role_htm).await?;
}
Ok(())
}

/// check if there's an active mute of a user that just joined.
/// if so, reapply the mute and log their mute-evasion attempt in modlog
async fn handle_mute_evasion(ctx: &client::Context, new_member: &Member) -> Result<()> {
Expand All @@ -33,12 +53,13 @@ async fn handle_mute_evasion(ctx: &client::Context, new_member: &Member) -> Resu
Ok(())
}

pub async fn guild_member_addition(ctx: client::Context, new_member: Member) -> Result<()> {
pub async fn guild_member_addition(ctx: client::Context, mut new_member: Member) -> Result<()> {
let config = ctx.get_config().await;
if config.guild != new_member.guild_id {
return Ok(());
}

log_error!(handle_htm_evasion(&ctx, &mut new_member).await);
log_error!(handle_mute_evasion(&ctx, &new_member).await);

config
Expand Down
9 changes: 8 additions & 1 deletion crates/robbb/src/events/guild_member_removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ pub async fn guild_member_removal(
ctx: client::Context,
guild_id: GuildId,
user: User,
_member: Option<Member>,
member: Option<Member>,
) -> Result<()> {
let db: Arc<Db> = ctx.get_db().await;
let config = ctx.get_config().await;
if config.guild != guild_id {
return Ok(());
}

if let Some(member) = member {
let roles = member.roles(&ctx).unwrap_or_default();
if roles.iter().any(|x| x.id == config.role_htm) {
log_error!(db.add_htm(member.user.id).await);
}
}

config
.channel_bot_traffic
.send_embed(&ctx, |e| {
Expand Down
9 changes: 8 additions & 1 deletion crates/robbb/src/events/guild_member_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ pub async fn guild_member_update(
_old: Option<Member>,
new: Member,
) -> Result<()> {
dehoist_member(ctx, new).await?;
let (config, db) = ctx.get_config_and_db().await;
dehoist_member(ctx.clone(), new.clone()).await?;

let roles = new.roles(&ctx).unwrap_or_default();
if roles.iter().any(|x| x.id == config.role_htm) {
log_error!(db.add_htm(new.user.id).await);
}

Ok(())
}

Expand Down
37 changes: 37 additions & 0 deletions crates/robbb_db/src/db/htm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use serenity::model::id::UserId;

use crate::Db;

#[derive(Debug)]
pub struct HardToModerateEntry {
pub user: UserId,
}

impl Db {
#[tracing::instrument(skip_all)]
pub async fn check_user_htm(&self, id: UserId) -> anyhow::Result<bool> {
let id = id.0 as i64;
Ok(sqlx::query!(r#"select * from hard_to_moderate where usr=?"#, id)
.fetch_optional(&self.pool)
.await?
.is_some())
}

#[tracing::instrument(skip_all)]
pub async fn add_htm(&self, id: UserId) -> anyhow::Result<()> {
let id = id.0 as i64;
sqlx::query!(r#"insert or ignore into hard_to_moderate (usr) values (?)"#, id)
.fetch_optional(&self.pool)
.await?;
Ok(())
}

#[tracing::instrument(skip_all)]
pub async fn remove_htm(&self, id: UserId) -> anyhow::Result<()> {
let id = id.0 as i64;
sqlx::query!(r#"delete from hard_to_moderate where usr=?"#, id)
.fetch_optional(&self.pool)
.await?;
Ok(())
}
}
3 changes: 2 additions & 1 deletion crates/robbb_db/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod emoji_logging;
pub mod fetch;
pub mod fetch_field;
pub mod highlights;
pub mod htm;
pub mod mod_action;
pub mod mute;
pub mod tag;
Expand Down Expand Up @@ -41,7 +42,7 @@ impl Db {
}

pub async fn run_migrations(&self) -> Result<()> {
sqlx::migrate!("./migrations")
sqlx::migrate!("../../migrations")
.run(&self.pool)
.await
.context("Failed to run database migrations")?;
Expand Down
2 changes: 2 additions & 0 deletions crates/robbb_util/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Config {
pub role_mod: RoleId,
pub role_helper: RoleId,
pub role_mute: RoleId,
pub role_htm: RoleId,
pub roles_color: Vec<RoleId>,

pub category_mod_private: ChannelId,
Expand Down Expand Up @@ -47,6 +48,7 @@ impl Config {
role_mod: RoleId(parse_required_env_var("ROLE_MOD")?),
role_helper: RoleId(parse_required_env_var("ROLE_HELPER")?),
role_mute: RoleId(parse_required_env_var("ROLE_MUTE")?),
role_htm: RoleId(parse_required_env_var("ROLE_HTM")?),
roles_color: required_env_var("ROLES_COLOR")?
.split(',')
.map(|x| Ok(RoleId(x.trim().parse()?)))
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions migrations/20231120193725_Add_hard_to_moderate_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS hard_to_moderate (
usr integer primary key
);

0 comments on commit 1024b47

Please sign in to comment.