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 serde::Deserialize;
|
||||||
use serenity::model::id::GuildId;
|
use serenity::model::id::GuildId;
|
||||||
|
@ -38,66 +38,116 @@ struct RecordCSV {
|
||||||
expiry: String,
|
expiry: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RecordCSV> for Accounts {
|
impl From<&RecordCSV> for Wolves {
|
||||||
fn from(input: RecordCSV) -> Self {
|
fn from(input: &RecordCSV) -> Self {
|
||||||
Self {
|
Self {
|
||||||
server: Default::default(),
|
id_wolves: input.mem_id.to_owned(),
|
||||||
id_wolves: "".to_string(),
|
email: input.email.to_owned(),
|
||||||
id_member: input.mem_id,
|
|
||||||
email: input.email,
|
|
||||||
expiry: input.expiry,
|
|
||||||
discord: None,
|
discord: None,
|
||||||
minecraft: None,
|
minecraft: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_csv(config: &Config) -> Result<Vec<Accounts>, Box<dyn std::error::Error>> {
|
impl From<&RecordCSV> for ServerMembers {
|
||||||
let mut records: Vec<Accounts> = vec![];
|
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);
|
let csv = format!("{}/{}", &config.home, &config.csv);
|
||||||
if let Ok(mut rdr) = csv::Reader::from_path(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
|
// Notice that we need to provide a type hint for automatic
|
||||||
// deserialization.
|
// deserialization.
|
||||||
let record: RecordCSV = result?;
|
let record: RecordCSV = r?;
|
||||||
if record.mem_id.is_empty() {
|
if record.mem_id.is_empty() {
|
||||||
continue;
|
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) {
|
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Csv) {
|
||||||
let existing = match sqlx::query_as::<_, Accounts>(
|
match sqlx::query_as::<_, Wolves>(
|
||||||
r#"
|
"
|
||||||
SELECT *
|
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
||||||
FROM accounts
|
VALUES (?1, ?2)
|
||||||
WHERE server = ? AND id_member = ?
|
",
|
||||||
"#,
|
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(&user.wolves.id_wolves)
|
||||||
.bind(&user.id_member)
|
.bind(&user.wolves.email)
|
||||||
.fetch_one(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(acc) => acc.id_wolves,
|
Ok(_) => {}
|
||||||
Err(_) => String::new(),
|
Err(e) => {
|
||||||
};
|
println!("Failure to insert into {} {:?}", server.as_u64(), user.wolves);
|
||||||
|
println!("{:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match sqlx::query_as::<_, Accounts>(
|
match sqlx::query_as::<_, ServerMembers>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
|
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
VALUES (?1, ?2, ?3)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&existing)
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct SkynetResult {
|
||||||
|
discord: String,
|
||||||
|
id_member: String,
|
||||||
|
}
|
||||||
|
async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
|
||||||
|
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
|
||||||
|
if let Ok(result) = surf::get(url).recv_json::<Vec<SkynetResult>>().await {
|
||||||
|
for user in result {
|
||||||
|
add_users_skynet(db, &config.skynet_server, &user).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
||||||
|
match sqlx::query_as::<_, Wolves>(
|
||||||
|
"
|
||||||
|
UPDATE wolves
|
||||||
|
SET discord = ?
|
||||||
|
WHERE id_wolves = ?
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.bind(&user.discord)
|
||||||
.bind(&user.id_member)
|
.bind(&user.id_member)
|
||||||
.bind(&user.email)
|
|
||||||
.bind(&user.expiry)
|
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
@ -109,69 +159,9 @@ 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) {
|
|
||||||
let url = format!("{}/ldap/discord?auth={}", &config.ldap_api, &config.auth);
|
|
||||||
if let Ok(result) = surf::get(url).recv_json::<Vec<SkynetResult>>().await {
|
|
||||||
for user in result {
|
|
||||||
add_users_skynet(db, &config.skynet_server, &user).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
|
|
||||||
if !user.id_wolves.is_empty() {
|
|
||||||
match sqlx::query_as::<_, Accounts>(
|
|
||||||
"
|
|
||||||
UPDATE accounts
|
|
||||||
SET discord = ?
|
|
||||||
WHERE server = ? AND 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
|
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
|
||||||
println!("{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct WolvesResult {
|
struct WolvesResult {
|
||||||
pub id_wolves: String,
|
pub id_wolves: String,
|
||||||
pub id_member: String,
|
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
}
|
}
|
||||||
|
@ -186,7 +176,6 @@ async fn get_wolves(db: &Pool<Sqlite>) {
|
||||||
// get the data here
|
// get the data here
|
||||||
let result: Vec<WolvesResult> = vec![WolvesResult {
|
let result: Vec<WolvesResult> = vec![WolvesResult {
|
||||||
id_wolves: "12345".to_string(),
|
id_wolves: "12345".to_string(),
|
||||||
id_member: "166425".to_string(),
|
|
||||||
email: "ul.wolves2@brendan.ie".to_string(),
|
email: "ul.wolves2@brendan.ie".to_string(),
|
||||||
expiry: "2024-08-31".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) {
|
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
|
INSERT OR REPLACE INTO wolves (id_wolves, email)
|
||||||
SET id_wolves = ?
|
VALUES (?1, ?2)
|
||||||
WHERE server = ? AND id_member = ?
|
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(&user.id_wolves)
|
.bind(&user.id_wolves)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(&user.email)
|
||||||
.bind(&user.id_member)
|
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Failure to update into {} {:?}", server.as_u64(), user);
|
println!("Failure to insert into Wolves {} {:?}", server.as_u64(), user);
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match sqlx::query_as::<_, Accounts>(
|
match sqlx::query_as::<_, ServerMembers>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO accounts (server, id_wolves, id_member, email, expiry)
|
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
VALUES (?1, ?2, ?3)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&user.id_wolves)
|
.bind(&user.id_wolves)
|
||||||
.bind(&user.id_member)
|
|
||||||
.bind(&user.email)
|
|
||||||
.bind(&user.expiry)
|
.bind(&user.expiry)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Failure to insert into {} {:?}", server.as_u64(), user);
|
println!("Failure to insert into ServerMembers {} {:?}", server.as_u64(), user);
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use serenity::{
|
||||||
},
|
},
|
||||||
Client,
|
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 sqlx::{Pool, Sqlite};
|
||||||
use std::{process, sync::Arc};
|
use std::{process, sync::Arc};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
@ -131,13 +131,18 @@ async fn bulk_check(ctx: Arc<Context>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Wolves> {
|
||||||
sqlx::query_as::<_, Accounts>(
|
sqlx::query_as::<_, Wolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM accounts
|
FROM server_members
|
||||||
WHERE server = ? AND discord IS NOT NULL AND expiry > ?
|
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)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(get_now_iso(true))
|
.bind(get_now_iso(true))
|
||||||
|
@ -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) {
|
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
|
UPDATE servers
|
||||||
SET member_past = ?, member_current = ?
|
SET member_past = ?, member_current = ?
|
||||||
|
|
|
@ -1,17 +1,11 @@
|
||||||
use serenity::builder::CreateApplicationCommand;
|
use serenity::builder::CreateApplicationCommand;
|
||||||
use serenity::client::Context;
|
use serenity::client::Context;
|
||||||
use serenity::model::application::interaction::Interaction;
|
|
||||||
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
use serenity::model::application::interaction::application_command::ApplicationCommandInteraction;
|
||||||
use serenity::model::id::GuildId;
|
use serenity::model::id::GuildId;
|
||||||
use serenity::model::prelude::command::CommandOptionType;
|
use serenity::model::prelude::command::CommandOptionType;
|
||||||
use serenity::model::prelude::interaction::application_command::{
|
use serenity::model::prelude::interaction::application_command::CommandDataOptionValue;
|
||||||
CommandDataOption,
|
use skynet_discord_bot::{get_now_iso, DataBase, Wolves};
|
||||||
CommandDataOptionValue,
|
|
||||||
};
|
|
||||||
use serenity::model::user::User;
|
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
use skynet_discord_bot::DataBase;
|
|
||||||
|
|
||||||
|
|
||||||
pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||||
let db_lock = {
|
let db_lock = {
|
||||||
|
@ -20,10 +14,14 @@ pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
||||||
};
|
};
|
||||||
let db = db_lock.read().await;
|
let db = db_lock.read().await;
|
||||||
|
|
||||||
|
let option = options
|
||||||
|
.data
|
||||||
|
.options
|
||||||
let option = options.data.options.get(0).expect("Expected email option").resolved.as_ref().expect("Expected email object");
|
.get(0)
|
||||||
|
.expect("Expected email option")
|
||||||
|
.resolved
|
||||||
|
.as_ref()
|
||||||
|
.expect("Expected email object");
|
||||||
|
|
||||||
if let CommandDataOptionValue::String(email) = option {
|
if let CommandDataOptionValue::String(email) = option {
|
||||||
format!("Email is {}, user is {} {:?}", email, options.user.name, options.guild_id)
|
format!("Email is {}, user is {} {:?}", email, options.user.name, options.guild_id)
|
||||||
|
@ -33,26 +31,28 @@ pub async fn run(options: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||||
command.name("link").description("Set Wolves Email").create_option(|option| {
|
command
|
||||||
option
|
.name("link")
|
||||||
.name("email")
|
.description("Set Wolves Email")
|
||||||
.description("UL Wolves Email")
|
.create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true))
|
||||||
.kind(CommandOptionType::String)
|
|
||||||
.required(true)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Accounts> {
|
async fn get_server_member_bulk(db: &Pool<Sqlite>, server: &GuildId) -> Vec<Wolves> {
|
||||||
sqlx::query_as::<_, Accounts>(
|
sqlx::query_as::<_, Wolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM wolves
|
FROM server_members
|
||||||
WHERE server = ? AND discord IS NOT NULL AND expiry > ?
|
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)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(get_now_iso(true))
|
.bind(get_now_iso(true))
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
122
src/lib.rs
122
src/lib.rs
|
@ -109,17 +109,12 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct Accounts {
|
pub struct ServerMembers {
|
||||||
pub server: GuildId,
|
pub server: GuildId,
|
||||||
pub id_wolves: String,
|
pub id_wolves: String,
|
||||||
pub id_member: String,
|
|
||||||
pub email: String,
|
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
pub discord: Option<String>,
|
|
||||||
pub minecraft: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
impl<'r> FromRow<'r, SqliteRow> for ServerMembers {
|
||||||
impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
|
||||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||||
let server_tmp: i64 = row.try_get("server")?;
|
let server_tmp: i64 = row.try_get("server")?;
|
||||||
let server = GuildId::from(server_tmp as u64);
|
let server = GuildId::from(server_tmp as u64);
|
||||||
|
@ -127,9 +122,24 @@ impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
server,
|
server,
|
||||||
id_wolves: row.try_get("id_wolves")?,
|
id_wolves: row.try_get("id_wolves")?,
|
||||||
id_member: row.try_get("id_member")?,
|
|
||||||
email: row.try_get("email")?,
|
|
||||||
expiry: row.try_get("expiry")?,
|
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")?,
|
discord: row.try_get("discord")?,
|
||||||
minecraft: row.try_get("minecraft")?,
|
minecraft: row.try_get("minecraft")?,
|
||||||
})
|
})
|
||||||
|
@ -180,38 +190,47 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
|
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
.max_connections(5)
|
.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?;
|
.await?;
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS accounts (
|
"CREATE TABLE IF NOT EXISTS wolves (
|
||||||
server integer not null,
|
id_wolves text PRIMARY KEY,
|
||||||
id_wolves text DEFAULT '',
|
email text not null,
|
||||||
id_member text DEFAULT '',
|
discord text,
|
||||||
email text not null,
|
minecraft text
|
||||||
expiry text not null,
|
)",
|
||||||
discord text,
|
|
||||||
minecraft text,
|
|
||||||
PRIMARY KEY(server,id_wolves,id_member)
|
|
||||||
)",
|
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_server ON accounts (server)").execute(&pool).await?;
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_discord ON wolves (discord)").execute(&pool).await?;
|
||||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_id_wolves ON accounts (id_wolves)")
|
|
||||||
.execute(&pool)
|
sqlx::query(
|
||||||
.await?;
|
"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(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS servers (
|
"CREATE TABLE IF NOT EXISTS servers (
|
||||||
server integer key,
|
server integer KEY,
|
||||||
wolves_api text not null,
|
wolves_api text not null,
|
||||||
role_past integer,
|
role_past integer,
|
||||||
role_current integer,
|
role_current integer,
|
||||||
member_past integer DEFAULT 0,
|
member_past integer DEFAULT 0,
|
||||||
member_current integer DEFAULT 0
|
member_current integer DEFAULT 0
|
||||||
)",
|
)",
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -222,10 +241,10 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Servers> {
|
pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Servers> {
|
||||||
sqlx::query_as::<_, Servers>(
|
sqlx::query_as::<_, Servers>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM servers
|
FROM servers
|
||||||
WHERE server = ?
|
WHERE server = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
|
@ -233,19 +252,36 @@ pub async fn get_server_config(db: &Pool<Sqlite>, server: &GuildId) -> Option<Se
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<Accounts> {
|
pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &guild::Member) -> Option<ServerMembers> {
|
||||||
sqlx::query_as::<_, Accounts>(
|
let wolves_data = sqlx::query_as::<_, Wolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM accounts
|
FROM wolves
|
||||||
WHERE server = ? AND discord = ?
|
WHERE discord = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&member.user.name)
|
.bind(&member.user.name)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await
|
.await;
|
||||||
.ok()
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
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,
|
async_trait,
|
||||||
client::{Context, EventHandler},
|
client::{Context, EventHandler},
|
||||||
model::{
|
model::{
|
||||||
|
application::{
|
||||||
|
command::Command,
|
||||||
|
interaction::{Interaction, InteractionResponseType},
|
||||||
|
},
|
||||||
gateway::{GatewayIntents, Ready},
|
gateway::{GatewayIntents, Ready},
|
||||||
guild,
|
guild,
|
||||||
application::{interaction::{Interaction, InteractionResponseType}, command::Command},
|
|
||||||
},
|
},
|
||||||
Client,
|
Client,
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
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 skynet_discord_bot::{db_init, get_config, get_server_config, get_server_member, Config, DataBase};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
@ -56,13 +58,11 @@ impl EventHandler for Handler {
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||||
println!("[Main] {} is connected!", ready.user.name);
|
println!("[Main] {} is connected!", ready.user.name);
|
||||||
|
|
||||||
|
|
||||||
Command::set_global_application_commands(&ctx.http, |commands| {
|
Command::set_global_application_commands(&ctx.http, |commands| {
|
||||||
commands
|
commands.create_application_command(|command| commands::link_email::register(command))
|
||||||
.create_application_command(|command| commands::link_email::register(command))
|
|
||||||
})
|
})
|
||||||
.await.ok();
|
.await
|
||||||
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||||
|
|
Loading…
Reference in a new issue