diff --git a/src/patchbot_forwarder/commands.rs b/src/patchbot_forwarder/commands.rs index 3174b5a..778cf4f 100644 --- a/src/patchbot_forwarder/commands.rs +++ b/src/patchbot_forwarder/commands.rs @@ -1,9 +1,7 @@ -use crate::{ - message::{SendMessage, SendableMessage}, - patchbot_forwarder::helpers::insert_to_table, -}; use poise::serenity_prelude::Channel; +use crate::message::{SendMessage, SendableMessage}; +use crate::patchbot_forwarder::helpers::{delete_from_table, guild_rules, insert_to_table}; use crate::{PoiseContext, PoiseError}; /// Add rule to forward Patchbot messages to a different channel @@ -45,9 +43,19 @@ pub async fn patchbot_forward( required_permissions = "MANAGE_GUILD" )] pub async fn patchbot_list(ctx: PoiseContext<'_>) -> Result<(), PoiseError> { - SendMessage::Normal("Unimplemented, contact bot author") - .send_msg(ctx) - .await; + let rules = guild_rules(&ctx).await; + let mut text = String::new(); + for rule in rules { + text.push_str(&format!( + r#"{}) "{}" in <#{}> -> <#{}>"#, + rule.id, + rule.match_text, + u64::from_str_radix(&rule.source_channel_id, 16).unwrap(), + u64::from_str_radix(&rule.dest_channel_id, 16).unwrap() + )); + text.push('\n'); + } + SendMessage::Normal(text).send_msg(ctx).await; Ok(()) } @@ -58,9 +66,12 @@ pub async fn patchbot_list(ctx: PoiseContext<'_>) -> Result<(), PoiseError> { guild_only, required_permissions = "MANAGE_GUILD" )] -pub async fn patchbot_remove(ctx: PoiseContext<'_>) -> Result<(), PoiseError> { - SendMessage::Normal("Unimplemented, contact bot author") - .send_msg(ctx) - .await; +pub async fn patchbot_remove(ctx: PoiseContext<'_>, id: i64) -> Result<(), PoiseError> { + let deleted_rows = delete_from_table(&ctx, id).await; + if deleted_rows != 0 { + SendMessage::Normal("Deleted 1 rule").send_msg(ctx).await; + } else { + SendMessage::Error("No rules deleted").send_msg(ctx).await; + } Ok(()) } diff --git a/src/patchbot_forwarder/entity.rs b/src/patchbot_forwarder/entity.rs index 5540fd6..c41b266 100644 --- a/src/patchbot_forwarder/entity.rs +++ b/src/patchbot_forwarder/entity.rs @@ -11,13 +11,7 @@ pub struct Model { pub dest_channel_id: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/src/patchbot_forwarder/helpers.rs b/src/patchbot_forwarder/helpers.rs index af0228e..f5a6f1d 100644 --- a/src/patchbot_forwarder/helpers.rs +++ b/src/patchbot_forwarder/helpers.rs @@ -1,10 +1,10 @@ -use sea_orm::ActiveModelTrait; use sea_orm::ActiveValue::Set; -use sea_orm::{ConnectionTrait, Database, Schema}; +use sea_orm::{ + ActiveModelTrait, ColumnTrait, ConnectionTrait, Database, EntityTrait, QueryFilter, Schema, +}; -use super::entity::*; use crate::config::Config; -use crate::PoiseContext; +use crate::{patchbot_forwarder, PoiseContext}; pub async fn create_table(config: &Config) -> String { let uri = format!( @@ -15,7 +15,7 @@ pub async fn create_table(config: &Config) -> String { let backend = db.get_database_backend(); let schema = Schema::new(backend); - let mut stmt = schema.create_table_from_entity(Entity); + let mut stmt = schema.create_table_from_entity(patchbot_forwarder::entity::Entity); stmt.if_not_exists(); db.execute(backend.build(&stmt)).await.unwrap(); @@ -31,15 +31,45 @@ pub async fn insert_to_table( let db_uri = ctx.data().db_uri.as_str(); let db = Database::connect(db_uri).await.unwrap(); - let x = ActiveModel { - guild_id: Set(format!( - "{:x}", - ctx.guild_id().map(|x| *x.as_u64()).unwrap_or(0) - )), - source_channel_id: Set(format!("{:x}", source_channel_id)), + let x = patchbot_forwarder::entity::ActiveModel { + guild_id: Set(stringify(ctx.guild_id().map(|x| *x.as_u64()).unwrap_or(0))), + source_channel_id: Set(stringify(source_channel_id)), match_text: Set(match_text.to_owned()), - dest_channel_id: Set(format!("{:x}", dest_channel_id)), + dest_channel_id: Set(stringify(dest_channel_id)), ..Default::default() }; x.insert(&db).await.unwrap(); } + +pub async fn delete_from_table(ctx: &PoiseContext<'_>, id: i64) -> u64 { + let db_uri = ctx.data().db_uri.as_str(); + let db = Database::connect(db_uri).await.unwrap(); + + let res = patchbot_forwarder::entity::Entity::delete_by_id(id) + .filter( + patchbot_forwarder::entity::Column::GuildId + .eq(stringify!(ctx.guild_id().map(|x| *x.as_u64()).unwrap_or(0))), + ) + .exec(&db) + .await + .unwrap(); + res.rows_affected +} + +pub async fn guild_rules(ctx: &PoiseContext<'_>) -> Vec { + let db_uri = ctx.data().db_uri.as_str(); + let db = Database::connect(db_uri).await.unwrap(); + + patchbot_forwarder::entity::Entity::find() + .filter( + patchbot_forwarder::entity::Column::GuildId + .eq(stringify(ctx.guild_id().map(|x| *x.as_u64()).unwrap_or(0))), + ) + .all(&db) + .await + .unwrap() +} + +fn stringify(x: u64) -> String { + format!("{:x}", x) +}