Skip to content

Commit 22ca44c

Browse files
committed
feat(reminder): add logic for allow staff to ping a role from a reminder
1 parent 0d6ab6e commit 22ca44c

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use crate::prelude::errors::*;
2+
3+
pub async fn insert_reminder_optout(
4+
guild_id: i64,
5+
user_id: i64,
6+
role_id: i64,
7+
pool: &sqlx::SqlitePool,
8+
) -> ModmailResult<()> {
9+
sqlx::query!(
10+
r#"
11+
INSERT OR IGNORE INTO reminder_optouts (guild_id, user_id, role_id)
12+
VALUES (?, ?, ?)
13+
"#,
14+
guild_id,
15+
user_id,
16+
role_id
17+
)
18+
.execute(pool)
19+
.await?;
20+
21+
Ok(())
22+
}
23+
24+
pub async fn delete_reminder_optout(
25+
guild_id: i64,
26+
user_id: i64,
27+
role_id: i64,
28+
pool: &sqlx::SqlitePool,
29+
) -> ModmailResult<bool> {
30+
let result = sqlx::query!(
31+
r#"
32+
DELETE FROM reminder_optouts
33+
WHERE guild_id = ? AND user_id = ? AND role_id = ?
34+
"#,
35+
guild_id,
36+
user_id,
37+
role_id
38+
)
39+
.execute(pool)
40+
.await?;
41+
42+
Ok(result.rows_affected() > 0)
43+
}
44+
45+
pub async fn get_optouts_for_role(
46+
guild_id: i64,
47+
role_id: i64,
48+
pool: &sqlx::SqlitePool,
49+
) -> ModmailResult<Vec<i64>> {
50+
let rows = sqlx::query_scalar!(
51+
r#"
52+
SELECT user_id FROM reminder_optouts
53+
WHERE guild_id = ? AND role_id = ?
54+
"#,
55+
guild_id,
56+
role_id
57+
)
58+
.fetch_all(pool)
59+
.await?;
60+
61+
Ok(rows)
62+
}
63+
64+
pub async fn is_user_opted_out(
65+
guild_id: i64,
66+
user_id: i64,
67+
role_id: i64,
68+
pool: &sqlx::SqlitePool,
69+
) -> ModmailResult<bool> {
70+
let result = sqlx::query!(
71+
r#"
72+
SELECT id FROM reminder_optouts
73+
WHERE guild_id = ? AND user_id = ? AND role_id = ?
74+
"#,
75+
guild_id,
76+
user_id,
77+
role_id
78+
)
79+
.fetch_optional(pool)
80+
.await?;
81+
82+
Ok(result.is_some())
83+
}

0 commit comments

Comments
 (0)