Skip to content

Commit

Permalink
start making this cf compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilloftheshadow committed May 6, 2024
1 parent 98492ee commit 53026d1
Show file tree
Hide file tree
Showing 13 changed files with 726 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist
.turbo
.pnpm-store
.env
./docs
./docs
wrangler.toml
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"biomejs",
"Buape",
"carbonjs",
"Cloudo",
"hono",
"Rocko",
"whatwg"
Expand Down
3 changes: 3 additions & 0 deletions apps/cloudo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Cloudo

Cloudo is our test bot that runs on CF workers, to test that Carbon functions correctly.
18 changes: 18 additions & 0 deletions apps/cloudo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cloudo",
"version": "0.0.0",
"type": "module",
"main": "./dist/src/index.js",
"scripts": {
"build": "wrangler deploy",
"dev": "wrangler deploy && wrangler tail"
},
"dependencies": {
"@buape/carbon": "workspace:*"
},
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20240502.0",
"wrangler": "3.53.1"
}
}
11 changes: 11 additions & 0 deletions apps/cloudo/src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command, type CommandInteraction } from "@buape/carbon"

export default class PingCommand extends Command {
name = "ping"
description = "A simple ping command"
defer = true

async run(interaction: CommandInteraction) {
interaction.reply({ content: "Pong <:caughtIn4k:1145473115703496816>" })
}
}
38 changes: 38 additions & 0 deletions apps/cloudo/src/commands/testing/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Button,
type ButtonInteraction,
ButtonStyle,
Command,
type CommandInteraction,
LinkButton,
Row
} from "@buape/carbon"

export default class ButtonCommand extends Command {
name = "button"
description = "A simple command with a button!"
defer = true

async run(interaction: CommandInteraction) {
interaction.reply({
content: "Pong <:caughtIn4k:1145473115703496816>",
components: [new Row([new PingButton(), new Link()])]
})
}
}

class PingButton extends Button {
customId = "ping"
label = "Ping"
style = ButtonStyle.Primary as typeof Button.prototype.style

async run(interaction: ButtonInteraction) {
interaction.reply({ content: "OMG YOU CLICKED THE BUTTON" })
}
}

class Link extends LinkButton {
label = "Link"
style = ButtonStyle.Link as typeof Button.prototype.style
url = "https://google.com"
}
27 changes: 27 additions & 0 deletions apps/cloudo/src/commands/testing/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
type APIApplicationCommandBasicOption,
ApplicationCommandOptionType,
Command,
type CommandInteraction
} from "@buape/carbon"

export default class Options extends Command {
name = "options"
description = "Options test"
defer = true

options: APIApplicationCommandBasicOption[] = [
{
name: "str",
type: ApplicationCommandOptionType.String,
description: "DESCRIPTION",
required: true
}
]

async run(interaction: CommandInteraction) {
interaction.reply({
content: `${interaction.options.str}`
})
}
}
32 changes: 32 additions & 0 deletions apps/cloudo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ExecutionContext } from "@cloudflare/workers-types/2023-07-01"
import { Client, ClientMode } from "@buape/carbon"

import ButtonCommand from "./commands/testing/button.js"
import Options from "./commands/testing/options.js"
import PingCommand from "./commands/ping.js"

type Env = {
CLIENT_ID: string
PUBLIC_KEY: string
DISCORD_TOKEN: string
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const client = new Client(
{
clientId: env.CLIENT_ID,
publicKey: env.PUBLIC_KEY,
token: env.DISCORD_TOKEN,
mode: ClientMode.CloudflareWorkers
},
[new ButtonCommand(), new Options(), new PingCommand()]
)
if (request.url.endsWith("/deploy")) {
await client.deployCommands()
return new Response("Deployed commands")
}
ctx.waitUntil(client.router.fetch(request))
return new Response("OK")
}
}
9 changes: 9 additions & 0 deletions apps/cloudo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*.ts", "src/*.ts"],
"compilerOptions": {
"outDir": "./dist",
"rootDir": ".",
"types": ["@cloudflare/workers-types"]
}
}
8 changes: 8 additions & 0 deletions apps/cloudo/wrangler.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = "carbon-test"
main = "src/index.ts"
compatibility_date = "2023-03-20"

[vars]
CLIENT_ID = ""
PUBLIC_KEY = ""
DISCORD_TOKEN = ""
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Carbon monorepo",
"scripts": {
"build": "turbo run build",
"cf": "pnpm build --filter carbon... && pnpm dev --filter cloudo",
"dev": "dotenv -- turbo run dev",
"lint": "biome check . --apply",
"preinstall": "npx only-allow pnpm",
Expand Down
26 changes: 21 additions & 5 deletions packages/carbon/src/classes/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ import { CommandWithSubcommandGroups } from "./CommandWithSubcommandGroups.js"
import { CommandWithSubcommands } from "./CommandWithSubcommands.js"
import { ComponentHandler } from "../structures/ComponentHandler.js"

/**
* The mode that the client is running in.
* Different platforms have different requirements for how processes are handled.
*/
export enum ClientMode {
NodeJS = "node",
CloudflareWorkers = "cloudflare",
Vercel = "vercel"
}

/**
* The options used for initializing the client
*/
Expand All @@ -29,6 +39,7 @@ export type ClientOptions = {
clientId: string
publicKey: string
token: string
mode: ClientMode
}

/**
Expand Down Expand Up @@ -71,13 +82,14 @@ export class Client {
}).setToken(options.token)
this.componentHandler = new ComponentHandler(this)
this.setupRoutes()
this.deployCommands()
if (this.options.mode === ClientMode.NodeJS) this.deployCommands()
}

/**
* Deploy the commands registered to Discord
* Deploy the commands registered to Discord.
* This is automatically called when running in NodeJS mode.
*/
private async deployCommands() {
async deployCommands() {
try {
const commands = this.commands.map((command) => {
return command.serialize()
Expand All @@ -93,6 +105,7 @@ export class Client {
body: JSON.stringify(commands)
}
)
console.log(`Deployed ${commands.length} commands to Discord`)
} catch (err) {
console.error("Failed to deploy commands")
console.error(err)
Expand Down Expand Up @@ -132,7 +145,6 @@ export class Client {
rawInteraction,
command
)
console.log(1)

if (command instanceof Command) {
if (command.defer) {
Expand Down Expand Up @@ -254,7 +266,11 @@ export class Client {
const isValid = await isValidRequest(
req,
this.options.publicKey,
PlatformAlgorithm.NewNode
this.options.mode === ClientMode.CloudflareWorkers
? PlatformAlgorithm.Cloudflare
: this.options.mode === ClientMode.Vercel
? PlatformAlgorithm.VercelProd
: PlatformAlgorithm.NewNode
)
return isValid
}
Expand Down
Loading

0 comments on commit 53026d1

Please sign in to comment.