Skip to content

Commit

Permalink
test bot works omg
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilloftheshadow committed Apr 16, 2024
1 parent 593c202 commit 5535aba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/rocko/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PingCommand extends Command {
defer = true

async run(interaction: Interaction) {
sleep(7500)
await sleep(7500)
interaction.reply({ content: "Pong" })
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/carbon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"license": "MIT",
"dependencies": {
"discord-api-types": "^0.37.79",
"discord-api-types": "0.37.79",
"discord-verify": "1.2.0",
"hono": "4.2.4"
"itty-router": "5.0.15"
}
}
60 changes: 23 additions & 37 deletions packages/carbon/src/classes/Client.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { ClientOptions } from "../typings.js";
import { type Context, Hono } from "hono"
import { verify } from "discord-verify";
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";

export class Client {
options: ClientOptions
commands: Command[]
router: Hono
router: ReturnType<typeof AutoRouter>
constructor(options: ClientOptions, commands: Command[]) {
this.options = options
this.commands = commands
this.router = new Hono()
this.router = AutoRouter()
this.setupRoutes()
this.deployCommands()
}
Expand All @@ -37,27 +37,26 @@ export class Client {
}

private setupRoutes() {
this.router.get("/", (c) => {
if (this.options.redirectUrl) return c.redirect(this.options.redirectUrl)
c.status(401)
return c.json({ error: "Unauthorized" })
this.router.get("/", () => {
if (this.options.redirectUrl) return Response.redirect(this.options.redirectUrl, 302)
throw new StatusError(404)
})
this.router.post("/interaction", async (c) => {
const isValid = await this.validateInteraction(c)
this.router.post("/interaction", async (req: IRequestStrict) => {
const isValid = await this.validateInteraction(req)
if (!isValid) {
c.status(401)
return c.json({ error: "Invalid request signature." })
return new Response("Invalid request signature", { status: 401 })
}

const rawInteraction = c.req.json() as unknown as APIInteraction
const rawInteraction = await req.json() as unknown as APIInteraction
console.log(rawInteraction)
if (rawInteraction.type === InteractionType.Ping) {
return c.json({
return json({
type: InteractionResponseType.Pong
})
}

if (rawInteraction.type !== InteractionType.ApplicationCommand) {
return c.json({
return json({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Interaction type not supported"
Expand All @@ -66,18 +65,18 @@ export class Client {
}

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

const interaction = new Interaction(rawInteraction)

if (command.defer) {
command.run(interaction)
return c.json({
return json({
type: InteractionResponseType.DeferredChannelMessageWithSource,
flags: command.ephemeral ? MessageFlags.Ephemeral : 0
})
}
return c.json({
return json({
type: InteractionResponseType.ChannelMessageWithSource,
content: "Man someone should really implement non-deferred replies huh"
})
Expand All @@ -87,27 +86,14 @@ export class Client {

}

private async validateInteraction(c: Context) {
if (c.req.method !== "POST") {
c.status(405)
return c.json({ error: "Method not allowed." })
private async validateInteraction(req: IRequestStrict) {
if (req.method !== "POST") {
throw new StatusError(405)
}
if (
!c.req.header("x-signature-ed25519") ||
!c.req.header("x-signature-timestamp")
) {
c.status(401)
return c.json({ error: "Invalid request signature." })
}
const signature = c.req.header("x-signature-ed25519")
const timestamp = c.req.header("x-signature-timestamp")
const body = JSON.stringify(c.req.json())
const isValid = await verify(
body,
signature,
timestamp,
const isValid = await isValidRequest(
req,
this.options.publicKey,
crypto.subtle
PlatformAlgorithm.NewNode
)
return isValid
}
Expand Down
17 changes: 8 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5535aba

Please sign in to comment.