From 2cde012c15cbd8287aca6dc7f79f26c5d1ea3504 Mon Sep 17 00:00:00 2001 From: jude Date: Thu, 27 Aug 2020 12:15:20 +0100 Subject: [PATCH] userdata --- src/commands/moderation_cmds.rs | 15 ++++- src/models.rs | 97 +++++++++++++++++++++++++++------ 2 files changed, 94 insertions(+), 18 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index f2c4e88..afdaab7 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -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::().cloned().expect("Could not get SQLPool from data"); + + let user_data = UserData::from_id(&msg.author, &ctx, pool.clone()).await.unwrap(); + + Ok(()) +} diff --git a/src/models.rs b/src/models.rs index 94873f5..80304ca 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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, - pub webhook_token: Option, - pub paused: bool, - pub paused_until: Option, - guild_id: u32, -} - impl GuildData { pub async fn from_id(guild: Guild, pool: MySqlPool) -> Result> { 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, + pub webhook_token: Option, + pub paused: bool, + pub paused_until: Option, + guild_id: u32, +} + impl ChannelData { pub async fn from_id(channel_id: u64, pool: MySqlPool) -> Option { 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> { + 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(); + } +}