Compare commits

..

No commits in common. "982814d99cd1c4e7d80d4182e4b88eab28f0cd39" and "7e40b862d30b18f18a3fd23a89f3a119d35296a4" have entirely different histories.

6 changed files with 39 additions and 71 deletions

View file

@ -3,5 +3,5 @@ CREATE TABLE IF NOT EXISTS committee (
discord integer PRIMARY KEY, discord integer PRIMARY KEY,
email text not null, email text not null,
auth_code text not null, auth_code text not null,
committee integer DEFAULT 0 committee integer DEFAULT 0,
); );

View file

@ -1,2 +1,2 @@
[toolchain] [toolchain]
channel = "1.69" channel = "1.69.0"

View file

@ -13,13 +13,13 @@ use serenity::{
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue}, prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
}, },
}; };
use skynet_discord_bot::{random_string, Config, DataBase}; use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
pub mod link { pub mod link {
use super::*;
use serenity::model::id::GuildId; use serenity::model::id::GuildId;
use skynet_discord_bot::Committee; use super::*;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
let committee_server = GuildId(1220150752656363520); let committee_server = GuildId(1220150752656363520);
@ -34,6 +34,7 @@ pub mod link {
} }
} }
let option = command let option = command
.data .data
.options .options
@ -54,6 +55,7 @@ pub mod link {
return "Please use a @ulwolves.ie address you have access to.".to_string(); return "Please use a @ulwolves.ie address you have access to.".to_string();
} }
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
@ -74,6 +76,7 @@ pub mod link {
return "Linking already in process, please check email.".to_string(); return "Linking already in process, please check email.".to_string();
} }
// generate a auth key // generate a auth key
let auth = random_string(20); let auth = random_string(20);
match send_mail(&config, email, &auth, &command.user.name) { match send_mail(&config, email, &auth, &command.user.name) {
@ -95,17 +98,11 @@ pub mod link {
command command
.name("link_committee") .name("link_committee")
.description("Verify you are a committee member") .description("Verify you are a committee member")
.create_option(|option| { .create_option(|option| option.name("email").description("UL Wolves Committee Email").kind(CommandOptionType::String).required(true))
option
.name("email")
.description("UL Wolves Committee Email")
.kind(CommandOptionType::String)
.required(true)
})
} }
pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> { pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Wolves> {
sqlx::query_as::<_, Committee>( sqlx::query_as::<_, Wolves>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -178,8 +175,8 @@ pub mod link {
mailer.send(&email) mailer.send(&email)
} }
pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> { pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<WolvesVerify> {
sqlx::query_as::<_, Committee>( sqlx::query_as::<_, WolvesVerify>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -192,8 +189,8 @@ pub mod link {
.ok() .ok()
} }
async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<Committee>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> {
sqlx::query_as::<_, Committee>( sqlx::query_as::<_, WolvesVerify>(
" "
INSERT INTO committee (email, discord, auth_code) INSERT INTO committee (email, discord, auth_code)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
@ -208,11 +205,11 @@ pub mod link {
} }
pub mod verify { pub mod verify {
use super::*;
use crate::commands::committee::link::get_verify_from_db;
use serenity::model::id::{GuildId, RoleId}; use serenity::model::id::{GuildId, RoleId};
use super::*;
use crate::commands::committee::link::{get_verify_from_db};
use serenity::model::user::User; use serenity::model::user::User;
use skynet_discord_bot::Committee; use skynet_discord_bot::{get_server_config, ServerMembersWolves, Servers};
use sqlx::Error; use sqlx::Error;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
@ -230,7 +227,7 @@ pub mod verify {
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
@ -260,7 +257,7 @@ pub mod verify {
return "Invalid verification code".to_string(); return "Invalid verification code".to_string();
} }
match set_discord(&db, &command.user.id).await { return match set_discord(&db, &command.user.id).await {
Ok(_) => { Ok(_) => {
// get teh right roles for the user // get teh right roles for the user
set_server_roles(&command.user, ctx).await; set_server_roles(&command.user, ctx).await;
@ -270,14 +267,11 @@ pub mod verify {
println!("{:?}", e); println!("{:?}", e);
"Failed to save, please try /link_committee again".to_string() "Failed to save, please try /link_committee again".to_string()
} }
} };
} }
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command.name("verify_committee").description("Verify Wolves Committee Email").create_option(|option| {
.name("verify_committee")
.description("Verify Wolves Committee Email")
.create_option(|option| {
option option
.name("code") .name("code")
.description("Code from verification email") .description("Code from verification email")
@ -286,8 +280,8 @@ pub mod verify {
}) })
} }
async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Committee>, Error> { async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Wolves>, Error> {
sqlx::query_as::<_, Committee>( sqlx::query_as::<_, Wolves>(
" "
UPDATE committee UPDATE committee
SET committee = 1 SET committee = 1
@ -308,4 +302,5 @@ pub mod verify {
} }
} }
} }
} }

View file

@ -1,4 +1,4 @@
pub mod add_server; pub mod add_server;
pub mod committee;
pub mod link_email; pub mod link_email;
pub mod minecraft; pub mod minecraft;
pub mod committee;

View file

@ -218,27 +218,6 @@ impl<'r> FromRow<'r, SqliteRow> for WolvesVerify {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Committee {
pub email: String,
pub discord: UserId,
pub auth_code: String,
pub committee: i64,
}
impl<'r> FromRow<'r, SqliteRow> for Committee {
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
let user_tmp: i64 = row.try_get("discord")?;
let discord = UserId::from(user_tmp as u64);
Ok(Self {
email: row.try_get("email")?,
discord,
auth_code: row.try_get("auth_code")?,
committee: row.try_get("committee")?,
})
}
}
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Servers { pub struct Servers {
pub server: GuildId, pub server: GuildId,

View file

@ -64,9 +64,6 @@ impl EventHandler for Handler {
.create_application_command(|command| commands::minecraft::server::list::register(command)) .create_application_command(|command| commands::minecraft::server::list::register(command))
.create_application_command(|command| commands::minecraft::server::delete::register(command)) .create_application_command(|command| commands::minecraft::server::delete::register(command))
.create_application_command(|command| commands::minecraft::user::add::register(command)) .create_application_command(|command| commands::minecraft::user::add::register(command))
// for committee server, temp
.create_application_command(|command| commands::committee::link::register(command))
.create_application_command(|command| commands::committee::verify::register(command))
}) })
.await .await
{ {
@ -92,9 +89,6 @@ impl EventHandler for Handler {
"minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await, "minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await,
"minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await, "minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await,
"minecraft_delete" => commands::minecraft::server::delete::run(&command, &ctx).await, "minecraft_delete" => commands::minecraft::server::delete::run(&command, &ctx).await,
// for teh committee server, temporary
"link_committee" => commands::committee::link::run(&command, &ctx).await,
"verify_committee" => commands::committee::verify::run(&command, &ctx).await,
_ => "not implemented :(".to_string(), _ => "not implemented :(".to_string(),
}; };