From b54d9c3a708b287599214925d385d00f84b59481 Mon Sep 17 00:00:00 2001 From: Rishi Chaubey <155433512+RishiChaubey31@users.noreply.github.com> Date: Sat, 19 Oct 2024 20:49:28 +0530 Subject: [PATCH 1/4] remove feature flag (#275) --- src/constants/commands.ts | 6 ------ src/controllers/mentionEachUser.ts | 9 ++++----- src/typeDefinitions/filterUsersByRole.d.ts | 5 ----- tests/unit/handlers/mentionEachUser.test.ts | 7 +------ 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/constants/commands.ts b/src/constants/commands.ts index 47a83d65..f98b1380 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -44,12 +44,6 @@ export const MENTION_EACH = { type: 3, require: false, }, - { - name: "dev", - description: "want to tag them individually?", - type: 5, - require: false, - }, ], }; diff --git a/src/controllers/mentionEachUser.ts b/src/controllers/mentionEachUser.ts index ebe63602..c02812b6 100644 --- a/src/controllers/mentionEachUser.ts +++ b/src/controllers/mentionEachUser.ts @@ -6,7 +6,6 @@ import { env } from "../typeDefinitions/default.types"; import { UserArray, MentionEachUserOptions, - DevFlag, } from "../typeDefinitions/filterUsersByRole"; import { mentionEachUserInMessage } from "../utils/guildRole"; import { checkDisplayType } from "../utils/checkDisplayType"; @@ -16,7 +15,6 @@ export async function mentionEachUser( roleToBeTaggedObj: MentionEachUserOptions; displayMessageObj?: MentionEachUserOptions; channelId: number; - dev?: DevFlag; }, env: env, ctx: ExecutionContext @@ -24,19 +22,20 @@ export async function mentionEachUser( const getMembersInServerResponse = await getMembersInServer(env); const roleId = transformedArgument.roleToBeTaggedObj.value; const msgToBeSent = transformedArgument?.displayMessageObj?.value; - const dev = transformedArgument?.dev?.value || false; - // optional chaining here only because display message obj is optional argument + const usersWithMatchingRole = filterUserByRoles( getMembersInServerResponse as UserArray[], roleId ); + const payload = { channelId: transformedArgument.channelId, roleId: roleId, message: msgToBeSent, usersWithMatchingRole, }; - if (!dev || usersWithMatchingRole.length === 0) { + + if (usersWithMatchingRole.length === 0) { const responseData = checkDisplayType({ usersWithMatchingRole, msgToBeSent, diff --git a/src/typeDefinitions/filterUsersByRole.d.ts b/src/typeDefinitions/filterUsersByRole.d.ts index a54fd748..4d42fd2a 100644 --- a/src/typeDefinitions/filterUsersByRole.d.ts +++ b/src/typeDefinitions/filterUsersByRole.d.ts @@ -10,8 +10,3 @@ export type MentionEachUserOptions = { type: number; value: string; }; -export type DevFlag = { - name: string; - type: number; - value: boolean; -}; diff --git a/tests/unit/handlers/mentionEachUser.test.ts b/tests/unit/handlers/mentionEachUser.test.ts index dc23bc22..5224f904 100644 --- a/tests/unit/handlers/mentionEachUser.test.ts +++ b/tests/unit/handlers/mentionEachUser.test.ts @@ -19,7 +19,7 @@ describe("Test mention each function", () => { expect(response).toBeInstanceOf(Promise); }); - it("should run without displayMessageObj argument in dev mode", async () => { + it("should run without displayMessageObj argument", async () => { const env = { BOT_PUBLIC_KEY: "xyz", DISCORD_GUILD_ID: "123", @@ -28,11 +28,6 @@ describe("Test mention each function", () => { const response = mentionEachUser( { ...onlyRoleToBeTagged, - dev: { - name: "dev", - type: 4, - value: true, - }, }, env, ctx From 28636d7d27ed7ed9d6dde374fad7f7255eeeaed8 Mon Sep 17 00:00:00 2001 From: Vinit khandal <111434418+vinit717@users.noreply.github.com> Date: Mon, 21 Oct 2024 00:33:17 +0530 Subject: [PATCH 2/4] Revert "remove feature flag (#275)" (#281) This reverts commit b54d9c3a708b287599214925d385d00f84b59481. --- src/constants/commands.ts | 6 ++++++ src/controllers/mentionEachUser.ts | 9 +++++---- src/typeDefinitions/filterUsersByRole.d.ts | 5 +++++ tests/unit/handlers/mentionEachUser.test.ts | 7 ++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/constants/commands.ts b/src/constants/commands.ts index f98b1380..47a83d65 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -44,6 +44,12 @@ export const MENTION_EACH = { type: 3, require: false, }, + { + name: "dev", + description: "want to tag them individually?", + type: 5, + require: false, + }, ], }; diff --git a/src/controllers/mentionEachUser.ts b/src/controllers/mentionEachUser.ts index c02812b6..ebe63602 100644 --- a/src/controllers/mentionEachUser.ts +++ b/src/controllers/mentionEachUser.ts @@ -6,6 +6,7 @@ import { env } from "../typeDefinitions/default.types"; import { UserArray, MentionEachUserOptions, + DevFlag, } from "../typeDefinitions/filterUsersByRole"; import { mentionEachUserInMessage } from "../utils/guildRole"; import { checkDisplayType } from "../utils/checkDisplayType"; @@ -15,6 +16,7 @@ export async function mentionEachUser( roleToBeTaggedObj: MentionEachUserOptions; displayMessageObj?: MentionEachUserOptions; channelId: number; + dev?: DevFlag; }, env: env, ctx: ExecutionContext @@ -22,20 +24,19 @@ export async function mentionEachUser( const getMembersInServerResponse = await getMembersInServer(env); const roleId = transformedArgument.roleToBeTaggedObj.value; const msgToBeSent = transformedArgument?.displayMessageObj?.value; - + const dev = transformedArgument?.dev?.value || false; + // optional chaining here only because display message obj is optional argument const usersWithMatchingRole = filterUserByRoles( getMembersInServerResponse as UserArray[], roleId ); - const payload = { channelId: transformedArgument.channelId, roleId: roleId, message: msgToBeSent, usersWithMatchingRole, }; - - if (usersWithMatchingRole.length === 0) { + if (!dev || usersWithMatchingRole.length === 0) { const responseData = checkDisplayType({ usersWithMatchingRole, msgToBeSent, diff --git a/src/typeDefinitions/filterUsersByRole.d.ts b/src/typeDefinitions/filterUsersByRole.d.ts index 4d42fd2a..a54fd748 100644 --- a/src/typeDefinitions/filterUsersByRole.d.ts +++ b/src/typeDefinitions/filterUsersByRole.d.ts @@ -10,3 +10,8 @@ export type MentionEachUserOptions = { type: number; value: string; }; +export type DevFlag = { + name: string; + type: number; + value: boolean; +}; diff --git a/tests/unit/handlers/mentionEachUser.test.ts b/tests/unit/handlers/mentionEachUser.test.ts index 5224f904..dc23bc22 100644 --- a/tests/unit/handlers/mentionEachUser.test.ts +++ b/tests/unit/handlers/mentionEachUser.test.ts @@ -19,7 +19,7 @@ describe("Test mention each function", () => { expect(response).toBeInstanceOf(Promise); }); - it("should run without displayMessageObj argument", async () => { + it("should run without displayMessageObj argument in dev mode", async () => { const env = { BOT_PUBLIC_KEY: "xyz", DISCORD_GUILD_ID: "123", @@ -28,6 +28,11 @@ describe("Test mention each function", () => { const response = mentionEachUser( { ...onlyRoleToBeTagged, + dev: { + name: "dev", + type: 4, + value: true, + }, }, env, ctx From bd4d9875459e0fd0ecd814c568283d05b1fdaf1c Mon Sep 17 00:00:00 2001 From: Rishi Chaubey <155433512+RishiChaubey31@users.noreply.github.com> Date: Tue, 5 Nov 2024 23:17:56 +0530 Subject: [PATCH 3/4] To display specific message when used /mention-each command (#280) * display a message everytime user runs mention-each command * created a new feature flag * removed unecessary lines * changed the feature flag name and removed nesting * changed flag name to dev_title * made some test changes * move some lines to fixtures from test * removed unnecessary comments --- src/constants/commands.ts | 6 ++ src/controllers/baseHandler.ts | 3 + src/controllers/mentionEachUser.ts | 15 +++++ tests/fixtures/fixture.ts | 13 +++++ tests/unit/handlers/mentionEachUser.test.ts | 63 +++++++++++++++++++-- 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/constants/commands.ts b/src/constants/commands.ts index 47a83d65..d64a965f 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -50,6 +50,12 @@ export const MENTION_EACH = { type: 5, require: false, }, + { + name: "dev_title", + description: "want to see extra details?", + type: 5, + require: false, + }, ], }; diff --git a/src/controllers/baseHandler.ts b/src/controllers/baseHandler.ts index 2d36c389..61c5b765 100644 --- a/src/controllers/baseHandler.ts +++ b/src/controllers/baseHandler.ts @@ -75,6 +75,9 @@ export async function baseHandler( displayMessageObj: data.find((item) => item.name === "message"), channelId: message.channel_id, dev: data.find((item) => item.name === "dev") as unknown as DevFlag, + dev_title: data.find( + (item) => item.name === "dev_title" + ) as unknown as DevFlag, }; return await mentionEachUser(transformedArgument, env, ctx); } diff --git a/src/controllers/mentionEachUser.ts b/src/controllers/mentionEachUser.ts index ebe63602..6e8f65cf 100644 --- a/src/controllers/mentionEachUser.ts +++ b/src/controllers/mentionEachUser.ts @@ -16,6 +16,7 @@ export async function mentionEachUser( roleToBeTaggedObj: MentionEachUserOptions; displayMessageObj?: MentionEachUserOptions; channelId: number; + dev_title?: DevFlag; dev?: DevFlag; }, env: env, @@ -25,6 +26,7 @@ export async function mentionEachUser( const roleId = transformedArgument.roleToBeTaggedObj.value; const msgToBeSent = transformedArgument?.displayMessageObj?.value; const dev = transformedArgument?.dev?.value || false; + const devtitle = transformedArgument?.dev_title?.value || false; // optional chaining here only because display message obj is optional argument const usersWithMatchingRole = filterUserByRoles( getMembersInServerResponse as UserArray[], @@ -36,6 +38,19 @@ export async function mentionEachUser( message: msgToBeSent, usersWithMatchingRole, }; + + if (transformedArgument.dev_title?.value === true) { + let responseMessage = ""; + if (usersWithMatchingRole.length === 0) { + responseMessage = `Sorry, no user found with <@&${roleId}> role.`; + } else if (usersWithMatchingRole.length === 1) { + responseMessage = `The user with <@&${roleId}> role is ${payload.usersWithMatchingRole}.`; + } else { + responseMessage = `The users with <@&${roleId}> role are ${payload.usersWithMatchingRole}.`; + } + return discordTextResponse(responseMessage); + } + if (!dev || usersWithMatchingRole.length === 0) { const responseData = checkDisplayType({ usersWithMatchingRole, diff --git a/tests/fixtures/fixture.ts b/tests/fixtures/fixture.ts index c5d9855f..8602c71c 100644 --- a/tests/fixtures/fixture.ts +++ b/tests/fixtures/fixture.ts @@ -370,3 +370,16 @@ export const overdueTaskUsers = { }, ], }; +export const testDataWithDevTitle = { + channelId: 123, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: "860900892193456149", + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, + }, +}; diff --git a/tests/unit/handlers/mentionEachUser.test.ts b/tests/unit/handlers/mentionEachUser.test.ts index dc23bc22..c73bfb68 100644 --- a/tests/unit/handlers/mentionEachUser.test.ts +++ b/tests/unit/handlers/mentionEachUser.test.ts @@ -1,6 +1,7 @@ import { mentionEachUser } from "../../../src/controllers/mentionEachUser"; import { checkDisplayType } from "../../../src/utils/checkDisplayType"; import { filterUserByRoles } from "../../../src/utils/filterUsersByRole"; +import { testDataWithDevTitle } from "../../../tests/fixtures/fixture"; import { onlyRoleToBeTagged, transformedArgument, @@ -14,7 +15,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const response = mentionEachUser(transformedArgument, env, ctx); expect(response).toBeInstanceOf(Promise); }); @@ -52,7 +52,6 @@ describe("Test mention each function", () => { DISCORD_GUILD_ID: "123", DISCORD_TOKEN: "abc", }; - const response = mentionEachUser(onlyRoleToBeTagged, env, ctx); expect(response).toBeInstanceOf(Promise); const textMessage: { data: { content: string } } = await response.then( @@ -107,7 +106,7 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - it("should return default string ", () => { + it("should return default string when no users found", () => { const usersWithMatchingRole = [] as string[]; const msgToBeSent = "hello"; const response = checkDisplayType({ usersWithMatchingRole, msgToBeSent }); @@ -115,7 +114,7 @@ describe("Test mention each function", () => { expect(response).toBe(expectedResponse); }); - it("should return default string ", () => { + it("should return default string with undefined message", () => { const usersWithMatchingRole = [ "<@282859044593598464>", "<@725745030706364447>", @@ -126,4 +125,60 @@ describe("Test mention each function", () => { const expectedResponse = `${returnString} ${usersWithMatchingRole}`; expect(response).toBe(expectedResponse); }); + + // New tests for dev_title flag + it("should show appropriate message when no users found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; + const roleId = "860900892193456149"; + const response = mentionEachUser( + { + ...onlyRoleToBeTagged, + roleToBeTaggedObj: { + name: "role", + type: 4, + value: roleId, + }, + dev_title: { + name: "dev_title", + type: 4, + value: true, + }, + }, + env, + ctx + ); + + expect(response).toBeInstanceOf(Promise); + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + expect(textMessage.data.content).toBe( + `Sorry, no user found with <@&${roleId}> role.` + ); + }); + + // Only showing the modified test case for clarity + it("should show appropriate message when single user found with dev_title flag", async () => { + const env = { + BOT_PUBLIC_KEY: "xyz", + DISCORD_GUILD_ID: "123", + DISCORD_TOKEN: "abc", + }; + + const response = mentionEachUser(testDataWithDevTitle, env, ctx); + expect(response).toBeInstanceOf(Promise); + + const textMessage: { data: { content: string } } = await response.then( + (res) => res.json() + ); + + expect([ + `The user with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role is <@282859044593598464>.`, + `Sorry, no user found with <@&${testDataWithDevTitle.roleToBeTaggedObj.value}> role.`, + ]).toContain(textMessage.data.content); + }); }); From 36e04eb8f6e3e86de91f6fce8ee3357e7cd12fa1 Mon Sep 17 00:00:00 2001 From: Tharun <45945950+Saitharun279@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:50:37 +0530 Subject: [PATCH 4/4] Create a mechanism to fail the CD when required environment variables are missing | issue#257 - 2 (#285) * issue#257 | Madge changes to fail CD when required env are missing * use loadEnv method in register.ts * rename to env in register.ts * renamed env in inedx.ts * renamed variables * removed unnecessary comments * removed redundant comments * fixed code format * format fix --- .../register-commands-production.yaml | 30 +++++++++++-- .../workflows/register-commands-staging.yaml | 30 +++++++++++-- config/config.ts | 42 +++++++++++++++++++ config/envVarCheck.ts | 22 ++++++++++ package.json | 3 +- src/index.ts | 8 ++-- src/register.ts | 9 ++-- 7 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 config/envVarCheck.ts diff --git a/.github/workflows/register-commands-production.yaml b/.github/workflows/register-commands-production.yaml index 161dffdb..3fb43a32 100644 --- a/.github/workflows/register-commands-production.yaml +++ b/.github/workflows/register-commands-production.yaml @@ -3,12 +3,36 @@ on: push: branches: main jobs: + Environment-Variables-Check: + runs-on: ubuntu-latest + environment: production + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18.18.2 + - run: npm install + - run: npm run env-var-check + env: + CURRENT_ENVIRONMENT: production + DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}} + DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}} + DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}} + DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}} + CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}} + CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}} + BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}} + RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}} + CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}} + IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}} + Register-Commands: + needs: [Environment-Variables-Check] runs-on: ubuntu-latest environment: production steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -22,7 +46,7 @@ jobs: runs-on: ubuntu-latest environment: production steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: npm install - uses: cloudflare/wrangler-action@2.0.0 with: diff --git a/.github/workflows/register-commands-staging.yaml b/.github/workflows/register-commands-staging.yaml index 79b47b12..c8c2adc6 100644 --- a/.github/workflows/register-commands-staging.yaml +++ b/.github/workflows/register-commands-staging.yaml @@ -3,12 +3,36 @@ on: push: branches: develop jobs: + Environment-Variables-Check: + runs-on: ubuntu-latest + environment: staging + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18.18.2 + - run: npm install + - run: npm run env-var-check + env: + CURRENT_ENVIRONMENT: staging + DISCORD_APPLICATION_ID: ${{secrets.DISCORD_APPLICATION_ID}} + DISCORD_GUILD_ID: ${{secrets.DISCORD_GUILD_ID}} + DISCORD_TOKEN: ${{secrets.DISCORD_TOKEN}} + DISCORD_PUBLIC_KEY: ${{secrets.DISCORD_PUBLIC_KEY}} + CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}} + CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}} + BOT_PRIVATE_KEY: ${{secrets.BOT_PRIVATE_KEY}} + RDS_SERVERLESS_PUBLIC_KEY: ${{secrets.RDS_SERVERLESS_PUBLIC_KEY}} + CRON_JOBS_PUBLIC_KEY: ${{secrets.CRON_JOBS_PUBLIC_KEY}} + IDENTITY_SERVICE_PUBLIC_KEY: ${{secrets.IDENTITY_SERVICE_PUBLIC_KEY}} + Register-Commands: + needs: [Environment-Variables-Check] runs-on: ubuntu-latest environment: staging steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18.18.2 - run: npm install @@ -22,7 +46,7 @@ jobs: runs-on: ubuntu-latest environment: staging steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - run: npm install - uses: cloudflare/wrangler-action@2.0.0 with: diff --git a/config/config.ts b/config/config.ts index 1c95bcad..e81afe71 100644 --- a/config/config.ts +++ b/config/config.ts @@ -19,6 +19,48 @@ import { DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP, DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP, } from "../src/constants/variables"; +import { config as configFromDotEnv } from "dotenv"; + +export function loadEnv(env: env, fromWorkerEnv: boolean): env { + const Env: env = { + CURRENT_ENVIRONMENT: fromWorkerEnv + ? env.CURRENT_ENVIRONMENT + : process.env.CURRENT_ENVIRONMENT || "", + DISCORD_APPLICATION_ID: fromWorkerEnv + ? env.DISCORD_APPLICATION_ID + : process.env.DISCORD_APPLICATION_ID || "", + DISCORD_GUILD_ID: fromWorkerEnv + ? env.DISCORD_GUILD_ID + : process.env.DISCORD_GUILD_ID || "", + DISCORD_TOKEN: fromWorkerEnv + ? env.DISCORD_TOKEN + : process.env.DISCORD_TOKEN || "", + DISCORD_PUBLIC_KEY: fromWorkerEnv + ? env.DISCORD_PUBLIC_KEY + : process.env.DISCORD_PUBLIC_KEY || "", + CLOUDFLARE_API_TOKEN: fromWorkerEnv + ? env.CLOUDFLARE_API_TOKEN + : process.env.CLOUDFLARE_API_TOKEN || "", + CLOUDFLARE_ACCOUNT_ID: fromWorkerEnv + ? env.CLOUDFLARE_ACCOUNT_ID + : process.env.CLOUDFLARE_ACCOUNT_ID || "", + BOT_PRIVATE_KEY: fromWorkerEnv + ? env.BOT_PRIVATE_KEY + : process.env.BOT_PRIVATE_KEY || "", + RDS_SERVERLESS_PUBLIC_KEY: fromWorkerEnv + ? env.RDS_SERVERLESS_PUBLIC_KEY + : process.env.RDS_SERVERLESS_PUBLIC_KEY || "", + CRON_JOBS_PUBLIC_KEY: fromWorkerEnv + ? env.CRON_JOBS_PUBLIC_KEY + : process.env.CRON_JOBS_PUBLIC_KEY || "", + IDENTITY_SERVICE_PUBLIC_KEY: fromWorkerEnv + ? env.IDENTITY_SERVICE_PUBLIC_KEY + : process.env.IDENTITY_SERVICE_PUBLIC_KEY || "", + }; + return Env; +} + +configFromDotEnv(); const config = (env: env) => { const environment: environment = { diff --git a/config/envVarCheck.ts b/config/envVarCheck.ts new file mode 100644 index 00000000..a7bd2af6 --- /dev/null +++ b/config/envVarCheck.ts @@ -0,0 +1,22 @@ +import { env } from "../src/typeDefinitions/default.types"; +import { loadEnv } from "./config"; + +/** + * Validate if all the required environment variables are set to a non empty value + * else throw an error + * --- + */ +export function validateEnv() { + const env: env = loadEnv({}, false); + const missingEnvVariables = Object.keys(env).filter((key) => env[key] == ""); + + if (missingEnvVariables.length > 0) { + throw new Error( + `Missing environment variables: ${missingEnvVariables.join(", ")}` + ); + } else { + console.log("All required environment variables are set."); + } +} + +validateEnv(); diff --git a/package.json b/package.json index ed1a11d8..763e0ab4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "format-fix": "prettier --write .", "fix": "npm run lint-fix && npm run format-fix", "ngrok": "ngrok http 8787", - "register": "ts-node-esm src/register.ts" + "register": "ts-node-esm src/register.ts", + "env-var-check": "ts-node-esm config/envVarCheck.ts" }, "keywords": [], "author": "", diff --git a/src/index.ts b/src/index.ts index dba65a39..966260a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,7 @@ import { generateInviteLink } from "./controllers/generateDiscordInvite"; import { sendProfileBlockedMessage } from "./controllers/profileHandler"; import { sendTaskUpdatesHandler } from "./controllers/taskUpdatesHandler"; -import config from "./../config/config"; +import config, { loadEnv } from "./../config/config"; const router = Router(); @@ -107,10 +107,12 @@ export default { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); } } - return router.handle(request, env, ctx); + const Env: env = loadEnv(env, true); + return router.handle(request, Env, ctx); }, async scheduled(req: Request, env: env, ctx: ExecutionContext) { - ctx.waitUntil(send(env)); + const Env: env = loadEnv(env, true); + ctx.waitUntil(send(Env)); }, }; diff --git a/src/register.ts b/src/register.ts index 6bd22737..8602d543 100644 --- a/src/register.ts +++ b/src/register.ts @@ -14,6 +14,7 @@ import { import { config } from "dotenv"; import { DISCORD_BASE_URL } from "./constants/urls"; import { registerCommands } from "./utils/registerCommands"; +import { loadEnv } from "../config/config"; config(); @@ -56,8 +57,10 @@ async function registerGuildCommands( } } +const env = loadEnv({}, false); + registerGuildCommands( - process.env.DISCORD_TOKEN, - process.env.DISCORD_APPLICATION_ID, - process.env.DISCORD_GUILD_ID + env.DISCORD_TOKEN, + env.DISCORD_APPLICATION_ID, + env.DISCORD_GUILD_ID );