This commit is contained in:
jude 2020-08-27 12:15:20 +01:00
parent ff09ccfd62
commit 2cde012c15
2 changed files with 94 additions and 18 deletions

View File

@ -13,7 +13,10 @@ use serenity::{
use regex::Regex;
use crate::{
models::ChannelData,
models::{
ChannelData,
UserData,
},
SQLPool,
};
@ -51,3 +54,13 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) -> CommandResult
Ok(())
}
#[command]
async fn timezone(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap();
Ok(())
}

View File

@ -1,7 +1,11 @@
use serenity::model::{
id::ChannelId,
guild::Guild,
channel::Channel
use serenity::{
prelude::Context,
model::{
id::ChannelId,
guild::Guild,
channel::Channel,
user::User,
}
};
use sqlx::MySqlPool;
@ -14,19 +18,6 @@ pub struct GuildData {
prefix: String,
}
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,
}
impl GuildData {
pub async fn from_id(guild: Guild, pool: MySqlPool) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let guild_id = guild.id.as_u64().clone();
@ -58,6 +49,19 @@ SELECT id, guild, name, prefix FROM guilds WHERE guild = ?
}
}
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,
}
impl ChannelData {
pub async fn from_id(channel_id: u64, pool: MySqlPool) -> Option<Self> {
sqlx::query_as_unchecked!(Self,
@ -114,3 +118,62 @@ UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhoo
.await.unwrap();
}
}
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();
}
}