Skip to content

Commit

Permalink
refactor: change options requirements (#190)
Browse files Browse the repository at this point in the history
* refactor: change env vars and options requirements

* fix: trying to use `__dirname` in es module

* chore: add changesets
  • Loading branch information
apteryxxyz authored Oct 20, 2024
1 parent bfa538f commit 5d43f99
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-spoons-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-carbon": patch
---

fix: trying to use `__dirname` in es module
5 changes: 5 additions & 0 deletions .changeset/tricky-cougars-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@buape/carbon": patch
---

refactor: change env vars and options requirements
20 changes: 6 additions & 14 deletions packages/carbon/src/classes/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,15 @@ 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
*/
clientId: string
/**
* 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
*/
Expand Down Expand Up @@ -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
Expand All @@ -133,15 +125,15 @@ 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())
for (const modal of command.modals)
this.modalHandler.registerModal(new modal())
}
}
if (this.options.autoDeploy) {
if (options.autoDeploy) {
this.handleDeployRequest()
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/carbon/src/plugins/linked-roles/LinkedRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/create-carbon/src/tools/templateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface FrontMatter extends Record<string, string | undefined> {
export const processTemplate = async (
context: Omit<TemplateContext, "versions">
) => {
const templatePath = resolve(__dirname, "../../template")
const templatePath = resolve(import.meta.dirname, "../../template")
debug("Processing template")
debug("Getting dependency versions")
const packageVersions = await getDependencyVersions()
Expand Down
4 changes: 4 additions & 0 deletions packages/create-carbon/template/.env.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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=
4 changes: 4 additions & 0 deletions packages/create-carbon/template/index.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion website/content/adapters/bun.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ First things first, you'll need to grab your Discord application's secrets from
<Step>
### 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="<PUBLIC_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="<PUBLIC_URL>"` in your `.env` file.

<PackageManager.Run executor scripts={["localtunnel"]} />

Expand Down
4 changes: 1 addition & 3 deletions website/content/adapters/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ First things first, you'll need to grab your Discord application's secrets from
<Step>
### 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="<PUBLIC_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="<PUBLIC_URL>"` in your `.dev.vars` file.

<PackageManager.Run executor scripts={["localtunnel"]} />

Expand Down Expand Up @@ -116,10 +116,8 @@ Before deploying your bot, you'll need to set your environment variables. This c
<PackageManager.Run
executor
scripts={[
"wrangler secret put BASE_URL # e.g. https://<PROJECT_NAME>.<ACCOUNT_NAME>.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",
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion website/content/adapters/next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ First things first, you'll need to grab your Discord application's secrets from
<Step>
### 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="<PUBLIC_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="<PUBLIC_URL>/api/discord"` in your `.env.local` file.

<PackageManager.Run executor scripts={["localtunnel"]} />

Expand Down
2 changes: 1 addition & 1 deletion website/content/adapters/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ First things first, you'll need to grab your Discord application's secrets from
<Step>
### 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="<PUBLIC_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="<PUBLIC_URL>"` in your `.env` file.

<PackageManager.Run executor scripts={["localtunnel"]} />

Expand Down
4 changes: 0 additions & 4 deletions website/content/classes/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions website/content/classes/modals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ModalCommand extends Modal {
)
}
}

class TextInputHi extends TextInput {
label = "Tell me about your life"
customId = "life"
Expand Down
2 changes: 0 additions & 2 deletions website/content/getting-started/basic-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
6 changes: 5 additions & 1 deletion website/content/plugins/linked-roles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down

0 comments on commit 5d43f99

Please sign in to comment.