From 5d43f99ce4322b16d7055861300a22073aae74f9 Mon Sep 17 00:00:00 2001 From: Kodie Date: Sun, 20 Oct 2024 15:28:50 +1300 Subject: [PATCH] refactor: change options requirements (#190) * refactor: change env vars and options requirements * fix: trying to use `__dirname` in es module * chore: add changesets --- .changeset/quick-spoons-crash.md | 5 +++++ .changeset/tricky-cougars-refuse.md | 5 +++++ packages/carbon/src/classes/Client.ts | 20 ++++++------------- .../src/plugins/linked-roles/LinkedRoles.ts | 18 +++++++++++++++++ .../src/tools/templateProcessor.ts | 2 +- packages/create-carbon/template/.env.hbs | 4 ++++ packages/create-carbon/template/index.ts.hbs | 4 ++++ website/content/adapters/bun.mdx | 2 +- website/content/adapters/cloudflare.mdx | 4 +--- website/content/adapters/next.mdx | 2 +- website/content/adapters/node.mdx | 2 +- website/content/classes/client.mdx | 4 ---- website/content/classes/modals.mdx | 1 + .../content/getting-started/basic-setup.mdx | 2 -- website/content/plugins/linked-roles.mdx | 6 +++++- 15 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 .changeset/quick-spoons-crash.md create mode 100644 .changeset/tricky-cougars-refuse.md diff --git a/.changeset/quick-spoons-crash.md b/.changeset/quick-spoons-crash.md new file mode 100644 index 00000000..02e33ef7 --- /dev/null +++ b/.changeset/quick-spoons-crash.md @@ -0,0 +1,5 @@ +--- +"create-carbon": patch +--- + +fix: trying to use `__dirname` in es module diff --git a/.changeset/tricky-cougars-refuse.md b/.changeset/tricky-cougars-refuse.md new file mode 100644 index 00000000..094e9350 --- /dev/null +++ b/.changeset/tricky-cougars-refuse.md @@ -0,0 +1,5 @@ +--- +"@buape/carbon": patch +--- + +refactor: change env vars and options requirements diff --git a/packages/carbon/src/classes/Client.ts b/packages/carbon/src/classes/Client.ts index 6e5cf02a..82f6cc66 100644 --- a/packages/carbon/src/classes/Client.ts +++ b/packages/carbon/src/classes/Client.ts @@ -25,11 +25,7 @@ import { concatUint8Arrays, subtleCrypto, valueToUint8Array } from "../utils.js" /** * The options used for initializing the client */ -export type ClientOptions = { - /** - * The base URL of the app - */ - baseUrl: string +export interface ClientOptions { /** * The client ID of the app */ @@ -37,11 +33,7 @@ export type ClientOptions = { /** * The deploy secret of the app, used for protecting the deploy route */ - deploySecret: string - /** - * The client secret of the app - */ - clientSecret: string + deploySecret?: string /** * The public key of the app, used for interaction verification */ @@ -117,11 +109,11 @@ export class Client extends Plugin { constructor(options: ClientOptions, commands: BaseCommand[]) { super() - if (!options.baseUrl) throw new Error("Missing base URL") - if (!options.clientSecret) throw new Error("Missing client secret") if (!options.clientId) throw new Error("Missing client ID") if (!options.publicKey) throw new Error("Missing public key") if (!options.token) throw new Error("Missing token") + if (!options.deploySecret && !options.disableDeployRoute) + throw new Error("Missing deploy secret") this.options = options this.commands = commands @@ -133,7 +125,7 @@ export class Client extends Plugin { this.rest = new RequestClient(options.token, options.requestOptions) - if (!this.options.disableAutoRegister) { + if (!options.disableAutoRegister) { for (const command of commands) { for (const component of command.components) this.componentHandler.registerComponent(new component()) @@ -141,7 +133,7 @@ export class Client extends Plugin { this.modalHandler.registerModal(new modal()) } } - if (this.options.autoDeploy) { + if (options.autoDeploy) { this.handleDeployRequest() } } diff --git a/packages/carbon/src/plugins/linked-roles/LinkedRoles.ts b/packages/carbon/src/plugins/linked-roles/LinkedRoles.ts index ef2c0bdd..d89885e6 100644 --- a/packages/carbon/src/plugins/linked-roles/LinkedRoles.ts +++ b/packages/carbon/src/plugins/linked-roles/LinkedRoles.ts @@ -13,6 +13,19 @@ type Tokens = { scope: string } +declare module "../../classes/Client.d.ts" { + interface ClientOptions { + /** + * The base URL of the app + */ + baseUrl: string + /** + * The client secret of the app, used for OAuth + */ + clientSecret: string + } +} + // TODO: IMO, the metadata for this should be handled similarly to the client and its commands // That is passing an array of connection instances as the second argument to the constructor // That is, maybe, for another pr though @@ -56,6 +69,11 @@ export class LinkedRoles extends Plugin { constructor(client: Client, options: LinkedRolesOptions) { super() + if (!client.options.baseUrl) throw new Error("Missing base URL") + if (!client.options.clientSecret) throw new Error("Missing client secret") + if (!client.options.deploySecret && !options.disableDeployRoute) + throw new Error("Missing deploy secret") + this.client = client this.options = { ...options } this.appendRoutes() diff --git a/packages/create-carbon/src/tools/templateProcessor.ts b/packages/create-carbon/src/tools/templateProcessor.ts index ebda5ba7..42124541 100644 --- a/packages/create-carbon/src/tools/templateProcessor.ts +++ b/packages/create-carbon/src/tools/templateProcessor.ts @@ -49,7 +49,7 @@ interface FrontMatter extends Record { export const processTemplate = async ( context: Omit ) => { - const templatePath = resolve(__dirname, "../../template") + const templatePath = resolve(import.meta.dirname, "../../template") debug("Processing template") debug("Getting dependency versions") const packageVersions = await getDependencyVersions() diff --git a/packages/create-carbon/template/.env.hbs b/packages/create-carbon/template/.env.hbs index 671207fa..3e8f322f 100644 --- a/packages/create-carbon/template/.env.hbs +++ b/packages/create-carbon/template/.env.hbs @@ -9,9 +9,13 @@ path: .env --- +{{#if plugins.linkedRoles}} BASE_URL= +{{/if}} DEPLOY_SECRET= DISCORD_CLIENT_ID= +{{#if plugins.linkedRoles}} DISCORD_CLIENT_SECRET= +{{/if}} DISCORD_PUBLIC_KEY= DISCORD_BOT_TOKEN= \ No newline at end of file diff --git a/packages/create-carbon/template/index.ts.hbs b/packages/create-carbon/template/index.ts.hbs index 560a13bf..2190a4c5 100644 --- a/packages/create-carbon/template/index.ts.hbs +++ b/packages/create-carbon/template/index.ts.hbs @@ -33,10 +33,14 @@ import ButtonCommand from "./commands/button.js" const handle = createHandle((env) => { const client = new Client( { + {{#if plugins.linkedRoles}} baseUrl: String(env.BASE_URL), + {{/if}} deploySecret: String(env.DEPLOY_SECRET), clientId: String(env.DISCORD_CLIENT_ID), + {{#if plugins.linkedRoles}} clientSecret: String(env.DISCORD_CLIENT_SECRET), + {{/if}} publicKey: String(env.DISCORD_PUBLIC_KEY), token: String(env.DISCORD_BOT_TOKEN) }, diff --git a/website/content/adapters/bun.mdx b/website/content/adapters/bun.mdx index 3aabc828..f5fa2f3f 100644 --- a/website/content/adapters/bun.mdx +++ b/website/content/adapters/bun.mdx @@ -65,7 +65,7 @@ First things first, you'll need to grab your Discord application's secrets from ### Set Up a Proxy -Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, set it as `BASE_URL=""` in your `.env` file. +Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, you may want to set it as `BASE_URL=""` in your `.env` file. diff --git a/website/content/adapters/cloudflare.mdx b/website/content/adapters/cloudflare.mdx index 20bb46f1..f125a61c 100644 --- a/website/content/adapters/cloudflare.mdx +++ b/website/content/adapters/cloudflare.mdx @@ -66,7 +66,7 @@ First things first, you'll need to grab your Discord application's secrets from ### Start a Proxy -Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, set it as `BASE_URL=""` in your `.dev.vars` file. +Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, you may want to set it as `BASE_URL=""` in your `.dev.vars` file. @@ -116,10 +116,8 @@ Before deploying your bot, you'll need to set your environment variables. This c ..workers.dev", "wrangler secret put DISCORD_PUBLIC_KEY", "wrangler secret put DISCORD_CLIENT_ID", - "wrangler secret put DISCORD_CLIENT_SECRET", "wrangler secret put DISCORD_BOT_TOKEN", ]} /> diff --git a/website/content/adapters/next.mdx b/website/content/adapters/next.mdx index 14bf00e4..2a396db4 100644 --- a/website/content/adapters/next.mdx +++ b/website/content/adapters/next.mdx @@ -72,7 +72,7 @@ First things first, you'll need to grab your Discord application's secrets from ### Start a Proxy -Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, set it as `BASE_URL="/api/discord"` in your `.env.local` file. +Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, you may want to set it as `BASE_URL="/api/discord"` in your `.env.local` file. diff --git a/website/content/adapters/node.mdx b/website/content/adapters/node.mdx index f3f479cc..99af1495 100644 --- a/website/content/adapters/node.mdx +++ b/website/content/adapters/node.mdx @@ -66,7 +66,7 @@ First things first, you'll need to grab your Discord application's secrets from ### Start a Proxy -Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, set it as `BASE_URL=""` in your `.env` file. +Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using [`localtunnel`](https://www.npmjs.com/package/localtunnel). Once you have the public URL, you may want to set it as `BASE_URL=""` in your `.env` file. diff --git a/website/content/classes/client.mdx b/website/content/classes/client.mdx index 65f1ef6e..66ba8ca7 100644 --- a/website/content/classes/client.mdx +++ b/website/content/classes/client.mdx @@ -13,10 +13,8 @@ A client must be created within your [`createHandle`](/api/index/functions/creat ```ts title="src/index.ts" const handle = createHandle((env) => { const client = new Client({ - baseUrl: String(env.BASE_URL), deploySecret: String(env.DEPLOY_SECRET), clientId: String(env.CLIENT_ID), - clientSecret: String(env.CLIENT_SECRET), publicKey: String(env.PUBLIC_KEY), token: String(env.TOKEN), }, [new PingCommand()]) @@ -26,10 +24,8 @@ const handle = createHandle((env) => { Here we have created a client with the following options: -- `baseUrl`: The base URL of your bot, relative to your public URL - `deploySecret`: The deploy secret of your bot, used as a password for deploying commands and other sensitive matters - `clientId`: The Discord client ID of your bot -- `clientSecret`: The Discord client secret of your bot - `publicKey`: The Discord public key of your bot - `token`: The Discord token of your bot diff --git a/website/content/classes/modals.mdx b/website/content/classes/modals.mdx index 2c6e095e..0007e09f 100644 --- a/website/content/classes/modals.mdx +++ b/website/content/classes/modals.mdx @@ -26,6 +26,7 @@ class ModalCommand extends Modal { ) } } + class TextInputHi extends TextInput { label = "Tell me about your life" customId = "life" diff --git a/website/content/getting-started/basic-setup.mdx b/website/content/getting-started/basic-setup.mdx index 3d3ad7c5..ef167988 100644 --- a/website/content/getting-started/basic-setup.mdx +++ b/website/content/getting-started/basic-setup.mdx @@ -46,10 +46,8 @@ import { createHandle, Client } from "@buape/carbon"; const handle = createHandle((env) => { const client = new Client( { - baseUrl: String(env.BASE_URL), deploySecret: String(env.DEPLOY_SECRET), clientId: String(env.DISCORD_CLIENT_ID), - clientSecret: String(env.DISCORD_CLIENT_SECRET), publicKey: String(env.DISCORD_PUBLIC_KEY), token: String(env.DISCORD_TOKEN), }, diff --git a/website/content/plugins/linked-roles.mdx b/website/content/plugins/linked-roles.mdx index 70f85961..0d96f37c 100644 --- a/website/content/plugins/linked-roles.mdx +++ b/website/content/plugins/linked-roles.mdx @@ -40,7 +40,11 @@ import { createHandle, Client, ApplicationRoleConnectionMetadataType } from "@bu import { LinkedRoles } from "@buape/carbon/linked-roles" const handle = createHandle((env) => { - const client = new Client({ ... }, [ ... ]) + const client = new Client({ + // Add these options and environment variables + baseUrl: String(env.BASE_URL), + clientSecret: String(env.CLIENT_SECRET), + }, [ ... ]) const linkedRoles = new LinkedRoles(client, { metadata: [ {