added env vars

This commit is contained in:
liyunze 2025-11-23 22:47:05 +11:00
parent 72937ed5b9
commit 7e6d4f014c
2 changed files with 45 additions and 32 deletions

View file

@ -1,4 +1,5 @@
use crate::{Data, Error}; use crate::{Data, Error};
use dotenv::dotenv;
use poise::{CreateReply, Modal}; use poise::{CreateReply, Modal};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serenity::all::{ use serenity::all::{
@ -26,46 +27,47 @@ struct VerificationModal {
student_id: String, student_id: String,
} }
#[poise::command(slash_command, subcommands("embed", "myself"))] // #[poise::command(slash_command, subcommands("embed", "myself"))]
pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> { // pub async fn verify(_: ApplicationContext<'_>) -> Result<(), Error> {
Ok(()) // Ok(())
} // }
/// Verify your DSEC club membership to obtain role
#[poise::command(slash_command)] #[poise::command(slash_command)]
pub async fn myself(ctx: ApplicationContext<'_>) -> Result<(), Error> { pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> {
verify_member(ctx).await?; verify_member(ctx).await?;
Ok(()) Ok(())
} }
#[poise::command(slash_command)] // #[poise::command(slash_command)]
pub async fn embed(ctx: ApplicationContext<'_>) -> Result<(), Error> { // pub async fn verify(ctx: ApplicationContext<'_>) -> Result<(), Error> {
let reply: CreateReply = { // let reply: CreateReply = {
let embed: CreateEmbed = CreateEmbed::new() // let embed: CreateEmbed = CreateEmbed::new()
.title("Verify your DSEC membership") // .title("Verify your DSEC membership")
.description("Click **Verify Here** and enter your **Full name** and **Student ID** (e.g., s123456789). Your responses are private."); // .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() // CreateReply::default()
.ephemeral(false) // .ephemeral(false)
.embed(embed) // .embed(embed)
.components(components) // .components(components)
}; // };
ctx.send(reply).await?; // ctx.send(reply).await?;
while let Some(_) = ComponentInteractionCollector::new(ctx.serenity_context()) // while let Some(_) = ComponentInteractionCollector::new(ctx.serenity_context())
.filter(move |mci| mci.data.custom_id == "verify") // .filter(move |mci| mci.data.custom_id == "verify")
.await // .await
{ // {
verify_member(ctx).await?; // verify_member(ctx).await?;
} // }
Ok(()) // Ok(())
} // }
// flow: // flow:
// 1. check correct discord server -> layer 1 // 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 // - check if name matches full name
// - if match, assign role // - if match, assign role
async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> { 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 // check if user is in a Discord server, then check if user is in THE Discord server
let guild_id = ctx.guild_id(); let guild_id = ctx.guild_id();
@ -87,7 +91,18 @@ async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> {
return Ok(()); 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 let in_correct_server = &guild_id.unwrap() == &server; // CHANGE
@ -100,7 +115,7 @@ async fn verify_member(ctx: ApplicationContext<'_>) -> Result<(), Error> {
// role ID 1441965955822649344 // role ID 1441965955822649344
let user_id = ctx.author().id; 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 discord_member = GuildId::member(ctx.guild_id().unwrap(), ctx, user_id).await?;
let has_role = discord_member.roles.contains(&verified_role_id); let has_role = discord_member.roles.contains(&verified_role_id);

View file

@ -61,9 +61,7 @@ async fn main() {
.setup(|ctx, _ready, framework| { .setup(|ctx, _ready, framework| {
Box::pin(async move { Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data { Ok(Data { state: app_state })
state: app_state.clone(),
})
}) })
}) })
.build(); .build();