Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilloftheshadow committed Apr 16, 2024
1 parent d5b64db commit 9d6fd9b
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 78 deletions.
10 changes: 2 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"dotenv.enableAutocloaking": false,
"cSpell.words": [
"biomejs",
"Buape",
"carbonjs",
"hono",
"Rocko"
],
"cSpell.words": ["biomejs", "Buape", "carbonjs", "hono", "Rocko"],
"editor.defaultFormatter": "biomejs.biome"
}
}
2 changes: 1 addition & 1 deletion apps/rocko/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"carbon": "workspace:*"
},
"license": "MIT"
}
}
22 changes: 12 additions & 10 deletions apps/rocko/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, type Interaction, Command } from "carbon"
import { serve } from "@carbonjs/nodejs"
import { Client, Command, type Interaction } from "carbon"

class PingCommand extends Command {
name = "ping"
Expand All @@ -12,15 +12,17 @@ class PingCommand extends Command {
}
}

const client = new Client({
clientId: process.env.CLIENT_ID!,
publicKey: process.env.PUBLIC_KEY!,
token: process.env.DISCORD_TOKEN!
}, [
new PingCommand()
])
const client = new Client(
{
clientId: process.env.CLIENT_ID!,
publicKey: process.env.PUBLIC_KEY!,
token: process.env.DISCORD_TOKEN!
},
[new PingCommand()]
)

serve(client, { port: 3000 })

const sleep = async (ms: number) => { return new Promise(resolve => setTimeout(resolve, ms)) }

const sleep = async (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
7 changes: 2 additions & 5 deletions apps/rocko/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"include": [
"src/**/*.ts",
"src/*.ts"
],
"include": ["src/**/*.ts", "src/*.ts"],
"compilerOptions": {
"outDir": "./dist",
"rootDir": "."
}
}
}
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"trailingComma": "none"
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"pierre": "^2.0.30",
"typescript": "5.4.5"
}
}
}
35 changes: 21 additions & 14 deletions packages/carbon/src/classes/Client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type { ClientOptions } from "../typings.js";
import { AutoRouter, StatusError, json, type IRequestStrict } from "itty-router"
import { PlatformAlgorithm, isValidRequest } from "discord-verify";
import { type APIInteraction, InteractionResponseType, InteractionType, MessageFlags, Routes, RouteBases } from "discord-api-types/v10";
import type { Command } from "../structures/Command.js";
import { Interaction } from "./Interaction.js";
import {
type APIInteraction,
InteractionResponseType,
InteractionType,
MessageFlags,
RouteBases,
Routes
} from "discord-api-types/v10"
import { PlatformAlgorithm, isValidRequest } from "discord-verify"
import { AutoRouter, type IRequestStrict, StatusError, json } from "itty-router"
import type { Command } from "../structures/Command.js"
import type { ClientOptions } from "../typings.js"
import { Interaction } from "./Interaction.js"

export class Client {
options: ClientOptions
Expand Down Expand Up @@ -33,12 +40,13 @@ export class Client {
body: JSON.stringify(commands)
}
)
} catch { }
} catch {}
}

private setupRoutes() {
this.router.get("/", () => {
if (this.options.redirectUrl) return Response.redirect(this.options.redirectUrl, 302)
if (this.options.redirectUrl)
return Response.redirect(this.options.redirectUrl, 302)
throw new StatusError(404)
})
this.router.post("/interaction", async (req: IRequestStrict) => {
Expand All @@ -47,7 +55,7 @@ export class Client {
return new Response("Invalid request signature", { status: 401 })
}

const rawInteraction = await req.json() as unknown as APIInteraction
const rawInteraction = (await req.json()) as unknown as APIInteraction
if (rawInteraction.type === InteractionType.Ping) {
return json({
type: InteractionResponseType.Pong
Expand All @@ -63,7 +71,9 @@ export class Client {
})
}

const command = this.commands.find(x => x.name === rawInteraction.data.name)
const command = this.commands.find(
(x) => x.name === rawInteraction.data.name
)
if (!command) return new Response(null, { status: 400 })

const interaction = new Interaction(rawInteraction)
Expand All @@ -79,10 +89,7 @@ export class Client {
type: InteractionResponseType.ChannelMessageWithSource,
content: "Man someone should really implement non-deferred replies huh"
})


})

}

private async validateInteraction(req: IRequestStrict) {
Expand All @@ -96,4 +103,4 @@ export class Client {
)
return isValid
}
}
}
32 changes: 23 additions & 9 deletions packages/carbon/src/classes/Interaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { RouteBases, Routes, type APIInteraction, type InteractionType, type RESTPostAPIChannelMessageJSONBody } from "discord-api-types/v10";
import {
type APIInteraction,
type InteractionType,
type RESTPostAPIChannelMessageJSONBody,
RouteBases,
Routes
} from "discord-api-types/v10"

export class Interaction {
type: InteractionType
Expand All @@ -9,12 +15,20 @@ export class Interaction {
}

async reply(data: RESTPostAPIChannelMessageJSONBody) {
fetch(RouteBases.api + Routes.webhookMessage(this.#data.application_id, this.#data.token, "@original"), {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
fetch(
RouteBases.api +
Routes.webhookMessage(
this.#data.application_id,
this.#data.token,
"@original"
),
{
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}
)
}
}
}
2 changes: 1 addition & 1 deletion packages/carbon/src/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export abstract class Command {
description: this.description
} satisfies RESTPostAPIChatInputApplicationCommandsJSONBody
}
}
}
2 changes: 1 addition & 1 deletion packages/carbon/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export type ClientOptions = {
clientId: string
publicKey: string
token: string
}
}
4 changes: 2 additions & 2 deletions packages/carbon/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from "vitest"
import { expect, test } from "vitest"
import { hello } from "../src"

test("It works", () => expect(hello()).toBe("Hello world"));
test("It works", () => expect(hello()).toBe("Hello world"))
2 changes: 1 addition & 1 deletion packages/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import type { Client } from "carbon"
export const serve = (client: Client, options: { port: number }) => {
console.log(`Serving client on port ${options.port}`)
honoServe({ fetch: client.router.fetch, port: options.port })
}
}
4 changes: 2 additions & 2 deletions packages/nodejs/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from "vitest"
import { expect, test } from "vitest"
import { hello } from "../src"

test("It works", () => expect(hello()).toBe("Hello world"));
test("It works", () => expect(hello()).toBe("Hello world"))
14 changes: 4 additions & 10 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": [
"^build"
],
"outputs": [
"dist/**"
],
"dependsOn": ["^build"],
"outputs": ["dist/**"],
"cache": true
},
"test": {},
"start": {
"dependsOn": [
"build"
]
"dependsOn": ["build"]
},
"dev": {},
"typecheck": {}
}
}
}
24 changes: 12 additions & 12 deletions turbo/generators/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PlopTypes } from "@turbo/gen";
import type { PlopTypes } from "@turbo/gen"

export default function generator(plop: PlopTypes.NodePlopAPI): void {
// create a generator
Expand All @@ -8,36 +8,36 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
{
type: "input",
name: "name",
message: "What is the name of the package?",
},
message: "What is the name of the package?"
}
],
actions: [
// Top Level Files
{
type: "add",
path: "packages/{{name}}/package.json",
templateFile: "templates/package.json.hbs",
templateFile: "templates/package.json.hbs"
},
{
type: "add",
path: "packages/{{name}}/tsconfig.json",
templateFile: "templates/tsconfig.json.hbs",
templateFile: "templates/tsconfig.json.hbs"
},
{
type: "add",
path: "packages/{{name}}/README.md",
templateFile: "templates/README.md.hbs",
templateFile: "templates/README.md.hbs"
},
{
type: "add",
path: "packages/{{name}}/src/index.ts",
templateFile: "templates/src/index.ts.hbs",
templateFile: "templates/src/index.ts.hbs"
},
{
type: "add",
path: "packages/{{name}}/tests/index.test.ts",
templateFile: "templates/tests/index.test.ts.hbs",
},
],
});
}
templateFile: "templates/tests/index.test.ts.hbs"
}
]
})
}

0 comments on commit 9d6fd9b

Please sign in to comment.