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]]
name = "reminder_rs"
version = "1.2.1"
version = "1.2.2"
dependencies = [
"Inflector",
"async-trait",

View File

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

View File

@ -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::<u64>().unwrap(), &pool)
.await
.unwrap(),
let (channel, local) = match capture_opt {
Some(capture) => (
ChannelId(capture.as_str().parse::<u64>().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, "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 {
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(
channel: Channel,
pool: &MySqlPool,