fixed blacklist strings. fixed blacklist silent failing on occasion

This commit is contained in:
jellywx 2020-11-19 21:31:31 +00:00
parent 38e2767f99
commit 19754d3bcc
4 changed files with 48 additions and 23 deletions

2
Cargo.lock generated
View File

@ -1175,7 +1175,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.2.1" version = "1.2.2"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"async-trait", "async-trait",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.2.1" version = "1.2.2"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -19,6 +19,7 @@ use crate::{
FrameworkCtx, SQLPool, FrameworkCtx, SQLPool,
}; };
use serenity::model::id::ChannelId;
use std::iter; use std::iter;
#[command] #[command]
@ -34,28 +35,61 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
.cloned() .cloned()
.expect("Could not get SQLPool from data"); .expect("Could not get SQLPool from data");
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let capture_opt = REGEX_CHANNEL let capture_opt = REGEX_CHANNEL
.captures(&args) .captures(&args)
.map(|cap| cap.get(1)) .map(|cap| cap.get(1))
.flatten(); .flatten();
let mut channel = match capture_opt { let (channel, local) = match capture_opt {
Some(capture) => ChannelData::from_id(capture.as_str().parse::<u64>().unwrap(), &pool) Some(capture) => (
.await ChannelId(capture.as_str().parse::<u64>().unwrap())
.unwrap(), .to_channel_cached(&ctx)
.await,
false,
),
None => ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) None => (msg.channel(&ctx).await, true),
.await
.unwrap(),
}; };
channel.blacklisted = !channel.blacklisted; let mut channel_data = ChannelData::from_channel(channel.unwrap(), &pool)
channel.commit_changes(&pool).await; .await
.unwrap();
if channel.blacklisted { channel_data.blacklisted = !channel_data.blacklisted;
let _ = msg.channel_id.say(&ctx, "Blacklisted").await; 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 { } else {
let _ = msg.channel_id.say(&ctx, "Unblacklisted").await; let _ = msg
.channel_id
.say(
&ctx,
user_data.response(&pool, "blacklist/added_from").await,
)
.await;
}
} else {
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;
}
} }
} }

View File

@ -118,15 +118,6 @@ pub struct ChannelData {
} }
impl ChannelData { impl ChannelData {
pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option<Self> {
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( pub async fn from_channel(
channel: Channel, channel: Channel,
pool: &MySqlPool, pool: &MySqlPool,