2020-08-27 11:15:20 +00:00
|
|
|
use serenity::{
|
2020-09-15 13:43:49 +00:00
|
|
|
http::CacheHttp,
|
2020-10-12 20:01:27 +00:00
|
|
|
model::{channel::Channel, guild::Guild, id::GuildId, user::User},
|
2020-08-22 00:24:12 +00:00
|
|
|
};
|
2020-08-18 19:09:21 +00:00
|
|
|
|
2020-09-28 12:42:20 +00:00
|
|
|
use std::env;
|
|
|
|
|
2020-10-12 22:17:47 +00:00
|
|
|
use sqlx::{Cursor, MySqlPool, Row};
|
2020-08-18 19:09:21 +00:00
|
|
|
|
2020-09-02 16:13:17 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2020-09-01 17:18:45 +00:00
|
|
|
use chrono_tz::Tz;
|
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
use log::error;
|
|
|
|
|
2020-10-13 12:58:34 +00:00
|
|
|
use crate::consts::{LOCAL_LANGUAGE, LOCAL_TIMEZONE, PREFIX, STRINGS_TABLE};
|
2020-09-28 12:42:20 +00:00
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
pub struct GuildData {
|
2020-09-01 17:18:45 +00:00
|
|
|
pub id: u32,
|
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-10-12 20:01:27 +00:00
|
|
|
pub async fn prefix_from_id<T: Into<GuildId>>(
|
|
|
|
guild_id_opt: Option<T>,
|
|
|
|
pool: &MySqlPool,
|
|
|
|
) -> String {
|
2020-09-27 21:57:55 +00:00
|
|
|
if let Some(guild_id) = guild_id_opt {
|
|
|
|
let guild_id = guild_id.into().as_u64().to_owned();
|
|
|
|
|
|
|
|
let row = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT prefix FROM guilds WHERE guild = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id
|
2020-09-27 21:57:55 +00:00
|
|
|
)
|
2020-10-12 20:01:27 +00:00
|
|
|
.fetch_one(pool)
|
|
|
|
.await;
|
2020-09-27 21:57:55 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
row.map_or_else(
|
|
|
|
|_| env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string()),
|
|
|
|
|r| r.prefix,
|
|
|
|
)
|
|
|
|
} else {
|
2020-09-28 12:42:20 +00:00
|
|
|
env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string())
|
2020-09-27 21:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
pub async fn from_guild(
|
|
|
|
guild: Guild,
|
|
|
|
pool: &MySqlPool,
|
|
|
|
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
2020-09-25 22:07:22 +00:00
|
|
|
let guild_id = guild.id.as_u64().to_owned();
|
2020-08-25 14:09:33 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
if let Ok(g) = sqlx::query_as!(
|
|
|
|
Self,
|
2020-08-18 19:09:21 +00:00
|
|
|
"
|
2020-09-28 12:42:20 +00:00
|
|
|
SELECT id, name, prefix FROM guilds WHERE guild = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id
|
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
{
|
2020-08-22 00:24:12 +00:00
|
|
|
Ok(g)
|
2020-10-12 20:01:27 +00:00
|
|
|
} else {
|
2020-08-18 19:09:21 +00:00
|
|
|
sqlx::query!(
|
2020-08-22 00:24:12 +00:00
|
|
|
"
|
2020-09-28 12:42:20 +00:00
|
|
|
INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id,
|
|
|
|
guild.name,
|
|
|
|
env::var("DEFAULT_PREFIX").unwrap_or_else(|_| PREFIX.to_string())
|
|
|
|
)
|
|
|
|
.execute(&pool.clone())
|
|
|
|
.await?;
|
2020-08-22 00:24:12 +00:00
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
Ok(sqlx::query_as!(
|
|
|
|
Self,
|
|
|
|
"
|
2020-09-28 12:42:20 +00:00
|
|
|
SELECT id, name, prefix FROM guilds WHERE guild = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
guild_id
|
|
|
|
)
|
2020-09-01 16:07:51 +00:00
|
|
|
.fetch_one(pool)
|
2020-08-25 14:09:33 +00:00
|
|
|
.await?)
|
2020-08-22 00:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-30 20:08:08 +00:00
|
|
|
|
2020-09-01 16:07:51 +00:00
|
|
|
pub async fn commit_changes(&self, pool: &MySqlPool) {
|
2020-08-30 20:08:08 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
UPDATE guilds SET name = ?, prefix = ? WHERE id = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
self.name,
|
|
|
|
self.prefix,
|
|
|
|
self.id
|
|
|
|
)
|
|
|
|
.execute(pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-08-30 20:08:08 +00:00
|
|
|
}
|
2020-08-22 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 11:15:20 +00:00
|
|
|
pub struct ChannelData {
|
2020-09-11 16:41:15 +00:00
|
|
|
pub id: u32,
|
2020-08-27 11:15:20 +00:00
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
2020-08-22 00:24:12 +00:00
|
|
|
impl ChannelData {
|
2020-09-01 16:07:51 +00:00
|
|
|
pub async fn from_id(channel_id: u64, pool: &MySqlPool) -> Option<Self> {
|
2020-08-26 17:26:28 +00:00
|
|
|
sqlx::query_as_unchecked!(Self,
|
|
|
|
"
|
2020-09-28 12:42:20 +00:00
|
|
|
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
|
2020-08-26 17:26:28 +00:00
|
|
|
", channel_id)
|
2020-09-01 16:07:51 +00:00
|
|
|
.fetch_one(pool)
|
2020-08-26 17:26:28 +00:00
|
|
|
.await.ok()
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:01:27 +00:00
|
|
|
pub async fn from_channel(
|
|
|
|
channel: Channel,
|
|
|
|
pool: &MySqlPool,
|
|
|
|
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
2020-09-25 22:07:22 +00:00
|
|
|
let channel_id = channel.id().as_u64().to_owned();
|
2020-08-25 14:09:33 +00:00
|
|
|
|
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-09-28 12:42:20 +00:00
|
|
|
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", channel_id)
|
2020-09-01 16:07:51 +00:00
|
|
|
.fetch_one(pool)
|
2020-08-22 00:24:12 +00:00
|
|
|
.await {
|
|
|
|
|
|
|
|
Ok(c)
|
|
|
|
}
|
|
|
|
else {
|
2020-09-25 22:07:22 +00:00
|
|
|
let props = channel.guild().map(|g| (g.guild_id.as_u64().to_owned(), g.name));
|
2020-08-25 14:09:33 +00:00
|
|
|
|
|
|
|
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-09-01 16:07:51 +00:00
|
|
|
.execute(&pool.clone())
|
2020-08-22 00:24:12 +00:00
|
|
|
.await?;
|
|
|
|
|
2020-08-25 14:09:33 +00:00
|
|
|
Ok(sqlx::query_as_unchecked!(Self,
|
|
|
|
"
|
2020-09-28 12:42:20 +00:00
|
|
|
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
|
2020-08-25 14:09:33 +00:00
|
|
|
", channel_id)
|
2020-09-01 16:07:51 +00:00
|
|
|
.fetch_one(pool)
|
2020-08-25 14:09:33 +00:00
|
|
|
.await?)
|
2020-08-18 19:09:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 00:24:12 +00:00
|
|
|
|
2020-09-01 16:07:51 +00:00
|
|
|
pub async fn commit_changes(&self, pool: &MySqlPool) {
|
2020-08-22 00:24:12 +00:00
|
|
|
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)
|
2020-09-01 16:07:51 +00:00
|
|
|
.execute(pool)
|
2020-08-22 00:24:12 +00:00
|
|
|
.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 {
|
2020-10-12 20:01:27 +00:00
|
|
|
pub async fn from_user(
|
|
|
|
user: &User,
|
|
|
|
ctx: impl CacheHttp,
|
|
|
|
pool: &MySqlPool,
|
|
|
|
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
|
2020-09-25 22:07:22 +00:00
|
|
|
let user_id = user.id.as_u64().to_owned();
|
2020-08-27 11:15:20 +00:00
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
match sqlx::query_as_unchecked!(
|
2020-10-12 20:01:27 +00:00
|
|
|
Self,
|
2020-08-27 11:15:20 +00:00
|
|
|
"
|
2020-10-13 12:58:34 +00:00
|
|
|
SELECT id, user, name, dm_channel, IF(language IS NULL, ?, language) AS language, IF(timezone IS NULL, ?, timezone) AS timezone FROM users WHERE user = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
2020-10-13 12:58:34 +00:00
|
|
|
*LOCAL_LANGUAGE, *LOCAL_TIMEZONE, user_id
|
2020-10-12 20:01:27 +00:00
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
{
|
2020-10-13 12:48:47 +00:00
|
|
|
Ok(c) => Ok(c),
|
2020-08-27 11:15:20 +00:00
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
Err(sqlx::Error::RowNotFound) => {
|
|
|
|
let dm_channel = user.create_dm_channel(ctx).await?;
|
|
|
|
let dm_id = dm_channel.id.as_u64().to_owned();
|
2020-09-01 16:07:51 +00:00
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
let pool_c = pool.clone();
|
2020-08-27 11:15:20 +00:00
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT IGNORE INTO channels (channel) VALUES (?)
|
|
|
|
",
|
|
|
|
dm_id
|
|
|
|
)
|
2020-09-01 16:07:51 +00:00
|
|
|
.execute(&pool_c)
|
2020-08-27 11:15:20 +00:00
|
|
|
.await?;
|
|
|
|
|
2020-10-13 12:48:47 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO users (user, name, dm_channel) VALUES (?, ?, (SELECT id FROM channels WHERE channel = ?))
|
|
|
|
", user_id, user.name, dm_id)
|
|
|
|
.execute(&pool_c)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(sqlx::query_as_unchecked!(
|
|
|
|
Self,
|
|
|
|
"
|
2020-08-27 11:15:20 +00:00
|
|
|
SELECT id, user, name, dm_channel, language, timezone FROM users WHERE user = ?
|
2020-10-13 12:48:47 +00:00
|
|
|
",
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:58:34 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!("Error querying for user: {:?}", e);
|
|
|
|
|
|
|
|
Err(Box::new(e))
|
|
|
|
},
|
2020-08-27 11:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:07:51 +00:00
|
|
|
pub async fn commit_changes(&self, pool: &MySqlPool) {
|
2020-08-27 11:15:20 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
self.name,
|
|
|
|
self.language,
|
|
|
|
self.timezone,
|
|
|
|
self.id
|
|
|
|
)
|
|
|
|
.execute(pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-08-27 11:15:20 +00:00
|
|
|
}
|
2020-09-01 16:07:51 +00:00
|
|
|
|
|
|
|
pub async fn response(&self, pool: &MySqlPool, name: &str) -> String {
|
2020-10-12 22:17:47 +00:00
|
|
|
let query_str = &format!(
|
2020-09-01 16:07:51 +00:00
|
|
|
"
|
2020-10-12 22:17:47 +00:00
|
|
|
SELECT value FROM {} WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ?
|
|
|
|
",
|
|
|
|
*STRINGS_TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut query = sqlx::query(&query_str)
|
|
|
|
.bind(&self.language)
|
|
|
|
.bind(&*LOCAL_LANGUAGE)
|
|
|
|
.bind(name)
|
|
|
|
.bind(&*LOCAL_LANGUAGE)
|
|
|
|
.fetch(pool);
|
|
|
|
|
|
|
|
let row = query
|
|
|
|
.next()
|
2020-09-01 16:07:51 +00:00
|
|
|
.await
|
2020-10-12 22:17:47 +00:00
|
|
|
.unwrap_or_else(|e| panic!("Database error: {:?}", e))
|
|
|
|
.unwrap_or_else(|| panic!("No string with that name: {}", name));
|
2020-09-01 16:07:51 +00:00
|
|
|
|
2020-10-12 22:17:47 +00:00
|
|
|
row.get::<String, &str>("value").clone()
|
2020-09-01 16:07:51 +00:00
|
|
|
}
|
2020-09-01 17:18:45 +00:00
|
|
|
|
|
|
|
pub fn timezone(&self) -> Tz {
|
|
|
|
self.timezone.parse().unwrap()
|
|
|
|
}
|
2020-08-27 11:15:20 +00:00
|
|
|
}
|
2020-09-05 20:17:45 +00:00
|
|
|
|
2020-09-08 22:08:02 +00:00
|
|
|
pub struct Timer {
|
|
|
|
pub name: String,
|
|
|
|
pub start_time: NaiveDateTime,
|
|
|
|
pub owner: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Timer {
|
|
|
|
pub async fn from_owner(owner: u64, pool: &MySqlPool) -> Vec<Self> {
|
2020-10-12 20:01:27 +00:00
|
|
|
sqlx::query_as_unchecked!(
|
|
|
|
Timer,
|
2020-09-08 22:08:02 +00:00
|
|
|
"
|
|
|
|
SELECT name, start_time, owner FROM timers WHERE owner = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
owner
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-09-08 22:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn count_from_owner(owner: u64, pool: &MySqlPool) -> u32 {
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT COUNT(1) as count FROM timers WHERE owner = ?
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
owner
|
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.count as u32
|
2020-09-08 22:08:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create(name: &str, owner: u64, pool: &MySqlPool) {
|
|
|
|
sqlx::query!(
|
|
|
|
"
|
|
|
|
INSERT INTO timers (name, owner) VALUES (?, ?)
|
2020-10-12 20:01:27 +00:00
|
|
|
",
|
|
|
|
name,
|
|
|
|
owner
|
|
|
|
)
|
|
|
|
.execute(pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-09-08 22:08:02 +00:00
|
|
|
}
|
|
|
|
}
|