Skip to content

Commit

Permalink
Implement /patchbot_list and /patchbot_remove
Browse files Browse the repository at this point in the history
  • Loading branch information
evanc577 committed Nov 2, 2023
1 parent 6d0a96e commit 2560fe4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
33 changes: 22 additions & 11 deletions src/patchbot_forwarder/commands.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(())
}

Expand All @@ -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(())
}
8 changes: 1 addition & 7 deletions src/patchbot_forwarder/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
54 changes: 42 additions & 12 deletions src/patchbot_forwarder/helpers.rs
Original file line number Diff line number Diff line change
@@ -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!(
Expand All @@ -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();

Expand All @@ -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<patchbot_forwarder::entity::Model> {
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)
}

0 comments on commit 2560fe4

Please sign in to comment.