From 7e6d4f014cac36a5f7f6a932b4a376d83f0c85da Mon Sep 17 00:00:00 2001 From: liyunze <50455574+liyunze-coding@users.noreply.github.com> Date: Sun, 23 Nov 2025 22:47:05 +1100 Subject: [PATCH] added env vars --- src/commands/verification.rs | 73 ++++++++++++++++++++++-------------- src/main.rs | 4 +- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/commands/verification.rs b/src/commands/verification.rs index 1f4a770..01c48be 100644 --- a/src/commands/verification.rs +++ b/src/commands/verification.rs @@ -1,4 +1,5 @@ use crate::{Data, Error}; +use dotenv::dotenv; use poise::{CreateReply, Modal}; use serde::{Deserialize, Serialize}; use serenity::all::{ @@ -26,46 +27,47 @@ struct VerificationModal { student_id: String, } -#[poise::command(slash_command, subcommands("embed", "myself"))] -pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> { - Ok(()) -} +// #[poise::command(slash_command, subcommands("embed", "myself"))] +// pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> { +// Ok(()) +// } +/// Verify your DSEC club membership to obtain role #[poise::command(slash_command)] -pub async fn myself(ctx: ApplicationContext<'_>) -> Result<(), Error> { +pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> { verify_member(ctx).await?; Ok(()) } -#[poise::command(slash_command)] -pub async fn embed(ctx: ApplicationContext<'_>) -> Result<(), Error> { - let reply: CreateReply = { - let embed: CreateEmbed = CreateEmbed::new() - .title("Verify your DSEC membership") - .description("Click **Verify Here** and enter your **Full name** and **Student ID** (e.g., s123456789). Your responses are private."); +// #[poise::command(slash_command)] +// pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> { +// let reply: CreateReply = { +// let embed: CreateEmbed = CreateEmbed::new() +// .title("Verify your DSEC membership") +// .description("Click **Verify Here** and enter your **Full name** and **Student ID** (e.g., s123456789). Your responses are private."); - let button: CreateButton = CreateButton::new("verify").label("Verify Here"); +// let button: CreateButton = CreateButton::new("verify").label("Verify Here"); - let components = vec![CreateActionRow::Buttons(vec![button])]; +// let components = vec![CreateActionRow::Buttons(vec![button])]; - CreateReply::default() - .ephemeral(false) - .embed(embed) - .components(components) - }; +// CreateReply::default() +// .ephemeral(false) +// .embed(embed) +// .components(components) +// }; - ctx.send(reply).await?; +// ctx.send(reply).await?; - while let Some(_) = ComponentInteractionCollector::new(ctx.serenity_context()) - .filter(move |mci| mci.data.custom_id == "verify") - .await - { - verify_member(ctx).await?; - } +// while let Some(_) = ComponentInteractionCollector::new(ctx.serenity_context()) +// .filter(move |mci| mci.data.custom_id == "verify") +// .await +// { +// verify_member(ctx).await?; +// } - Ok(()) -} +// Ok(()) +// } // flow: // 1. check correct discord server -> layer 1 @@ -79,6 +81,8 @@ pub async fn embed(ctx: ApplicationContext<'_>) -> Result<(), Error> { // - check if name matches full name // - if match, assign role async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> { + dotenv().ok(); + // check if user is in a Discord server, then check if user is in THE Discord server let guild_id = ctx.guild_id(); @@ -87,7 +91,18 @@ async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> { return Ok(()); } - let server = GuildId::new(735865924829315072); + let guild_id_string = std::env::var("GUILD_ID").expect("missing GUILD_ID"); + let role_id_string = std::env::var("VERIFIED_ROLE_ID").expect("missing VERIFIED_ROLE_ID"); + + let guild_id_u64: u64 = guild_id_string + .parse() + .expect("Unable to parse GUILD_ID into number"); + + let role_id_u64: u64 = role_id_string + .parse() + .expect("Unable to parse VERIFIED_ROLE_ID into number"); + + let server = GuildId::new(guild_id_u64); let in_correct_server = &guild_id.unwrap() == &server; // CHANGE @@ -100,7 +115,7 @@ async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> { // role ID 1441965955822649344 let user_id = ctx.author().id; - let verified_role_id = RoleId::new(1441965955822649344); // hardcoded for now + let verified_role_id = RoleId::new(role_id_u64); // hardcoded for now let discord_member = GuildId::member(ctx.guild_id().unwrap(), ctx, user_id).await?; let has_role = discord_member.roles.contains(&verified_role_id); diff --git a/src/main.rs b/src/main.rs index 91f972d..79bf7b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,9 +61,7 @@ async fn main() { .setup(|ctx, _ready, framework| { Box::pin(async move { poise::builtins::register_globally(ctx, &framework.options().commands).await?; - Ok(Data { - state: app_state.clone(), - }) + Ok(Data { state: app_state }) }) }) .build();