feat: made the other user wolves commands sub_commands

This commit is contained in:
silver 2025-03-06 20:32:26 +00:00
parent 3a39084f40
commit aa58c97fcf
Signed by untrusted user: silver
GPG key ID: 36F93D61BAD3FD7D
3 changed files with 366 additions and 362 deletions

View file

@ -1,9 +1,12 @@
pub mod wolves {
use lettre::{ use lettre::{
message::{header, MultiPart, SinglePart}, message::{header, MultiPart, SinglePart},
transport::smtp::{self, authentication::Credentials}, transport::smtp::{self, authentication::Credentials},
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use maud::html; use maud::html;
use serenity::all::CommandOptionType;
use serenity::builder::CreateCommandOption;
use serenity::{builder::CreateCommand, client::Context, model::id::UserId}; use serenity::{builder::CreateCommand, client::Context, model::id::UserId};
use skynet_discord_bot::common::database::{DataBase, Wolves, WolvesVerify}; use skynet_discord_bot::common::database::{DataBase, Wolves, WolvesVerify};
use skynet_discord_bot::{get_now_iso, random_string, Config}; use skynet_discord_bot::{get_now_iso, random_string, Config};
@ -11,7 +14,7 @@ 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 = {
@ -36,14 +39,23 @@ pub mod link {
return "Linking already in process, please check email.".to_string(); return "Linking already in process, please check email.".to_string();
} }
let email = if let Some(CommandDataOption { let sub_options = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(email), value: CommandDataOptionValue::SubCommand(options),
.. ..
}) = command.data.options.first() }) = command.data.options.first()
{ {
email.trim() options
} else { } else {
return "Please provide a valid user".to_string(); 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 // check if email exists
@ -102,12 +114,6 @@ pub mod link {
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 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> { pub async fn get_server_member_discord(db: &Pool<Sqlite>, user: &UserId) -> Option<Wolves> {
sqlx::query_as::<_, Wolves>( sqlx::query_as::<_, Wolves>(
r#" r#"
@ -157,7 +163,7 @@ pub mod link {
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" }
@ -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}
------------------------------------------------------------------------- -------------------------------------------------------------------------
@ -276,8 +282,8 @@ pub mod link {
pub mod verify { pub mod verify {
use super::*; use super::*;
use crate::commands::link_email::link::{db_pending_clear_expired, get_server_member_discord, get_verify_from_db}; use crate::commands::link_email::wolves::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::all::{CommandDataOption, CommandDataOptionValue, CommandInteraction, GuildId, RoleId};
use serenity::model::user::User; use serenity::model::user::User;
use skynet_discord_bot::common::database::get_server_config; use skynet_discord_bot::common::database::get_server_config;
use skynet_discord_bot::common::database::{ServerMembersWolves, Servers}; use skynet_discord_bot::common::database::{ServerMembersWolves, Servers};
@ -287,7 +293,7 @@ pub mod verify {
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 Database in TypeMap.").clone()
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
@ -295,22 +301,31 @@ pub mod verify {
let details = if let Some(x) = get_verify_from_db(&db, &command.user.id).await { let details = if let Some(x) = get_verify_from_db(&db, &command.user.id).await {
x x
} else { } else {
return "Please use /link_wolves first".to_string(); return "Please use ''/wolves link'' first".to_string();
}; };
let code = if let Some(CommandDataOption { let sub_options = if let Some(CommandDataOption {
value: CommandDataOptionValue::String(code), value: CommandDataOptionValue::SubCommand(options),
.. ..
}) = command.data.options.first() }) = command.data.options.first()
{ {
code 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 { } else {
return "Please provide a verification code".to_string(); return "Please provide a verification code".to_string();
}; };
db_pending_clear_expired(&db).await; db_pending_clear_expired(&db).await;
if &details.auth_code != code { if details.auth_code != code {
return "Invalid verification code".to_string(); return "Invalid verification code".to_string();
} }
@ -338,12 +353,6 @@ pub mod verify {
"Failed to verify".to_string() "Failed to verify".to_string()
} }
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> { async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &UserId) -> Result<Option<WolvesVerify>, Error> {
sqlx::query_as::<_, WolvesVerify>( sqlx::query_as::<_, WolvesVerify>(
r#" r#"
@ -450,9 +459,6 @@ pub mod verify {
} }
} }
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};
use skynet_discord_bot::common::database::{DataBase, Wolves}; use skynet_discord_bot::common::database::{DataBase, Wolves};
@ -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
.default_member_permissions(serenity::model::Permissions::ADD_REACTIONS)
.add_option(CreateCommandOption::new(
// link // link
.add_option(
CreateCommandOption::new(CommandOptionType::SubCommand, "link", "Link your Wolves account to your Discord")
.add_sub_option(CreateCommandOption::new(CommandOptionType::String, "email", "UL Wolves Email").required(true)),
)
// verify // verify
.add_option(
CreateCommandOption::new(CommandOptionType::SubCommand, "verify", "Verify Wolves Email")
.add_sub_option(CreateCommandOption::new(CommandOptionType::String, "code", "Code from verification email").required(true)),
)
// unlink // unlink
CommandOptionType::SubCommand, .add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "unlink", "Unlink your Wolves account from your Discord"))
"unlink",
"List out the Committee Roles Numbers",
))
} }
} }

View file

@ -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;

View file

@ -115,8 +115,6 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
vec![ vec![
commands::add_server::register(), commands::add_server::register(),
commands::role_adder::edit::register(), commands::role_adder::edit::register(),
commands::link_email::link::register(),
commands::link_email::verify::register(),
commands::link_email::wolves::register(), commands::link_email::wolves::register(),
commands::minecraft::server::add::register(), commands::minecraft::server::add::register(),
commands::minecraft::server::list::register(), commands::minecraft::server::list::register(),
@ -167,17 +165,17 @@ Sign up on [UL Wolves]({}) 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,