2020-08-27 11:15:20 +00:00
|
|
|
use serenity::{
|
|
|
|
prelude::Context,
|
|
|
|
model::{
|
|
|
|
id::ChannelId,
|
|
|
|
guild::Guild,
|
|
|
|
channel::Channel,
|
|
|
|
user::User,
|
|
|
|
}
|
2020-08-22 00:24:12 +00:00
|
|
|
};
|
2020-08-18 19:09:21 +00:00
|
|
|
|
|
|
|
use sqlx::MySqlPool;
|
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
pub struct GuildData {
|
2020-08-18 19:09:21 +00:00
|
|
|
id: u32,
|
2020-08-22 00:24:12 +00:00
|
|
|
guild: u64,
|
2020-08-29 19:57:11 +00:00
|
|
|
pub name: String,
|
|
|
|
pub prefix: String,
|
2020-08-22 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GuildData {
|
2020-08-29 19:57:11 +00:00
|
|
|
pub async fn from_guild(guild: Guild, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
2020-08-25 14:09:33 +00:00
|
|
|
let guild_id = guild.id.as_u64().clone();
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
if let Ok(g) = sqlx::query_as!(Self,
|
2020-08-18 19:09:21 +00:00
|
|
|
"
|
2020-08-22 00:24:12 +00:00
|
|
|
SELECT id, guild, name, prefix FROM guilds WHERE guild = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", guild_id)
|
2020-08-18 19:09:21 +00:00
|
|
|
.fetch_one(&pool)
|
2020-08-22 00:24:12 +00:00
|
|
|
.await {
|
2020-08-18 19:09:21 +00:00
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
Ok(g)
|
2020-08-18 19:09:21 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sqlx::query!(
|
2020-08-22 00:24:12 +00:00
|
|
|
"
|
|
|
|
INSERT INTO guilds (guild, name) VALUES (?, ?)
|
2020-08-25 14:09:33 +00:00
|
|
|
", guild_id, guild.name)
|
2020-08-22 00:24:12 +00:00
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
|
|
|
|
2020-08-25 14:09:33 +00:00
|
|
|
Ok(sqlx::query_as!(Self,
|
2020-08-22 00:24:12 +00:00
|
|
|
"
|
|
|
|
SELECT id, guild, name, prefix FROM guilds WHERE guild = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", guild_id)
|
2020-08-22 00:24:12 +00:00
|
|
|
.fetch_one(&pool)
|
2020-08-25 14:09:33 +00:00
|
|
|
.await?)
|
2020-08-22 00:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:15:20 +00:00
|
|
|
pub struct ChannelData {
|
|
|
|
id: u32,
|
|
|
|
channel: u64,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
impl ChannelData {
|
2020-08-26 17:26:28 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-08-25 16:19:08 +00:00
|
|
|
pub async fn from_channel(channel: Channel, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
2020-08-25 14:09:33 +00:00
|
|
|
let channel_id = channel.id().as_u64().clone();
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
if let Ok(c) = sqlx::query_as_unchecked!(Self,
|
2020-08-18 19:09:21 +00:00
|
|
|
"
|
2020-08-22 00:24:12 +00:00
|
|
|
SELECT * FROM channels WHERE channel = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", channel_id)
|
2020-08-22 00:24:12 +00:00
|
|
|
.fetch_one(&pool)
|
|
|
|
.await {
|
|
|
|
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
else {
|
2020-08-25 14:09:33 +00:00
|
|
|
let props = channel.guild().map(|g| (g.guild_id.as_u64().clone(), g.name));
|
|
|
|
|
|
|
|
let (guild_id, channel_name) = if let Some((a, b)) = props {
|
|
|
|
(Some(a), Some(b))
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
2020-08-22 00:24:12 +00:00
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))
|
2020-08-25 14:09:33 +00:00
|
|
|
", channel_id, channel_name, guild_id)
|
2020-08-22 00:24:12 +00:00
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
|
|
|
|
2020-08-25 14:09:33 +00:00
|
|
|
Ok(sqlx::query_as_unchecked!(Self,
|
|
|
|
"
|
2020-08-22 00:24:12 +00:00
|
|
|
SELECT * FROM channels WHERE channel = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", channel_id)
|
|
|
|
.fetch_one(&pool)
|
|
|
|
.await?)
|
2020-08-18 19:09:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 00:24:12 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2020-08-18 19:09:21 +00:00
|
|
|
}
|
2020-08-27 11:15:20 +00:00
|
|
|
|
|
|
|
pub struct UserData {
|
|
|
|
id: u32,
|
|
|
|
pub user: u64,
|
|
|
|
pub name: String,
|
|
|
|
pub dm_channel: u32,
|
|
|
|
pub language: String,
|
|
|
|
pub timezone: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserData {
|
|
|
|
pub async fn from_id(user: &User, ctx: &&Context, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
|
|
|
let user_id = user.id.as_u64().clone();
|
|
|
|
|
|
|
|
if let Ok(c) = sqlx::query_as_unchecked!(Self,
|
|
|
|
"
|
|
|
|
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
|
|
|
|
", user_id)
|
|
|
|
.fetch_one(&pool)
|
|
|
|
.await {
|
|
|
|
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let dm_channel = user.create_dm_channel(ctx).await?;
|
|
|
|
let dm_id = dm_channel.id.as_u64().clone();
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO channels (channel) VALUES (?)
|
|
|
|
", dm_id)
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO users (user, name, dm_channel) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?))
|
|
|
|
", user_id, user.name, dm_id)
|
|
|
|
.execute(&pool)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(sqlx::query_as_unchecked!(Self,
|
|
|
|
"
|
|
|
|
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
|
|
|
|
", user_id)
|
|
|
|
.fetch_one(&pool)
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn commit_changes(&self, pool: MySqlPool) {
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
|
|
|
|
", self.name, self.language, self.timezone, self.id)
|
|
|
|
.execute(&pool)
|
|
|
|
.await.unwrap();
|
|
|
|
}
|
|
|
|
}
|