Skip to content

Commit

Permalink
chore: update documentation with new adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
apteryxxyz committed Jan 19, 2025
1 parent c32530b commit e14fcdc
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 207 deletions.
4 changes: 2 additions & 2 deletions website/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "./global.css"
import { RootProvider } from "fumadocs-ui/provider"
import { Rubik } from "next/font/google"
import type { ReactNode } from "react"
import { baseUrl, createMetadata } from "./og/[...slug]/metadata"
import Script from "next/script"
import type { ReactNode } from "react"
import { env } from "~/env.mjs"
import { baseUrl, createMetadata } from "./og/[...slug]/metadata"

const rubik = Rubik({
subsets: ["latin"],
Expand Down
10 changes: 4 additions & 6 deletions website/content/adapters/bun.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ icon: Dessert
import { Step, Steps } from "fumadocs-ui/components/steps";
import { Workflow, Server } from "lucide-react";

## Setup

<Cards>
<Card
icon={<Workflow />}
Expand All @@ -24,7 +22,7 @@ import { Workflow, Server } from "lucide-react";
/>
</Cards>

### Manual Setup
## Manual Setup

This is a continuation of the [Basic Setup](/getting-started/basic-setup) guide. If you haven't already, make sure to follow the steps in the guide before proceeding.

Expand All @@ -44,9 +42,9 @@ Using the `@buape/carbon/adapters/bun` package, you can create a server to host
```ts
import { createServer } from '@buape/carbon/adapters/bun'

const handle = createHandle( ... )
const client = new Client( ... )

createServer(handle, { port: 3000 })
createServer(client, { port: 3000 })
```

</Step>
Expand Down Expand Up @@ -143,7 +141,7 @@ You may also want to set up a process manager like [PM2](https://npmjs.com/packa
</Step>

<Callout type="info">
Reminder to deploy your commands to Discord using `<BASE_URL>/deploy?secret=<DEPLOY_SECRET>`.
Remember to deploy your commands to Discord using `<BASE_URL>/deploy?secret=<DEPLOY_SECRET>`.
</Callout>

</Steps>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Workflow, Server } from "lucide-react";
/>
</Cards>

### Manual Setup
## Manual Setup

This is a continuation of the [Basic Setup](/getting-started/basic-setup) guide. If you haven't already, make sure to follow the steps in the guide before proceeding.

Expand All @@ -37,36 +37,59 @@ This is a continuation of the [Basic Setup](/getting-started/basic-setup) guide.
<div class="step" style={{ display: 'hidden' }} />

<Step>
### Create a Handler
### Create a Fetch Handler

Using the `@buape/carbon/adapters/cloudflare` package, you can create a handler that you can then export for Cloudflare Workers. This server will handle incoming interactions and route them to your bot.

```ts
import { createHandler } from '@buape/carbon/adapters/cloudflare'
```ts title="src/index.ts"
import { createHandler } from '@buape/carbon/adapters/fetch'

const handle = createHandle( ... )
const client = new Client( ... )

const handler = createHandler(handle)
const handler = createHandler(client)
export default { fetch: handler }
```

</Step>
</Steps>

## Running in Development
<Step>
### Create a Entry Point File

To access environment variables globally in a Cloudflare Worker, Carbon uses a workaround by assigning the `process.env` object to the `globalThis` object before importing the main handler file. This approach allows you to access environment variables at the top level of your handler file, something that is not normally possible in Cloudflare Workers.

```ts title="src/entry.ts"
import type { ExecutionContext } from '@cloudflare/workers-types'

export default {
fetch(req: Request, env: Record<string, string>, ctx: ExecutionContext) {
Reflect.set(globalThis, 'process', { env })
const handle = await import('./index.js')
return handle.default.fetch(req, ctx)
}
}
```
</Step>

<Steps>
<Step>
### Set Environment Variables
### Add a Wrangler Configuration

First things first, you'll need to grab your Discord application's secrets from the [Developer Portal](https://discord.com/developers/applications) and paste them in your `.dev.vars` file.
You'll need to create a `wrangler.toml` file in the root of your project to configure your Cloudflare Worker to use the entry point file you created. This file should look something like this:

```toml title="wrangler.toml"
name = " ... "
main = "src/entry.ts"
compatibility_date = " ... "
```
</Step>
</Steps>

## Running in Development

<Steps>
<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, you may want to 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). This created public URL will be referred to as `PUBLIC_URL` in the following steps.

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

Expand All @@ -75,6 +98,13 @@ You can use the `--subdomain` flag to specify a custom subdomain for your proxy.
</Callout>
</Step>

<Step>
### Set Environment Variables

First things first, you'll need to grab your Discord application's secrets from the [Developer Portal](https://discord.com/developers/applications) and paste them in your `.dev.vars` file.

</Step>

<Step>
### Configure Portal URLs

Expand Down Expand Up @@ -116,6 +146,7 @@ Before deploying your bot, you'll need to set your environment variables. This c
<PackageManager.Run
executor
scripts={[
"wrangler secret put BASE_URL",
"wrangler secret put DISCORD_PUBLIC_KEY",
"wrangler secret put DISCORD_CLIENT_ID",
"wrangler secret put DISCORD_BOT_TOKEN",
Expand Down
107 changes: 107 additions & 0 deletions website/content/adapters/fetch/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: Fetch
icon: Workflow
---

import { Step, Steps } from "fumadocs-ui/components/steps";
import { Workflow, Server } from "lucide-react";

## Fetch Request Handler

A Carbon bot can be deployed in any environment or framework that supports the fetch request handler API. This flexibility ensures seamless integration across different setups without the need for custom adapters.

```ts
import { createHandler } from '@buape/carbon/adapters/fetch'

const client = new Client( ... )

const handler = createHandler(client)
// ^? ((req: Request) => Promise<Response>)
// This new handler can be used with any fetch-compatible environment or framework
```

### Customizing Request Handling

You can also extend this approach to customize routing and request handling for different paths or use cases.

```ts
const handler = createHandler(client)

// Bun or other frameworks
Bun.serve({
fetch(req: Request) {
const url = new URL(req.url)
if (url.startsWith('/api/discord')) {
return handler(req)
} else {
// Handle other requests
}
}
})
```

## Running in Development

How you run your Carbon bot in development depends on the environment or framework you're using. Here are some common setups:

<Steps>
<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). This created public URL will be referred to as `PUBLIC_URL` in the following steps.

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

<Callout type="info">
You can use the `--subdomain` flag to specify a custom subdomain for your proxy.
</Callout>
</Step>

<Step>
### Set Environment Variables

First things first, you'll need to grab your public URL and your Discord application's secrets from the [Developer Portal](https://discord.com/developers/applications) and paste them into whatever environment variables file your setup uses.

<Callout type="warn">
`BASE_URL` should be your public URL plus the relative path - if any - to your bot's handler.
You can rename this variable if it conflicts with your existing environment variables.
</Callout>

</Step>

<Step>
### Configure Portal URLs

Now that you have a public URL, navigate back to the [Discord Developer Portal](https://discord.com/developers/applications) and set the "Interactions Endpoint URL" to `<BASE_URL>/interactions`.

</Step>

<Step>
### Invite your App

You'll need to invite your app to your server to interact with it. To do so, navigate to the Installation tab of your app in the [Discord Developer Portal](https://discord.com/developers/applications).

</Step>

<Step>
### Run the Bot

How you run your bot in development depends on the environment or framework you're using.
</Step>

<Step>
### Deploy Your Commands to Discord

Finally, to deploy your commands to Discord, navigate to `<BASE_URL>/deploy?secret=<DEPLOY_SECRET>` in your browser. This will send your command data to Discord to register them with your bot.
</Step>
</Steps>

## Deploying to Production

Deploying your Carbon bot to production also depends on the environment or framework you're using. You'll need to refer to the specific documentation for your setup.

<Callout type="info">
Remember,
- you'll need a public URL for Discord to access your bot
- to deploy your commands to Discord using `<BASE_URL>/deploy?secret=<DEPLOY_SECRET>`.
</Callout>
3 changes: 3 additions & 0 deletions website/content/adapters/fetch/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"defaultOpen": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ Ensure the file where you export your handler is placed at `src/app/api/discord/
<Step>
### Create a Handler

Using the `@buape/carbon/adapters/next` package, you can create a handler that you can then export for Next.js API routes. This server will handle incoming interactions and route them to your bot.
Using the `@buape/carbon/adapters/fetch` package, you can create a handler that you can then export for Next.js API routes. This will handle incoming interactions and route them to your bot.

```ts
import { createHandler } from '@buape/carbon/adapters/next'
import { createHandler } from '@buape/carbon/adapters/fetch'

const handle = createHandle( ... )
const client = new Client( ... )

const handler = createHandler(handle)
const handler = createHandler(client)
export { handler as GET, handler as POST }
```

Expand All @@ -62,17 +62,10 @@ export { handler as GET, handler as POST }
## Running in Development

<Steps>
<Step>
### Set Environment Variables

First things first, you'll need to grab your Discord application's secrets from the [Developer Portal](https://discord.com/developers/applications) and paste them in your `.env.local` file.

</Step>

<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, you may want to 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). This created public URL will be referred to as `PUBLIC_URL` in the following steps.

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

Expand All @@ -82,16 +75,24 @@ You can use the `--subdomain` flag to specify a custom subdomain for your proxy.
</Step>

<Step>
### Configure Portal URLs
### Set Environment Variables

Now that you have a public URL, navigate back to the [Discord Developer Portal](https://discord.com/developers/applications) and set the "Interactions Endpoint URL" to `<BASE_URL>/interactions`.
First things first, you'll need to grab your Discord application's secrets from the [Developer Portal](https://discord.com/developers/applications) and paste them in your `.env.local` file.

<Callout type="warn">
`<BASE_URL>` refers to the public URL plus the relative path to your Next.js API routes, likely `/api/discord`.
`BASE_URL` should be your public URL **plus the relative path** to your Next.js API routes, likely `/api/discord`.
You can rename this variable if it conflicts with your existing environment variables.
</Callout>

</Step>

<Step>
### Configure Portal URLs

Now that you have a public URL, navigate back to the [Discord Developer Portal](https://discord.com/developers/applications) and set the "Interactions Endpoint URL" to `<BASE_URL>/interactions`.

</Step>

<Step>
### Invite your App

Expand Down
Loading

0 comments on commit e14fcdc

Please sign in to comment.