Compare commits

...

4 commits

6 changed files with 71 additions and 39 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.0" channel = "1.69"

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::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify}; use skynet_discord_bot::{random_string, Config, DataBase};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
pub mod link { pub mod link {
use serenity::model::id::GuildId;
use super::*; use super::*;
use serenity::model::id::GuildId;
use skynet_discord_bot::Committee;
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,15 +34,14 @@ pub mod link {
} }
} }
let option = command let option = command
.data .data
.options .options
.first() .first()
.expect("Expected email option") .expect("Expected email option")
.resolved .resolved
.as_ref() .as_ref()
.expect("Expected email object"); .expect("Expected email object");
let email = if let CommandDataOptionValue::String(email) = option { let email = if let CommandDataOptionValue::String(email) = option {
email.trim() email.trim()
@ -55,7 +54,6 @@ 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()
@ -76,7 +74,6 @@ 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) {
@ -98,11 +95,17 @@ 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| option.name("email").description("UL Wolves Committee Email").kind(CommandOptionType::String).required(true)) .create_option(|option| {
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<Wolves> { pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Committee>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -175,8 +178,8 @@ pub mod link {
mailer.send(&email) mailer.send(&email)
} }
pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<WolvesVerify> { pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &UserId) -> Option<Committee> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, Committee>(
r#" r#"
SELECT * SELECT *
FROM committee FROM committee
@ -189,8 +192,8 @@ pub mod link {
.ok() .ok()
} }
async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> { async fn save_to_db(db: &Pool<Sqlite>, email: &str, auth: &str, user: &UserId) -> Result<Option<Committee>, sqlx::Error> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, Committee>(
" "
INSERT INTO committee (email, discord, auth_code) INSERT INTO committee (email, discord, auth_code)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
@ -205,11 +208,11 @@ pub mod link {
} }
pub mod verify { pub mod verify {
use serenity::model::id::{GuildId, RoleId};
use super::*; use super::*;
use crate::commands::committee::link::{get_verify_from_db}; use crate::commands::committee::link::get_verify_from_db;
use serenity::model::id::{GuildId, RoleId};
use serenity::model::user::User; use serenity::model::user::User;
use skynet_discord_bot::{get_server_config, ServerMembersWolves, Servers}; use skynet_discord_bot::Committee;
use sqlx::Error; use sqlx::Error;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
@ -227,7 +230,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 Databse in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
@ -257,7 +260,7 @@ pub mod verify {
return "Invalid verification code".to_string(); return "Invalid verification code".to_string();
} }
return match set_discord(&db, &command.user.id).await { 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;
@ -267,21 +270,24 @@ 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.name("verify_committee").description("Verify Wolves Committee Email").create_option(|option| { command
option .name("verify_committee")
.name("code") .description("Verify Wolves Committee Email")
.description("Code from verification email") .create_option(|option| {
.kind(CommandOptionType::String) option
.required(true) .name("code")
}) .description("Code from verification email")
.kind(CommandOptionType::String)
.required(true)
})
} }
async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Wolves>, Error> { async fn set_discord(db: &Pool<Sqlite>, discord: &UserId) -> Result<Option<Committee>, Error> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Committee>(
" "
UPDATE committee UPDATE committee
SET committee = 1 SET committee = 1
@ -302,5 +308,4 @@ 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,6 +218,27 @@ 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,6 +64,9 @@ 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
{ {
@ -89,6 +92,9 @@ 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(),
}; };