From 19754d3bccd2d157c9e1e6b966a8d6b23b13c9c1 Mon Sep 17 00:00:00 2001 From: jellywx Date: Thu, 19 Nov 2020 21:31:31 +0000 Subject: [PATCH] fixed blacklist strings. fixed blacklist silent failing on occasion --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/moderation_cmds.rs | 58 ++++++++++++++++++++++++++------- src/models.rs | 9 ----- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4759615..1d44ac0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1175,7 +1175,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "1.2.1" +version = "1.2.2" dependencies = [ "Inflector", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index c6a8026..ee4fe8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "1.2.1" +version = "1.2.2" authors = ["jellywx "] edition = "2018" diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index d8d88d7..1b2526d 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -19,6 +19,7 @@ use crate::{ FrameworkCtx, SQLPool, }; +use serenity::model::id::ChannelId; use std::iter; #[command] @@ -34,28 +35,61 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) { .cloned() .expect("Could not get SQLPool from data"); + let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); + let capture_opt = REGEX_CHANNEL .captures(&args) .map(|cap| cap.get(1)) .flatten(); - let mut channel = match capture_opt { - Some(capture) => ChannelData::from_id(capture.as_str().parse::().unwrap(), &pool) - .await - .unwrap(), + let (channel, local) = match capture_opt { + Some(capture) => ( + ChannelId(capture.as_str().parse::().unwrap()) + .to_channel_cached(&ctx) + .await, + false, + ), - None => ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) - .await - .unwrap(), + None => (msg.channel(&ctx).await, true), }; - channel.blacklisted = !channel.blacklisted; - channel.commit_changes(&pool).await; + let mut channel_data = ChannelData::from_channel(channel.unwrap(), &pool) + .await + .unwrap(); - if channel.blacklisted { - let _ = msg.channel_id.say(&ctx, "Blacklisted").await; + channel_data.blacklisted = !channel_data.blacklisted; + channel_data.commit_changes(&pool).await; + + if channel_data.blacklisted { + if local { + let _ = msg + .channel_id + .say(&ctx, user_data.response(&pool, "blacklist/added").await) + .await; + } else { + let _ = msg + .channel_id + .say( + &ctx, + user_data.response(&pool, "blacklist/added_from").await, + ) + .await; + } } else { - let _ = msg.channel_id.say(&ctx, "Unblacklisted").await; + if local { + let _ = msg + .channel_id + .say(&ctx, user_data.response(&pool, "blacklist/removed").await) + .await; + } else { + let _ = msg + .channel_id + .say( + &ctx, + user_data.response(&pool, "blacklist/removed_from").await, + ) + .await; + } } } diff --git a/src/models.rs b/src/models.rs index 885c004..d273dad 100644 --- a/src/models.rs +++ b/src/models.rs @@ -118,15 +118,6 @@ pub struct ChannelData { } impl ChannelData { - pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option { - sqlx::query_as_unchecked!(Self, - " -SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ? - ", channel_id) - .fetch_one(pool) - .await.ok() - } - pub async fn from_channel( channel: Channel, pool: &MySqlPool,