models stuff

This commit is contained in:
jude 2020-08-22 01:24:12 +01:00
parent e69264d726
commit 81e339c796
4 changed files with 95 additions and 21 deletions

1
Cargo.lock generated
View File

@ -1417,6 +1417,7 @@ dependencies = [
"bigdecimal",
"bitflags",
"byteorder",
"chrono",
"crossbeam-queue",
"crossbeam-utils",
"digest",

View File

@ -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"

View File

@ -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::<SQLPool>().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(())
}

View File

@ -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<u64>,
pub webhook_token: Option<String>,
pub paused: bool,
pub paused_until: Option<NaiveDateTime>,
guild_id: u32,
}
impl Channel {
async fn from_id(channel: u64, pool: MySqlPool) -> Result<Channel, Box<dyn std::error::Error + Sync + Send>> {
if let Some(c) = sqlx::query_as!(Self,
impl GuildData {
pub async fn from_id(guild: Guild, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
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 channels (channel, guild_id) VALUES ()
INSERT INTO guilds (guild, name) VALUES (?, ?)
", guild.id.as_u64(), guild.name)
.execute(&pool)
.await?;
sqlx::query_as!(Self,
"
)
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<Self, Box<dyn std::error::Error + Sync + Send>> {
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();
}
}