Skip to content

Commit

Permalink
refactor: replace creating handle with new adapters (#205)
Browse files Browse the repository at this point in the history
* refactor: replace creating handle with new adapters

* chore: update example apps to use new adapters

* chore: update documentation with new adapters

* chore: remove some headings

* chore: add description to fetch adapter page

* fix: small url tweaks

* ci: re-combine ci files

---------

Co-authored-by: Shadow <[email protected]>
  • Loading branch information
apteryxxyz and thewilloftheshadow authored Jan 20, 2025
1 parent 309db77 commit 626f3c3
Show file tree
Hide file tree
Showing 40 changed files with 751 additions and 609 deletions.
6 changes: 6 additions & 0 deletions .changeset/wild-wombats-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@buape/carbon": minor
"create-carbon": minor
---

refactor: replace creating handle with new adapters
19 changes: 0 additions & 19 deletions .github/workflows/ci-main.yml

This file was deleted.

48 changes: 20 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: CI

on:
push:
branches:
- main
merge_group:
types: [checks_requested]
pull_request_target:
pull_request:
types:
- opened
- synchronize
Expand All @@ -12,42 +15,31 @@ on:
- "!renovate/web"

jobs:
build:
name: Check for Successful Build
validate:
name: ${{ matrix.task.name }}
timeout-minutes: 15
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
task:
- name: Check Build
run: pnpm run ci
- name: Check Formatting
run: biome ci .
- name: Run Tests
run: pnpm run test

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Build
run: pnpm run ci

biome:
name: Check Formatting
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Biome
if: ${{ matrix.task.run == 'biome ci .' }}
uses: biomejs/setup-biome@v2
- name: Run Biome
run: biome ci .

test:
name: Run Test Suites
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Run tests
run: pnpm run test
- name: ${{ matrix.task.name }}
run: ${{ matrix.task.run }}
2 changes: 1 addition & 1 deletion apps/cloudo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

This is a [Discord](https://discord.dev) app made with [Carbon](https://carbon.buape.com) and generated with the [`create-carbon`](https://npmjs.com/create-carbon) tool.

To learn how to get started in development, deploy to production, or add commands, head over to the [documentation](https://carbon.buape.com/adapters/cloudflare) for your runtime.
To learn how to get started in development, deploy to production, or add commands, head over to the [documentation](https://carbon.buape.com/adapters/fetch/cloudflare) for your runtime.

If you need any assistance, you can join our [Discord](https://go.buape.com/carbon) and ask in the [`#support`](https://discord.com/channels/1280628625904894072/1280630704308486174) channel.
19 changes: 19 additions & 0 deletions apps/cloudo/src/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ExecutionContext, Request } from "@cloudflare/workers-types"

declare global {
namespace NodeJS {
interface ProcessEnv extends Record<string, string | undefined> {}
interface Process {
env: ProcessEnv
}
}
var process: NodeJS.Process
}

export default {
async fetch(req: Request, env: NodeJS.ProcessEnv, ctx: ExecutionContext) {
Reflect.set(globalThis, "process", { env })
const mod = await import("./index.js")
return mod.default.fetch(req, ctx)
}
}
100 changes: 56 additions & 44 deletions apps/cloudo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, createHandle } from "@buape/carbon"
import { createHandler } from "@buape/carbon/adapters/cloudflare"
import { Client } from "@buape/carbon"
import { createHandler } from "@buape/carbon/adapters/fetch"
import {
ApplicationRoleConnectionMetadataType,
LinkedRoles
Expand All @@ -15,50 +15,62 @@ import SubcommandsCommand from "./commands/testing/subcommand.js"
import SubcommandGroupsCommand from "./commands/testing/subcommandgroup.js"
import UserCommand from "./commands/testing/user_command.js"

const handle = createHandle((env) => {
const client = new Client(
const linkedRoles = new LinkedRoles({
metadata: [
{
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_BOT_TOKEN)
},
[
// commands/*
new PingCommand(),
// commands/testing/*
new ButtonCommand(),
new EphemeralCommand(),
new EverySelectCommand(),
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand()
]
)
const linkedRoles = new LinkedRoles(client, {
metadata: [
{
key: "is_staff",
name: "Verified Staff",
description: "Whether the user is a verified staff member",
type: ApplicationRoleConnectionMetadataType.BooleanEqual
}
],
metadataCheckers: {
is_staff: async (userId) => {
const isAllowed = ["439223656200273932"]
if (isAllowed.includes(userId)) return true
return false
}
key: "is_staff",
name: "Verified Staff",
description: "Whether the user is a verified staff member",
type: ApplicationRoleConnectionMetadataType.BooleanEqual
}
})
return [client, linkedRoles]
],
metadataCheckers: {
is_staff: async (userId) => {
const isAllowed = ["439223656200273932"]
if (isAllowed.includes(userId)) return true
return false
}
}
})

const handler = createHandler(handle)
const client = new Client(
{
baseUrl: process.env.BASE_URL,
deploySecret: process.env.DEPLOY_SECRET,
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
publicKey: process.env.DISCORD_PUBLIC_KEY,
token: process.env.DISCORD_BOT_TOKEN
},
[
// commands/*
new PingCommand(),
// commands/testing/*
new ButtonCommand(),
new EphemeralCommand(),
new EverySelectCommand(),
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand()
],
[linkedRoles]
)

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

declare global {
namespace NodeJS {
interface ProcessEnv {
BASE_URL: string
DEPLOY_SECRET: string
DISCORD_CLIENT_ID: string
DISCORD_CLIENT_SECRET: string
DISCORD_PUBLIC_KEY: string
DISCORD_BOT_TOKEN: string
}
}
}
2 changes: 1 addition & 1 deletion apps/cloudo/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name = "cloudo"
main = "src/index.ts"
main = "src/entry.ts"
compatibility_date = "2024-10-18"
19 changes: 11 additions & 8 deletions apps/rocko/src/commands/testing/allow_mentions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Command, CommandInteraction } from "@buape/carbon";
import { Command, type CommandInteraction } from "@buape/carbon"

export default class MentionsCommand extends Command {
name = "mention"
description = "Allowed Mentions Test"
defer = true
name = "mention"
description = "Allowed Mentions Test"
defer = true

async run(interaction: CommandInteraction) {
await interaction.reply({content: `<@${interaction.userId}>`, allowedMentions: {parse: []}})
}
}
async run(interaction: CommandInteraction) {
await interaction.reply({
content: `<@${interaction.userId}>`,
allowedMentions: { parse: [] }
})
}
}
104 changes: 58 additions & 46 deletions apps/rocko/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "dotenv/config"
import { Client, createHandle } from "@buape/carbon"
import { Client } from "@buape/carbon"
import { createServer } from "@buape/carbon/adapters/node"
import {
ApplicationRoleConnectionMetadataType,
LinkedRoles
} from "@buape/carbon/linked-roles"
import PingCommand from "./commands/ping.js"
import MentionsCommand from "./commands/testing/allow_mentions.js"
import AttachmentCommand from "./commands/testing/attachment.js"
import ButtonCommand from "./commands/testing/button.js"
import EphemeralCommand from "./commands/testing/ephemeral.js"
Expand All @@ -16,53 +17,64 @@ import OptionsCommand from "./commands/testing/options.js"
import SubcommandsCommand from "./commands/testing/subcommand.js"
import SubcommandGroupsCommand from "./commands/testing/subcommandgroup.js"
import UserCommand from "./commands/testing/user_command.js"
import MentionsCommand from "./commands/testing/allow_mentions.js"

const handle = createHandle((env) => {
const client = new Client(
const linkedRoles = new LinkedRoles({
metadata: [
{
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_BOT_TOKEN)
},
[
// commands/*
new PingCommand(),
// commands/testing/*
new AttachmentCommand(),
new ButtonCommand(),
new EphemeralCommand(),
new EverySelectCommand(),
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand(),
new MentionsCommand()
]
)
const linkedRoles = new LinkedRoles(client, {
metadata: [
{
key: "is_staff",
name: "Verified Staff",
description: "Whether the user is a verified staff member",
type: ApplicationRoleConnectionMetadataType.BooleanEqual
}
],
metadataCheckers: {
is_staff: async (userId) => {
const isAllowed = ["439223656200273932"]
if (isAllowed.includes(userId)) return true
return false
}
key: "random",
name: "Verified Staff",
description: "Whether the user is a verified staff member",
type: ApplicationRoleConnectionMetadataType.BooleanEqual
}
],
metadataCheckers: {
random: async (userId) => {
const isAllowed = ["548150274414608399"]
if (isAllowed.includes(userId)) return true
return false
}
})
return [client, linkedRoles]
}
})

createServer(handle, { port: 3000 })
const client = new Client(
{
baseUrl: process.env.BASE_URL,
deploySecret: process.env.DEPLOY_SECRET,
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
publicKey: process.env.DISCORD_PUBLIC_KEY,
token: process.env.DISCORD_BOT_TOKEN
},
[
// commands/*
new PingCommand(),
// commands/testing/*
new AttachmentCommand(),
new ButtonCommand(),
new EphemeralCommand(),
new EverySelectCommand(),
new MessageCommand(),
new ModalCommand(),
new OptionsCommand(),
new SubcommandsCommand(),
new SubcommandGroupsCommand(),
new UserCommand(),
new MentionsCommand()
],
[linkedRoles]
)

createServer(client, { port: 3000 })

declare global {
namespace NodeJS {
interface ProcessEnv {
BASE_URL: string
DEPLOY_SECRET: string
DISCORD_CLIENT_ID: string
DISCORD_CLIENT_SECRET: string
DISCORD_PUBLIC_KEY: string
DISCORD_BOT_TOKEN: string
}
}
}
Loading

0 comments on commit 626f3c3

Please sign in to comment.