aboutsummaryrefslogtreecommitdiff
path: root/src/commands/filters.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/filters.rs')
-rw-r--r--src/commands/filters.rs79
1 files changed, 74 insertions, 5 deletions
diff --git a/src/commands/filters.rs b/src/commands/filters.rs
index 35fefac..5e8f900 100644
--- a/src/commands/filters.rs
+++ b/src/commands/filters.rs
@@ -1,5 +1,6 @@
-use crate::{Context, Error, models::{FilterAction, MessageFilter}};
+use crate::{Context, Error, models::{FilterAction, MessageFilter, ConfigChannel}};
use poise::{serenity_prelude as serenity, AutocompleteChoice};
+use regex::Regex;
use std::str::FromStr;
@@ -28,8 +29,8 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
if filters.len() == 0 {
list = "No filters set, try adding one with `/filter add`!".to_string();
} else {
- for f in filters {
- list.push_str(&format!("{}\n", f.to_string()));
+ for (i, f) in filters.iter().enumerate() {
+ list.push_str(&format!("**{}.** {}\n", (i + 1), f.to_string()));
}
}
@@ -49,7 +50,7 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
/// filter add <pattern> <action>
///
/// Where <action> is one of "review" or "delete"
-#[poise::command(slash_command, ephemeral, prefix_command)]
+#[poise::command(slash_command, ephemeral, prefix_command, check = "c_modify_filters")]
pub async fn add(ctx: Context<'_>,
#[description = "The regular expression to match against"]
regex: String,
@@ -62,8 +63,9 @@ pub async fn add(ctx: Context<'_>,
let action = FilterAction::from_str(&action)?;
+ let regex = Regex::new(&regex)?;
sqlx::query("INSERT INTO message_filter (pattern, action, guild_id) VALUES ($1, $2, $3)")
- .bind(regex)
+ .bind(regex.to_string())
.bind(action as FilterAction)
.bind(ctx.guild().unwrap().id.0.to_string())
.execute(&pool)
@@ -74,9 +76,76 @@ pub async fn add(ctx: Context<'_>,
Ok(())
}
+/// Deletes a message filter
+///
+/// Usage:
+/// filter del <id>
+///
+/// You can get the filter ID from `filter list`
+#[poise::command(slash_command, ephemeral, prefix_command, check = "c_modify_filters")]
+pub async fn del(ctx: Context<'_>,
+ #[description = "The ID to delete"]
+ id: i32,
+ ) -> Result<(), Error> {
+ let pool = ctx.data().pg.lock().unwrap().clone();
+
+ sqlx::query("DELETE FROM message_filter WHERE id=$1")
+ .bind(id)
+ .execute(&pool)
+ .await?;
+
+ ctx.say("Got it! I've removed that from my list.").await?;
+ Ok(())
+}
+
+/// Sets the review channel
+///
+/// Usage:
+/// filter channel <channel>
+#[poise::command(slash_command, ephemeral, prefix_command, check = "c_modify_filters")]
+pub async fn channel(ctx: Context<'_>,
+ #[description = "The channel to send flagged messages"]
+ channel_id: serenity::ChannelId,
+ ) -> Result<(), Error> {
+ let pool = ctx.data().pg.lock().unwrap().clone();
+
+ if let Ok(m) = sqlx::query_as!(ConfigChannel, "SELECT * FROM channels WHERE guild_id=$1 AND purpose='review'", ctx.guild().unwrap().id.0.to_string()).fetch_one(&pool).await {
+ sqlx::query!("UPDATE channels SET channel_id=$1 WHERE id=$2", channel_id.0.to_string(), m.id).execute(&pool).await?;
+ } else {
+ sqlx::query!("INSERT INTO channels (channel_id, purpose, guild_id) VALUES ($1, 'review', $2)", channel_id.0.to_string(), ctx.guild().unwrap().id.0.to_string())
+ .execute(&pool).await?;
+ }
+
+ ctx.say(format!("All set! I'll send flagged messages to <#{}> from now on", channel_id.0)).await?;
+ Ok(())
+}
+
async fn ac_action(_ctx: Context<'_>, partial: String) -> impl Stream<Item = String> {
futures::stream::iter(&["review", "delete"])
.filter(move |name| futures::future::ready(name.starts_with(&partial)))
.map(|name| name.to_string())
}
+
+#[cfg(debug_assertions)]
+async fn c_modify_filters(ctx: Context<'_>) -> Result<bool, Error> {
+ if ctx.author().id.0 == 118455061222260736u64 {
+ Ok(true)
+ } else {
+ Ok(false)
+ }
+}
+#[cfg(not(debug_assertions))]
+async fn c_modify_filters(ctx: Context<'_>) -> Result<bool, Error> {
+ if let Some(guild) = ctx.guild() {
+ if guild.owner_id == ctx.author().id {
+ Ok(true)
+ } else {
+ let member = guild.member(ctx.discord(), ctx.author().id).await?;
+ let member_permissions = member.permissions(ctx.discord())?;
+ Ok(member_permissions.manage_messages())
+ }
+ } else {
+ Ok(false)
+ }
+}