blacklist command

This commit is contained in:
jude 2020-08-26 18:26:28 +01:00
parent afe3dbc525
commit ff09ccfd62
7 changed files with 31 additions and 2 deletions

1
Cargo.lock generated
View File

@ -1093,6 +1093,7 @@ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
"dotenv", "dotenv",
"lazy_static",
"log", "log",
"regex", "regex",
"regex_command_attr", "regex_command_attr",

View File

@ -15,6 +15,7 @@ async-trait = "0.1.36"
log = "0.4.11" log = "0.4.11"
chrono = "0.4" chrono = "0.4"
chrono-tz = "0.5" chrono-tz = "0.5"
lazy_static = "1.4.0"
[dependencies.regex_command_attr] [dependencies.regex_command_attr]
path = "./regex_command_attr" path = "./regex_command_attr"

View File

@ -14,6 +14,7 @@ use crate::THEME_COLOR;
#[command] #[command]
#[can_blacklist(false)]
async fn help(ctx: &Context, msg: &Message, _args: String) -> CommandResult { async fn help(ctx: &Context, msg: &Message, _args: String) -> CommandResult {
msg.channel_id.send_message(ctx, |m| m msg.channel_id.send_message(ctx, |m| m
.embed(|e| e .embed(|e| e

View File

@ -10,11 +10,17 @@ use serenity::{
framework::standard::CommandResult, framework::standard::CommandResult,
}; };
use regex::Regex;
use crate::{ use crate::{
models::ChannelData, models::ChannelData,
SQLPool, SQLPool,
}; };
lazy_static! {
static ref REGEX_CHANNEL: Regex = Regex::new(r#"^\s*<#(\d+)>\s*$"#).unwrap();
}
#[command] #[command]
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
@ -23,7 +29,15 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) -> CommandResult
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).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.clone()).await.unwrap(),
None =>
ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap(),
};
channel.blacklisted = !channel.blacklisted; channel.blacklisted = !channel.blacklisted;
channel.commit_changes(pool).await; channel.commit_changes(pool).await;

View File

@ -320,7 +320,7 @@ impl Framework for RegexFramework {
let command = self.commands.get(full_match.name("cmd").unwrap().as_str()).unwrap(); let command = self.commands.get(full_match.name("cmd").unwrap().as_str()).unwrap();
let channel_data = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool).await; let channel_data = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool).await;
if !command.can_blacklist || channel_data.map(|c| c.blacklisted).unwrap_or(false) { if !command.can_blacklist || !channel_data.map(|c| c.blacklisted).unwrap_or(false) {
let args = full_match.name("args") let args = full_match.name("args")
.map(|m| m.as_str()) .map(|m| m.as_str())
.unwrap_or("") .unwrap_or("")

View File

@ -1,3 +1,6 @@
#[macro_use]
extern crate lazy_static;
mod models; mod models;
mod framework; mod framework;
mod commands; mod commands;

View File

@ -59,6 +59,15 @@ SELECT id, guild, name, prefix FROM guilds WHERE guild = ?
} }
impl ChannelData { impl ChannelData {
pub async fn from_id(channel_id: u64, pool: MySqlPool) -> Option<Self> {
sqlx::query_as_unchecked!(Self,
"
SELECT * FROM channels WHERE channel = ?
", channel_id)
.fetch_one(&pool)
.await.ok()
}
pub async fn from_channel(channel: Channel, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> { pub async fn from_channel(channel: Channel, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let channel_id = channel.id().as_u64().clone(); let channel_id = channel.id().as_u64().clone();