diff --git a/Cargo.lock b/Cargo.lock index cdbe603..5c27e11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1417,6 +1417,7 @@ dependencies = [ "bigdecimal", "bitflags", "byteorder", + "chrono", "crossbeam-queue", "crossbeam-utils", "digest", diff --git a/Cargo.toml b/Cargo.toml index c6c7e71..9b117df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ serenity = {git = "https://github.com/acdenisSK/serenity", branch = "await_next" dotenv = "0.15" tokio = {version = "0.2.19", features = ["fs", "sync", "process", "io-util"]} reqwest = "0.10.6" -sqlx = {version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal"]} +sqlx = {version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal", "chrono"]} regex = "1.3.9" async-trait = "0.1.36" log = "0.4.11" diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 28c94a8..1bb0d1f 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -10,7 +10,10 @@ use serenity::{ framework::standard::CommandResult, }; -use crate::SQLPool; +use crate::{ + models::ChannelData, + SQLPool, +}; #[command] #[supports_dm(false)] @@ -19,7 +22,17 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) -> CommandResult let pool = ctx.data.read().await .get::().cloned().expect("Could not get SQLPool from data"); - if + let mut channel = ChannelData::from_id(msg.channel(&ctx), pool.clone()).await.unwrap(); + + channel.blacklisted = !channel.blacklisted; + channel.commit_changes(pool).await; + + if channel.blacklisted { + + } + else { + + } Ok(()) } diff --git a/src/models.rs b/src/models.rs index 61dd791..208d75d 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,37 +1,97 @@ +use serenity::model::{ + guild::Guild, + channel::Channel +}; use sqlx::MySqlPool; use chrono::NaiveDateTime; -struct Channel { +pub struct GuildData { + id: u32, + guild: u64, + name: String, + prefix: String, +} + +pub struct ChannelData { id: u32, channel: u64, - name: String, - nudge: i16, - blacklisted: bool, - webhook_id: u64, - webhook_token: String, - paused: bool, - paused_until: NaiveDateTime, + pub name: String, + pub nudge: i16, + pub blacklisted: bool, + pub webhook_id: Option, + pub webhook_token: Option, + pub paused: bool, + pub paused_until: Option, guild_id: u32, } -impl Channel { - async fn from_id(channel: u64, pool: MySqlPool) -> Result> { - if let Some(c) = sqlx::query_as!(Self, +impl GuildData { + pub async fn from_id(guild: Guild, pool: MySqlPool) -> Result> { + if let Ok(g) = sqlx::query_as!(Self, " -SELECT * FROM channels WHERE channel = ? - ", channel) +SELECT id, guild, name, prefix FROM guilds WHERE guild = ? + ", guild.id.as_u64()) .fetch_one(&pool) - .await? { + .await { - c + Ok(g) } else { sqlx::query!( + " +INSERT INTO guilds (guild, name) VALUES (?, ?) + ", guild.id.as_u64(), guild.name) + .execute(&pool) + .await?; + + sqlx::query_as!(Self, " -INSERT INTO channels (channel, guild_id) VALUES () - " - ) +SELECT id, guild, name, prefix FROM guilds WHERE guild = ? + ", guild.id.as_u64()) + .fetch_one(&pool) + .await } } } + +impl ChannelData { + pub async fn from_id(channel: Channel, pool: MySqlPool) -> Result> { + if let Ok(c) = sqlx::query_as_unchecked!(Self, + " +SELECT * FROM channels WHERE channel = ? + ", channel.id().as_u64()) + .fetch_one(&pool) + .await { + + Ok(c) + } + else { + let guild_id = channel.guild().map(|g| g.guild_id.as_u64()); + let channel_name = channel.guild().map(|g| g.name); + + sqlx::query!( + " +INSERT INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?)) + ", channel.id().as_u64(), channel_name, guild_id) + .execute(&pool) + .await?; + + sqlx::query_as_unchecked!(Self, + " +SELECT * FROM channels WHERE channel = ? + ", channel.id().as_u64()) + .fetch_one(&pool) + .await + } + } + + pub async fn commit_changes(&self, pool: MySqlPool) { + sqlx::query!( + " +UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?, paused = ?, paused_until = ? WHERE id = ? + ", self.name, self.nudge, self.blacklisted, self.webhook_id, self.webhook_token, self.paused, self.paused_until, self.id) + .execute(&pool) + .await.unwrap(); + } +}