feat: now using seperate tables for key info, joined when needed
This commit is contained in:
parent
f405cbbb93
commit
591c61b009
6 changed files with 225 additions and 198 deletions
|
@ -1,4 +1,4 @@
|
|||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Accounts, Config, Servers};
|
||||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serenity::model::id::GuildId;
|
||||
|
@ -38,72 +38,88 @@ struct RecordCSV {
|
|||
expiry: String,
|
||||
}
|
||||
|
||||
impl From<RecordCSV> for Accounts {
|
||||
fn from(input: RecordCSV) -> Self {
|
||||
impl From<&RecordCSV> for Wolves {
|
||||
fn from(input: &RecordCSV) -> Self {
|
||||
Self {
|
||||
server: Default::default(),
|
||||
id_wolves: "".to_string(),
|
||||
id_member: input.mem_id,
|
||||
email: input.email,
|
||||
expiry: input.expiry,
|
||||
id_wolves: input.mem_id.to_owned(),
|
||||
email: input.email.to_owned(),
|
||||
discord: None,
|
||||
minecraft: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_csv(config: &Config) -> Result<Vec<Accounts>, Box<dyn std::error::Error>> {
|
||||
let mut records: Vec<Accounts> = vec![];
|
||||
impl From<&RecordCSV> for ServerMembers {
|
||||
fn from(input: &RecordCSV) -> Self {
|
||||
Self {
|
||||
server: Default::default(),
|
||||
id_wolves: input.mem_id.to_owned(),
|
||||
expiry: input.expiry.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Csv {
|
||||
wolves: Wolves,
|
||||
server_members: ServerMembers,
|
||||
}
|
||||
|
||||
fn get_csv(config: &Config) -> Result<Vec<Csv>, Box<dyn std::error::Error>> {
|
||||
let mut result = vec![];
|
||||
|
||||
let csv = format!("{}/{}", &config.home, &config.csv);
|
||||
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
|
||||
for result in rdr.deserialize() {
|
||||
for r in rdr.deserialize() {
|
||||
// Notice that we need to provide a type hint for automatic
|
||||
// deserialization.
|
||||
let record: RecordCSV = result?;
|
||||
let record: RecordCSV = r?;
|
||||
if record.mem_id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
records.push(Accounts::from(record));
|
||||
|
||||
result.push(Csv {
|
||||
wolves: Wolves::from(&record),
|
||||
server_members: ServerMembers::from(&record),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
Ok(result)
|
||||
}
|
||||
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Accounts) {
|
||||
let existing = match sqlx::query_as::<_, Accounts>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM accounts
|
||||
WHERE server = ? AND id_member = ?
|
||||
"#,
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_member)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
{
|
||||
Ok(acc) => acc.id_wolves,
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Csv) {
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
||||
VALUES (?1, ?2)
|
||||
",
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&existing)
|
||||
.bind(&user.id_member)
|
||||
.bind(&user.email)
|
||||
.bind(&user.expiry)
|
||||
.bind(&user.wolves.id_wolves)
|
||||
.bind(&user.wolves.email)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.wolves);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
match sqlx::query_as::<_, ServerMembers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||
VALUES (?1, ?2, ?3)
|
||||
",
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.server_members.id_wolves)
|
||||
.bind(&user.server_members.expiry)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.server_members);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
@ -112,7 +128,6 @@ async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Accoun
|
|||
#[derive(Debug, Deserialize)]
|
||||
pub struct SkynetResult {
|
||||
discord: String,
|
||||
id_wolves: String,
|
||||
id_member: String,
|
||||
}
|
||||
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
|
||||
|
@ -124,37 +139,14 @@ async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
|
|||
}
|
||||
}
|
||||
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
||||
if !user.id_wolves.is_empty() {
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
UPDATE accounts
|
||||
UPDATE wolves
|
||||
SET discord = ?
|
||||
WHERE server = ? AND id_wolves = ?
|
||||
WHERE id_wolves = ?
|
||||
",
|
||||
)
|
||||
.bind(&user.discord)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_wolves)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !user.id_member.is_empty() {
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
"
|
||||
UPDATE accounts
|
||||
SET discord = ?
|
||||
WHERE server = ? AND id_member = ?
|
||||
",
|
||||
)
|
||||
.bind(&user.discord)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_member)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
|
@ -165,13 +157,11 @@ async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResu
|
|||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WolvesResult {
|
||||
pub id_wolves: String,
|
||||
pub id_member: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
}
|
||||
|
@ -186,7 +176,6 @@ async fn get_wolves(db: &Pool<Sqlite>) {
|
|||
// get the data here
|
||||
let result: Vec<WolvesResult> = vec![WolvesResult {
|
||||
id_wolves: "12345".to_string(),
|
||||
id_member: "166425".to_string(),
|
||||
email: "ul.wolves2@brendan.ie".to_string(),
|
||||
expiry: "2024-08-31".to_string(),
|
||||
}];
|
||||
|
@ -198,43 +187,40 @@ async fn get_wolves(db: &Pool<Sqlite>) {
|
|||
}
|
||||
|
||||
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
// expiry
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
UPDATE accounts
|
||||
SET id_wolves = ?
|
||||
WHERE server = ? AND id_member = ?
|
||||
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
||||
VALUES (?1, ?2)
|
||||
",
|
||||
)
|
||||
.bind(&user.id_wolves)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_member)
|
||||
.bind(&user.email)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to update into {} {:?}", server.as_u64(), user);
|
||||
println!("Failure to insert into Wolves {} {:?}", server.as_u64(), user);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
match sqlx::query_as::<_, ServerMembers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||
VALUES (?1, ?2, ?3)
|
||||
",
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user.id_wolves)
|
||||
.bind(&user.id_member)
|
||||
.bind(&user.email)
|
||||
.bind(&user.expiry)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
||||
println!("Failure to insert into ServerMembers {} {:?}", server.as_u64(), user);
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use serenity::{
|
|||
},
|
||||
Client,
|
||||
};
|
||||
use skynet_discord_bot::{db_init, get_config, get_now_iso, get_server_config_bulk, Accounts, Config, DataBase, Servers};
|
||||
use skynet_discord_bot::{db_init, get_config, get_now_iso, get_server_config_bulk, Config, DataBase, Servers, Wolves};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::{process, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
|
@ -131,12 +131,17 @@ async fn bulk_check(ctx: Arc<Context>) {
|
|||
}
|
||||
}
|
||||
|
||||
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
||||
sqlx::query_as::<_, Accounts>(
|
||||
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Wolves> {
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM accounts
|
||||
WHERE server = ? AND discord IS NOT NULL AND expiry > ?
|
||||
FROM server_members
|
||||
JOIN wolves ON server_members.id_wolves = wolves.id_wolves
|
||||
WHERE (
|
||||
server = ?
|
||||
AND discord IS NOT NULL
|
||||
AND expiry > ?
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
|
@ -147,7 +152,7 @@ async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Acco
|
|||
}
|
||||
|
||||
async fn set_server_numbers(db: &Pool<Sqlite>, server: &GuildId, past: i64, current: i64) {
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
match sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
UPDATE servers
|
||||
SET member_past = ?, member_current = ?
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
use serenity::builder::CreateApplicationCommand;
|
||||
use serenity::client::Context;
|
||||
use serenity::model::application::interaction::Interaction;
|
||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||
use serenity::model::id::GuildId;
|
||||
use serenity::model::prelude::command::CommandOptionType;
|
||||
use serenity::model::prelude::interaction::application_command::{
|
||||
CommandDataOption,
|
||||
CommandDataOptionValue,
|
||||
};
|
||||
use serenity::model::user::User;
|
||||
use serenity::model::prelude::interaction::application_command::CommandDataOptionValue;
|
||||
use skynet_discord_bot::{get_now_iso, DataBase, Wolves};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use skynet_discord_bot::DataBase;
|
||||
|
||||
|
||||
pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
let db_lock = {
|
||||
|
@ -20,10 +14,14 @@ pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
|||
};
|
||||
let db = db_lock.read().await;
|
||||
|
||||
|
||||
|
||||
|
||||
let option = options.data.options.get(0).expect("Expected email option").resolved.as_ref().expect("Expected email object");
|
||||
let option = options
|
||||
.data
|
||||
.options
|
||||
.get(0)
|
||||
.expect("Expected email option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected email object");
|
||||
|
||||
if let CommandDataOptionValue::String(email) = option {
|
||||
format!("Email is {}, user is {} {:?}", email, options.user.name, options.guild_id)
|
||||
|
@ -33,21 +31,23 @@ pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
|||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("link").description("Set Wolves Email").create_option(|option| {
|
||||
option
|
||||
.name("email")
|
||||
.description("UL Wolves Email")
|
||||
.kind(CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
command
|
||||
.name("link")
|
||||
.description("Set Wolves Email")
|
||||
.create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true))
|
||||
}
|
||||
|
||||
async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
||||
sqlx::query_as::<_, Accounts>(
|
||||
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Wolves> {
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM wolves
|
||||
WHERE server = ? AND discord IS NOT NULL AND expiry > ?
|
||||
FROM server_members
|
||||
JOIN wolves ON server_members.id_wolves = wolves.id_wolves
|
||||
WHERE (
|
||||
server = ?
|
||||
AND discord IS NOT NULL
|
||||
AND expiry > ?
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
|
|
86
src/lib.rs
86
src/lib.rs
|
@ -109,17 +109,12 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Accounts {
|
||||
pub struct ServerMembers {
|
||||
pub server: GuildId,
|
||||
pub id_wolves: String,
|
||||
pub id_member: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
pub discord: Option<String>,
|
||||
pub minecraft: Option<String>,
|
||||
}
|
||||
|
||||
impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
||||
impl<'r> FromRow<'r, SqliteRow> for ServerMembers {
|
||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||
let server_tmp: i64 = row.try_get("server")?;
|
||||
let server = GuildId::from(server_tmp as u64);
|
||||
|
@ -127,9 +122,24 @@ impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
|||
Ok(Self {
|
||||
server,
|
||||
id_wolves: row.try_get("id_wolves")?,
|
||||
id_member: row.try_get("id_member")?,
|
||||
email: row.try_get("email")?,
|
||||
expiry: row.try_get("expiry")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Wolves {
|
||||
pub id_wolves: String,
|
||||
pub email: String,
|
||||
pub discord: Option<String>,
|
||||
pub minecraft: Option<String>,
|
||||
}
|
||||
|
||||
impl<'r> FromRow<'r, SqliteRow> for Wolves {
|
||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
id_wolves: row.try_get("id_wolves")?,
|
||||
email: row.try_get("email")?,
|
||||
discord: row.try_get("discord")?,
|
||||
minecraft: row.try_get("minecraft")?,
|
||||
})
|
||||
|
@ -180,32 +190,41 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
||||
.connect_with(
|
||||
SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?
|
||||
.foreign_keys(true)
|
||||
.create_if_missing(true),
|
||||
)
|
||||
.await?;
|
||||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts (
|
||||
server integer not null,
|
||||
id_wolves text DEFAULT '',
|
||||
id_member text DEFAULT '',
|
||||
"CREATE TABLE IF NOT EXISTS wolves (
|
||||
id_wolves text PRIMARY KEY,
|
||||
email text not null,
|
||||
expiry text not null,
|
||||
discord text,
|
||||
minecraft text,
|
||||
PRIMARY KEY(server,id_wolves,id_member)
|
||||
minecraft text
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_server ON accounts (server)").execute(&pool).await?;
|
||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_id_wolves ON accounts (id_wolves)")
|
||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_discord ON wolves (discord)").execute(&pool).await?;
|
||||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS server_members (
|
||||
server integer not null,
|
||||
id_wolves text not null,
|
||||
expiry text not null,
|
||||
PRIMARY KEY(server,id_wolves),
|
||||
FOREIGN KEY (id_wolves) REFERENCES wolves (id_wolves)
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS servers (
|
||||
server integer key,
|
||||
server integer KEY,
|
||||
wolves_api text not null,
|
||||
role_past integer,
|
||||
role_current integer,
|
||||
|
@ -233,19 +252,36 @@ pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Se
|
|||
.ok()
|
||||
}
|
||||
|
||||
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<Accounts> {
|
||||
sqlx::query_as::<_, Accounts>(
|
||||
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<ServerMembers> {
|
||||
let wolves_data = sqlx::query_as::<_, Wolves>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM accounts
|
||||
WHERE server = ? AND discord = ?
|
||||
FROM wolves
|
||||
WHERE discord = ?
|
||||
"#,
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&member.user.name)
|
||||
.fetch_one(db)
|
||||
.await;
|
||||
|
||||
if let Ok(user_wolves) = wolves_data {
|
||||
// check if the suer is on the server
|
||||
return sqlx::query_as::<_, ServerMembers>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM server_members
|
||||
WHERE server = ? AND id_wolves = ?
|
||||
"#,
|
||||
)
|
||||
.bind(*server.as_u64() as i64)
|
||||
.bind(&user_wolves.id_wolves)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.ok()
|
||||
.ok();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -4,14 +4,16 @@ use serenity::{
|
|||
async_trait,
|
||||
client::{Context, EventHandler},
|
||||
model::{
|
||||
application::{
|
||||
command::Command,
|
||||
interaction::{Interaction, InteractionResponseType},
|
||||
},
|
||||
gateway::{GatewayIntents, Ready},
|
||||
guild,
|
||||
application::{interaction::{Interaction, InteractionResponseType}, command::Command},
|
||||
},
|
||||
Client,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use serenity::model::prelude::interaction;
|
||||
|
||||
use skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase};
|
||||
use tokio::sync::RwLock;
|
||||
|
@ -56,13 +58,11 @@ impl EventHandler for Handler {
|
|||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
println!("[Main] {} is connected!", ready.user.name);
|
||||
|
||||
|
||||
Command::set_global_application_commands(&ctx.http, |commands| {
|
||||
commands
|
||||
.create_application_command(|command| commands::link_email::register(command))
|
||||
commands.create_application_command(|command| commands::link_email::register(command))
|
||||
})
|
||||
.await.ok();
|
||||
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
|
|
Loading…
Reference in a new issue