forked from Skynet/discord-bot
feat: made the other user wolves commands sub_commands
This commit is contained in:
parent
3a39084f40
commit
aa58c97fcf
3 changed files with 366 additions and 362 deletions
|
@ -1,178 +1,184 @@
|
||||||
use lettre::{
|
pub mod wolves {
|
||||||
message::{header, MultiPart, SinglePart},
|
use lettre::{
|
||||||
transport::smtp::{self, authentication::Credentials},
|
message::{header, MultiPart, SinglePart},
|
||||||
Message, SmtpTransport, Transport,
|
transport::smtp::{self, authentication::Credentials},
|
||||||
};
|
Message, SmtpTransport, Transport,
|
||||||
use maud::html;
|
};
|
||||||
use serenity::{builder::CreateCommand, client::Context, model::id::UserId};
|
use maud::html;
|
||||||
use skynet_discord_bot::common::database::{DataBase, Wolves, WolvesVerify};
|
use serenity::all::CommandOptionType;
|
||||||
use skynet_discord_bot::{get_now_iso, random_string, Config};
|
use serenity::builder::CreateCommandOption;
|
||||||
use sqlx::{Pool, Sqlite};
|
use serenity::{builder::CreateCommand, client::Context, model::id::UserId};
|
||||||
|
use skynet_discord_bot::common::database::{DataBase, Wolves, WolvesVerify};
|
||||||
|
use skynet_discord_bot::{get_now_iso, random_string, Config};
|
||||||
|
use sqlx::{Pool, Sqlite};
|
||||||
|
|
||||||
pub mod link {
|
pub mod link {
|
||||||
use super::*;
|
use super::*;
|
||||||
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption};
|
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction};
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
pub async fn run(command: &CommandInteraction, ctx: &Context) -> 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()
|
||||||
};
|
};
|
||||||
let db = db_lock.read().await;
|
let db = db_lock.read().await;
|
||||||
|
|
||||||
let config_lock = {
|
let config_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
};
|
};
|
||||||
let config = config_lock.read().await;
|
let config = config_lock.read().await;
|
||||||
|
|
||||||
if get_server_member_discord(&db, &command.user.id).await.is_some() {
|
if get_server_member_discord(&db, &command.user.id).await.is_some() {
|
||||||
return "Already linked".to_string();
|
return "Already linked".to_string();
|
||||||
}
|
|
||||||
|
|
||||||
db_pending_clear_expired(&db).await;
|
|
||||||
|
|
||||||
if get_verify_from_db(&db, &command.user.id).await.is_some() {
|
|
||||||
return "Linking already in process, please check email.".to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
let email = if let Some(CommandDataOption {
|
|
||||||
value: CommandDataOptionValue::String(email),
|
|
||||||
..
|
|
||||||
}) = command.data.options.first()
|
|
||||||
{
|
|
||||||
email.trim()
|
|
||||||
} else {
|
|
||||||
return "Please provide a valid user".to_string();
|
|
||||||
};
|
|
||||||
|
|
||||||
// check if email exists
|
|
||||||
let details = match get_server_member_email(&db, email).await {
|
|
||||||
None => {
|
|
||||||
let invalid_user = "Please check it matches (including case) your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string();
|
|
||||||
|
|
||||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
|
||||||
|
|
||||||
// see if the user actually exists
|
|
||||||
let id = match wolves.get_member(email).await {
|
|
||||||
None => {
|
|
||||||
return invalid_user;
|
|
||||||
}
|
|
||||||
Some(x) => x,
|
|
||||||
};
|
|
||||||
|
|
||||||
// save teh user id and email to teh db
|
|
||||||
match save_to_db_user(&db, id, email).await {
|
|
||||||
Ok(x) => x,
|
|
||||||
Err(x) => {
|
|
||||||
dbg!(x);
|
|
||||||
return "Error: unable to save user to teh database, contact Computer Society".to_string();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// pull it back out (technically could do it in previous step but more explicit)
|
|
||||||
match get_server_member_email(&db, email).await {
|
|
||||||
None => {
|
|
||||||
return "Error: failed to read user from database.".to_string();
|
|
||||||
}
|
|
||||||
Some(x) => x,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(x) => x,
|
|
||||||
};
|
|
||||||
|
|
||||||
if details.discord.is_some() {
|
db_pending_clear_expired(&db).await;
|
||||||
return "Email already verified".to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate a auth key
|
if get_verify_from_db(&db, &command.user.id).await.is_some() {
|
||||||
let auth = random_string(20);
|
return "Linking already in process, please check email.".to_string();
|
||||||
match send_mail(&config, &details.email, &auth, &command.user.name) {
|
}
|
||||||
Ok(_) => match save_to_db(&db, &details, &auth, &command.user.id).await {
|
|
||||||
Ok(_) => {}
|
let sub_options = if let Some(CommandDataOption {
|
||||||
|
value: CommandDataOptionValue::SubCommand(options),
|
||||||
|
..
|
||||||
|
}) = command.data.options.first()
|
||||||
|
{
|
||||||
|
options
|
||||||
|
} else {
|
||||||
|
return "Please provide sub options".to_string();
|
||||||
|
};
|
||||||
|
|
||||||
|
let email = if let Some(x) = sub_options.first() {
|
||||||
|
match &x.value {
|
||||||
|
CommandDataOptionValue::String(email) => email.trim(),
|
||||||
|
_ => return "Please provide a valid email".to_string(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "Please provide a valid email".to_string();
|
||||||
|
};
|
||||||
|
|
||||||
|
// check if email exists
|
||||||
|
let details = match get_server_member_email(&db, email).await {
|
||||||
|
None => {
|
||||||
|
let invalid_user = "Please check it matches (including case) your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string();
|
||||||
|
|
||||||
|
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
||||||
|
|
||||||
|
// see if the user actually exists
|
||||||
|
let id = match wolves.get_member(email).await {
|
||||||
|
None => {
|
||||||
|
return invalid_user;
|
||||||
|
}
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
|
|
||||||
|
// save teh user id and email to teh db
|
||||||
|
match save_to_db_user(&db, id, email).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(x) => {
|
||||||
|
dbg!(x);
|
||||||
|
return "Error: unable to save user to teh database, contact Computer Society".to_string();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// pull it back out (technically could do it in previous step but more explicit)
|
||||||
|
match get_server_member_email(&db, email).await {
|
||||||
|
None => {
|
||||||
|
return "Error: failed to read user from database.".to_string();
|
||||||
|
}
|
||||||
|
Some(x) => x,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(x) => x,
|
||||||
|
};
|
||||||
|
|
||||||
|
if details.discord.is_some() {
|
||||||
|
return "Email already verified".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate a auth key
|
||||||
|
let auth = random_string(20);
|
||||||
|
match send_mail(&config, &details.email, &auth, &command.user.name) {
|
||||||
|
Ok(_) => match save_to_db(&db, &details, &auth, &command.user.id).await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
return format!("Unable to save to db {} {e:?}", &details.email);
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return format!("Unable to save to db {} {e:?}", &details.email);
|
return format!("Unable to send mail to {} {e:?}", &details.email);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
return format!("Unable to send mail to {} {e:?}", &details.email);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format!("Verification email sent to {}, it may take up to 15 min for it to arrive. If it takes longer check the Junk folder.", email)
|
||||||
}
|
}
|
||||||
|
|
||||||
format!("Verification email sent to {}, it may take up to 15 min for it to arrive. If it takes longer check the Junk folder.", email)
|
pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Wolves> {
|
||||||
}
|
sqlx::query_as::<_, Wolves>(
|
||||||
|
r#"
|
||||||
pub fn register() -> CreateCommand {
|
|
||||||
CreateCommand::new("link_wolves")
|
|
||||||
.description("Set Wolves Email")
|
|
||||||
.add_option(CreateCommandOption::new(CommandOptionType::String, "email", "UL Wolves Email").required(true))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Wolves> {
|
|
||||||
sqlx::query_as::<_, Wolves>(
|
|
||||||
r#"
|
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM wolves
|
FROM wolves
|
||||||
WHERE discord = ?
|
WHERE discord = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolves> {
|
async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolves> {
|
||||||
sqlx::query_as::<_, Wolves>(
|
sqlx::query_as::<_, Wolves>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM wolves
|
FROM wolves
|
||||||
WHERE email = ?
|
WHERE email = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(email)
|
.bind(email)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_mail(config: &Config, mail: &str, auth: &str, user: &str) -> Result<smtp::response::Response, smtp::Error> {
|
fn send_mail(config: &Config, mail: &str, auth: &str, user: &str) -> Result<smtp::response::Response, smtp::Error> {
|
||||||
let discord = "https://computer.discord.skynet.ie";
|
let discord = "https://computer.discord.skynet.ie";
|
||||||
let sender = format!("UL Computer Society <{}>", &config.mail_user);
|
let sender = format!("UL Computer Society <{}>", &config.mail_user);
|
||||||
|
|
||||||
// Create the html we want to send.
|
// Create the html we want to send.
|
||||||
let html = html! {
|
let html = html! {
|
||||||
head {
|
head {
|
||||||
title { "UL Wolves Discord Linker" }
|
title { "UL Wolves Discord Linker" }
|
||||||
style type="text/css" {
|
style type="text/css" {
|
||||||
"h2, h4 { font-family: Arial, Helvetica, sans-serif; }"
|
"h2, h4 { font-family: Arial, Helvetica, sans-serif; }"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
div {
|
div {
|
||||||
h2 { "UL Wolves Discord Linker" }
|
h2 { "UL Wolves Discord Linker" }
|
||||||
|
|
||||||
h3 { "Link your UL Wolves Account to Discord" }
|
h3 { "Link your UL Wolves Account to Discord" }
|
||||||
// Substitute in the name of our recipient.
|
// Substitute in the name of our recipient.
|
||||||
p { "Hi " (user) "," }
|
p { "Hi " (user) "," }
|
||||||
p {
|
p {
|
||||||
"Please paste this line into Discord (and press enter) to verify your discord account:"
|
"Please paste this line into Discord (and press enter) to verify your discord account:"
|
||||||
br;
|
br;
|
||||||
pre { "/verify code: " (auth)}
|
pre { "/wolves verify code: " (auth)}
|
||||||
}
|
}
|
||||||
hr;
|
hr;
|
||||||
h3 { "Help & Support" }
|
h3 { "Help & Support" }
|
||||||
p {
|
p {
|
||||||
"If you have issues please refer to our Computer Society Discord Server:"
|
"If you have issues please refer to our Computer Society Discord Server:"
|
||||||
br;
|
br;
|
||||||
a href=(discord) { (discord) }
|
a href=(discord) { (discord) }
|
||||||
br;
|
br;
|
||||||
"UL Computer Society"
|
"UL Computer Society"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let body_text = format!(
|
let body_text = format!(
|
||||||
r#"
|
r#"
|
||||||
UL Wolves Discord Linker
|
UL Wolves Discord Linker
|
||||||
Link your UL Wolves Account to Discord
|
Link your UL Wolves Account to Discord
|
||||||
|
|
||||||
|
@ -181,7 +187,7 @@ pub mod link {
|
||||||
Hi {user},
|
Hi {user},
|
||||||
|
|
||||||
Please paste this line into Discord (and press enter) to verify your Discord account:
|
Please paste this line into Discord (and press enter) to verify your Discord account:
|
||||||
/verify code: {auth}
|
/wolves verify code: {auth}
|
||||||
|
|
||||||
-------------------------------------------------------------------------
|
-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -191,267 +197,267 @@ pub mod link {
|
||||||
{discord}
|
{discord}
|
||||||
UL Computer Society
|
UL Computer Society
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
// Build the message.
|
// Build the message.
|
||||||
let email = Message::builder()
|
let email = Message::builder()
|
||||||
.from(sender.parse().unwrap())
|
.from(sender.parse().unwrap())
|
||||||
.to(mail.parse().unwrap())
|
.to(mail.parse().unwrap())
|
||||||
.subject("Skynet: Link Discord to Wolves.")
|
.subject("Skynet: Link Discord to Wolves.")
|
||||||
.multipart(
|
.multipart(
|
||||||
// This is composed of two parts.
|
// This is composed of two parts.
|
||||||
// also helps not trip spam settings (uneven number of url's
|
// also helps not trip spam settings (uneven number of url's
|
||||||
MultiPart::alternative()
|
MultiPart::alternative()
|
||||||
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_PLAIN).body(body_text))
|
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_PLAIN).body(body_text))
|
||||||
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_HTML).body(html.into_string())),
|
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_HTML).body(html.into_string())),
|
||||||
)
|
)
|
||||||
.expect("failed to build email");
|
.expect("failed to build email");
|
||||||
|
|
||||||
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
||||||
|
|
||||||
// Open a remote connection to gmail using STARTTLS
|
// Open a remote connection to gmail using STARTTLS
|
||||||
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp)?.credentials(creds).build();
|
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp)?.credentials(creds).build();
|
||||||
|
|
||||||
// Send the email
|
// Send the email
|
||||||
mailer.send(&email)
|
mailer.send(&email)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
|
pub async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
|
||||||
sqlx::query_as::<_, WolvesVerify>(
|
sqlx::query_as::<_, WolvesVerify>(
|
||||||
r#"
|
r#"
|
||||||
DELETE
|
DELETE
|
||||||
FROM wolves_verify
|
FROM wolves_verify
|
||||||
WHERE date_expiry < ?
|
WHERE date_expiry < ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(get_now_iso(true))
|
.bind(get_now_iso(true))
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
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<WolvesVerify> {
|
||||||
sqlx::query_as::<_, WolvesVerify>(
|
sqlx::query_as::<_, WolvesVerify>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM wolves_verify
|
FROM wolves_verify
|
||||||
WHERE discord = ?
|
WHERE discord = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> {
|
async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &UserId) -> Result<Option<WolvesVerify>, sqlx::Error> {
|
||||||
sqlx::query_as::<_, WolvesVerify>(
|
sqlx::query_as::<_, WolvesVerify>(
|
||||||
"
|
"
|
||||||
INSERT INTO wolves_verify (email, discord, auth_code, date_expiry)
|
INSERT INTO wolves_verify (email, discord, auth_code, date_expiry)
|
||||||
VALUES (?1, ?2, ?3, ?4)
|
VALUES (?1, ?2, ?3, ?4)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(record.email.to_owned())
|
.bind(record.email.to_owned())
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.bind(auth.to_owned())
|
.bind(auth.to_owned())
|
||||||
.bind(get_now_iso(false))
|
.bind(get_now_iso(false))
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_to_db_user(db: &Pool<Sqlite>, id_wolves: i64, email: &str) -> Result<Option<Wolves>, sqlx::Error> {
|
async fn save_to_db_user(db: &Pool<Sqlite>, id_wolves: i64, email: &str) -> Result<Option<Wolves>, sqlx::Error> {
|
||||||
sqlx::query_as::<_, Wolves>(
|
sqlx::query_as::<_, Wolves>(
|
||||||
"
|
"
|
||||||
INSERT INTO wolves (id_wolves, email)
|
INSERT INTO wolves (id_wolves, email)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(id_wolves)
|
.bind(id_wolves)
|
||||||
.bind(email)
|
.bind(email)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod verify {
|
|
||||||
use super::*;
|
|
||||||
use crate::commands::link_email::link::{db_pending_clear_expired, get_server_member_discord, get_verify_from_db};
|
|
||||||
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption, GuildId, RoleId};
|
|
||||||
use serenity::model::user::User;
|
|
||||||
use skynet_discord_bot::common::database::get_server_config;
|
|
||||||
use skynet_discord_bot::common::database::{ServerMembersWolves, Servers};
|
|
||||||
use skynet_discord_bot::common::wolves::committees::Committees;
|
|
||||||
use sqlx::Error;
|
|
||||||
|
|
||||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
|
||||||
let db_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
let db = db_lock.read().await;
|
|
||||||
|
|
||||||
// check if user has used /link_wolves
|
|
||||||
let details = if let Some(x) = get_verify_from_db(&db, &command.user.id).await {
|
|
||||||
x
|
|
||||||
} else {
|
|
||||||
return "Please use /link_wolves first".to_string();
|
|
||||||
};
|
|
||||||
|
|
||||||
let code = if let Some(CommandDataOption {
|
|
||||||
value: CommandDataOptionValue::String(code),
|
|
||||||
..
|
|
||||||
}) = command.data.options.first()
|
|
||||||
{
|
|
||||||
code
|
|
||||||
} else {
|
|
||||||
return "Please provide a verification code".to_string();
|
|
||||||
};
|
|
||||||
|
|
||||||
db_pending_clear_expired(&db).await;
|
|
||||||
|
|
||||||
if &details.auth_code != code {
|
|
||||||
return "Invalid verification code".to_string();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match db_pending_clear_successful(&db, &command.user.id).await {
|
pub mod verify {
|
||||||
Ok(_) => {
|
use super::*;
|
||||||
return match set_discord(&db, &command.user.id, &details.email).await {
|
use crate::commands::link_email::wolves::link::{db_pending_clear_expired, get_server_member_discord, get_verify_from_db};
|
||||||
Ok(_) => {
|
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, GuildId, RoleId};
|
||||||
// get teh right roles for the user
|
use serenity::model::user::User;
|
||||||
set_server_roles(&db, &command.user, ctx).await;
|
use skynet_discord_bot::common::database::get_server_config;
|
||||||
|
use skynet_discord_bot::common::database::{ServerMembersWolves, Servers};
|
||||||
|
use skynet_discord_bot::common::wolves::committees::Committees;
|
||||||
|
use sqlx::Error;
|
||||||
|
|
||||||
// check if they are a committee member, and on that server
|
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
||||||
set_server_roles_committee(&db, &command.user, ctx).await;
|
let db_lock = {
|
||||||
|
let data_read = ctx.data.read().await;
|
||||||
|
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
|
||||||
|
};
|
||||||
|
let db = db_lock.read().await;
|
||||||
|
|
||||||
"Discord username linked to Wolves".to_string()
|
// check if user has used /link_wolves
|
||||||
}
|
let details = if let Some(x) = get_verify_from_db(&db, &command.user.id).await {
|
||||||
Err(e) => {
|
x
|
||||||
println!("{:?}", e);
|
} else {
|
||||||
"Failed to save, please try /link_wolves again".to_string()
|
return "Please use ''/wolves link'' first".to_string();
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
let sub_options = if let Some(CommandDataOption {
|
||||||
|
value: CommandDataOptionValue::SubCommand(options),
|
||||||
|
..
|
||||||
|
}) = command.data.options.first()
|
||||||
|
{
|
||||||
|
options
|
||||||
|
} else {
|
||||||
|
return "Please provide sub options".to_string();
|
||||||
|
};
|
||||||
|
|
||||||
|
let code = if let Some(x) = sub_options.first() {
|
||||||
|
match &x.value {
|
||||||
|
CommandDataOptionValue::String(y) => y.trim(),
|
||||||
|
_ => return "Please provide a verification code".to_string(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "Please provide a verification code".to_string();
|
||||||
|
};
|
||||||
|
|
||||||
|
db_pending_clear_expired(&db).await;
|
||||||
|
|
||||||
|
if details.auth_code != code {
|
||||||
|
return "Invalid verification code".to_string();
|
||||||
}
|
}
|
||||||
Err(e) => println!("{:?}", e),
|
|
||||||
|
match db_pending_clear_successful(&db, &command.user.id).await {
|
||||||
|
Ok(_) => {
|
||||||
|
return match set_discord(&db, &command.user.id, &details.email).await {
|
||||||
|
Ok(_) => {
|
||||||
|
// get teh right roles for the user
|
||||||
|
set_server_roles(&db, &command.user, ctx).await;
|
||||||
|
|
||||||
|
// check if they are a committee member, and on that server
|
||||||
|
set_server_roles_committee(&db, &command.user, ctx).await;
|
||||||
|
|
||||||
|
"Discord username linked to Wolves".to_string()
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("{:?}", e);
|
||||||
|
"Failed to save, please try /link_wolves again".to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(e) => println!("{:?}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
"Failed to verify".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
"Failed to verify".to_string()
|
async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &UserId) -> Result<Option<WolvesVerify>, Error> {
|
||||||
}
|
sqlx::query_as::<_, WolvesVerify>(
|
||||||
|
r#"
|
||||||
pub fn register() -> CreateCommand {
|
|
||||||
CreateCommand::new("verify")
|
|
||||||
.description("Verify Wolves Email")
|
|
||||||
.add_option(CreateCommandOption::new(CommandOptionType::String, "code", "Code from verification email").required(true))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &UserId) -> Result<Option<WolvesVerify>, Error> {
|
|
||||||
sqlx::query_as::<_, WolvesVerify>(
|
|
||||||
r#"
|
|
||||||
DELETE
|
DELETE
|
||||||
FROM wolves_verify
|
FROM wolves_verify
|
||||||
WHERE discord = ?
|
WHERE discord = ?
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_discord(db: &Pool<Sqlite>, discord: &UserId, email: &str) -> Result<Option<Wolves>, Error> {
|
async fn set_discord(db: &Pool<Sqlite>, discord: &UserId, email: &str) -> Result<Option<Wolves>, Error> {
|
||||||
sqlx::query_as::<_, Wolves>(
|
sqlx::query_as::<_, Wolves>(
|
||||||
"
|
"
|
||||||
UPDATE wolves
|
UPDATE wolves
|
||||||
SET discord = ?
|
SET discord = ?
|
||||||
WHERE email = ?
|
WHERE email = ?
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(discord.get() as i64)
|
.bind(discord.get() as i64)
|
||||||
.bind(email)
|
.bind(email)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_server_roles(db: &Pool<Sqlite>, discord: &User, ctx: &Context) {
|
async fn set_server_roles(db: &Pool<Sqlite>, discord: &User, ctx: &Context) {
|
||||||
if let Ok(servers) = get_servers(db, &discord.id).await {
|
if let Ok(servers) = get_servers(db, &discord.id).await {
|
||||||
for server in servers {
|
for server in servers {
|
||||||
if let Ok(member) = server.server.member(&ctx.http, &discord.id).await {
|
if let Ok(member) = server.server.member(&ctx.http, &discord.id).await {
|
||||||
if let Some(config) = get_server_config(db, &server.server).await {
|
if let Some(config) = get_server_config(db, &server.server).await {
|
||||||
let Servers {
|
let Servers {
|
||||||
role_past,
|
role_past,
|
||||||
role_current,
|
role_current,
|
||||||
..
|
..
|
||||||
} = config;
|
} = config;
|
||||||
|
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if let Some(role) = &role_past {
|
if let Some(role) = &role_past {
|
||||||
if !member.roles.contains(role) {
|
if !member.roles.contains(role) {
|
||||||
roles.push(role.to_owned());
|
roles.push(role.to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !member.roles.contains(&role_current) {
|
if !member.roles.contains(&role_current) {
|
||||||
roles.push(role_current.to_owned());
|
roles.push(role_current.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_committees_id(db: &Pool<Sqlite>, wolves_id: i64) -> Vec<Committees> {
|
async fn get_committees_id(db: &Pool<Sqlite>, wolves_id: i64) -> Vec<Committees> {
|
||||||
sqlx::query_as::<_, Committees>(
|
sqlx::query_as::<_, Committees>(
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM committees
|
FROM committees
|
||||||
WHERE committee LIKE ?1
|
WHERE committee LIKE ?1
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(format!("%{}%", wolves_id))
|
.bind(format!("%{}%", wolves_id))
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
dbg!(e);
|
dbg!(e);
|
||||||
vec![]
|
vec![]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn set_server_roles_committee(db: &Pool<Sqlite>, discord: &User, ctx: &Context) {
|
async fn set_server_roles_committee(db: &Pool<Sqlite>, discord: &User, ctx: &Context) {
|
||||||
if let Some(x) = get_server_member_discord(db, &discord.id).await {
|
if let Some(x) = get_server_member_discord(db, &discord.id).await {
|
||||||
// if they are a member of one or more committees, and in teh committee server then give the teh general committee role
|
// if they are a member of one or more committees, and in teh committee server then give the teh general committee role
|
||||||
// they will get teh more specific vanity role later
|
// they will get teh more specific vanity role later
|
||||||
if !get_committees_id(db, x.id_wolves).await.is_empty() {
|
if !get_committees_id(db, x.id_wolves).await.is_empty() {
|
||||||
let server = GuildId::new(1220150752656363520);
|
let server = GuildId::new(1220150752656363520);
|
||||||
let committee_member = RoleId::new(1226602779968274573);
|
let committee_member = RoleId::new(1226602779968274573);
|
||||||
|
|
||||||
if let Ok(member) = server.member(ctx, &discord.id).await {
|
if let Ok(member) = server.member(ctx, &discord.id).await {
|
||||||
member.add_roles(&ctx, &[committee_member]).await.unwrap_or_default();
|
member.add_roles(&ctx, &[committee_member]).await.unwrap_or_default();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<ServerMembersWolves>, Error> {
|
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<ServerMembersWolves>, Error> {
|
||||||
sqlx::query_as::<_, ServerMembersWolves>(
|
sqlx::query_as::<_, ServerMembersWolves>(
|
||||||
"
|
"
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM server_members
|
FROM server_members
|
||||||
JOIN wolves USING (id_wolves)
|
JOIN wolves USING (id_wolves)
|
||||||
WHERE discord = ?
|
WHERE discord = ?
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(discord.get() as i64)
|
.bind(discord.get() as i64)
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub mod wolves {
|
|
||||||
use serenity::all::{CommandOptionType, CreateCommand, CreateCommandOption};
|
|
||||||
|
|
||||||
pub mod unlink {
|
pub mod unlink {
|
||||||
use serenity::all::{CommandInteraction, Context, UserId};
|
use serenity::all::{CommandInteraction, Context, UserId};
|
||||||
|
@ -494,17 +500,17 @@ pub mod wolves {
|
||||||
pub fn register() -> CreateCommand {
|
pub fn register() -> CreateCommand {
|
||||||
CreateCommand::new("wolves")
|
CreateCommand::new("wolves")
|
||||||
.description("Commands related to UL Wolves")
|
.description("Commands related to UL Wolves")
|
||||||
// All Committee members are able to add reactions to posts
|
// link
|
||||||
.default_member_permissions(serenity::model::Permissions::ADD_REACTIONS)
|
.add_option(
|
||||||
.add_option(CreateCommandOption::new(
|
CreateCommandOption::new(CommandOptionType::SubCommand, "link", "Link your Wolves account to your Discord")
|
||||||
// link
|
.add_sub_option(CreateCommandOption::new(CommandOptionType::String, "email", "UL Wolves Email").required(true)),
|
||||||
|
)
|
||||||
// verify
|
// verify
|
||||||
|
.add_option(
|
||||||
// unlink
|
CreateCommandOption::new(CommandOptionType::SubCommand, "verify", "Verify Wolves Email")
|
||||||
CommandOptionType::SubCommand,
|
.add_sub_option(CreateCommandOption::new(CommandOptionType::String, "code", "Code from verification email").required(true)),
|
||||||
"unlink",
|
)
|
||||||
"List out the Committee Roles Numbers",
|
// unlink
|
||||||
))
|
.add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "unlink", "Unlink your Wolves account from your Discord"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub(crate) mod user {
|
||||||
use super::*;
|
use super::*;
|
||||||
pub(crate) mod add {
|
pub(crate) mod add {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::commands::link_email::link::get_server_member_discord;
|
use crate::commands::link_email::wolves::link::get_server_member_discord;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
|
use serenity::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, CreateCommandOption};
|
||||||
use serenity::model::id::UserId;
|
use serenity::model::id::UserId;
|
||||||
|
|
|
@ -115,8 +115,6 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
|
||||||
vec and go to https://discord.com/channels/{}/{} and use
|
||||||
|
|
||||||
let content = match command.data.name.as_str() {
|
let content = match command.data.name.as_str() {
|
||||||
// user commands
|
// user commands
|
||||||
"link_wolves" => commands::link_email::link::run(&command, &ctx).await,
|
|
||||||
"wolves" => match command.data.options.first() {
|
"wolves" => match command.data.options.first() {
|
||||||
None => "Invalid Command".to_string(),
|
None => "Invalid Command".to_string(),
|
||||||
Some(x) => match x.name.as_str() {
|
Some(x) => match x.name.as_str() {
|
||||||
|
"link" => commands::link_email::wolves::link::run(&command, &ctx).await,
|
||||||
|
"verify" => commands::link_email::wolves::verify::run(&command, &ctx).await,
|
||||||
"unlink" => commands::link_email::wolves::unlink::run(&command, &ctx).await,
|
"unlink" => commands::link_email::wolves::unlink::run(&command, &ctx).await,
|
||||||
// "link" => commands::count::servers::run(&command, &ctx).await,
|
// "link" => commands::count::servers::run(&command, &ctx).await,
|
||||||
&_ => format!("not implemented :( wolves {}", x.name.as_str()),
|
&_ => format!("not implemented :( wolves {}", x.name.as_str()),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"verify" => commands::link_email::verify::run(&command, &ctx).await,
|
|
||||||
"link_minecraft" => commands::minecraft::user::add::run(&command, &ctx).await,
|
"link_minecraft" => commands::minecraft::user::add::run(&command, &ctx).await,
|
||||||
// admin commands
|
// admin commands
|
||||||
"add" => commands::add_server::run(&command, &ctx).await,
|
"add" => commands::add_server::run(&command, &ctx).await,
|
||||||
|
|
Loading…
Add table
Reference in a new issue