Skip to content

Commit

Permalink
squash!
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Sep 24, 2023
1 parent 859e054 commit 683d1bd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
58 changes: 35 additions & 23 deletions Source/Index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import type { ResponseInit } from "@cloudflare/workers-types";
import { Request, Response } from "@cloudflare/workers-types";
import {
InteractionResponseType,
InteractionType,
verifyKey,
} from "discord-interactions";
import { Router } from "itty-router";
import Environment from "./Library/Environment.js";
import type { Request, ResponseInit } from "@cloudflare/workers-types";

export const { default: Environment } = await import("./Object/Environment.js");

export const { Response } = await import("@cloudflare/workers-types");

class JSONResponse extends Response {
constructor(
Expand All @@ -22,24 +18,33 @@ class JSONResponse extends Response {
}
}

const router = Router();

router.post("/discord", async (request, env) => {
const message = await request["json"]();
(await import("itty-router")).Router().post("/discord", async (Request) => {
const Message = await Request["json"]();

if (message.type === InteractionType.PING) {
if (
Message.type ===
(await import("discord-interactions")).InteractionType.PING
) {
return new JSONResponse({
type: InteractionResponseType.PONG,
type: (await import("discord-interactions")).InteractionResponseType
.PONG,
});
}

if (message.type === InteractionType.APPLICATION_COMMAND) {
switch (message.data.name.toLowerCase()) {
if (
Message.type ===
(await import("discord-interactions")).InteractionType
.APPLICATION_COMMAND
) {
switch (Message.data.name.toLowerCase()) {
case "invite": {
return new JSONResponse({
type: 4,
data: {
content: `https://discord.com/oauth2/authorize?client_id=${env.DISCORD_APPLICATION_ID}&scope=applications.commands`,
content: `https://discord.com/oauth2/authorize?client_id=${
Environment.parse(process.env)
.DISCORD_APPLICATION_ID
}&scope=applications.commands`,
flags: 64,
},
});
Expand All @@ -56,16 +61,23 @@ router.post("/discord", async (request, env) => {
return new JSONResponse({ error: "Unknown Type" }, { status: 400 });
});

router.all("*", () => new Response("404 | Not Found.", { status: 404 }));
(await import("itty-router"))
.Router()
.all("*", () => new Response("404 | Not Found.", { status: 404 }));

export default {
async fetch(request: Request, _env = Environment) {
async fetch(
request: Request,
{ DISCORD_PUBLIC_KEY } = Environment.parse(process.env)
) {
if (request.method === "POST") {
const isValidRequest = verifyKey(
const isValidRequest = (
await import("discord-interactions")
).verifyKey(
await request.clone().arrayBuffer(),
request["headers"].get("x-signature-ed25519") ?? "",
request["headers"].get("x-signature-timestamp") ?? "",
_env.DISCORD_PUBLIC_KEY
DISCORD_PUBLIC_KEY
);

if (!isValidRequest) {
Expand All @@ -74,6 +86,6 @@ export default {
}
}

return router.handle(request, Environment);
return (await import("itty-router")).Router().handle(request);
},
};
5 changes: 5 additions & 0 deletions Source/Interface/Environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { default: Environment } = await import("../Object/Environment.js");

export type Type = Zod.infer<typeof Environment>;

export type { Type as default };
14 changes: 0 additions & 14 deletions Source/Library/Environment.ts

This file was deleted.

11 changes: 11 additions & 0 deletions Source/Object/Environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(await import("dotenv")).config();

export const { string } = await import("zod");

export default (await import("zod")).object({
DISCORD_APPLICATION_ID: string().optional().default(""),
DISCORD_CLIENT_ID: string().optional().default(""),
DISCORD_CLIENT_SECRET: string().optional().default(""),
DISCORD_PUBLIC_KEY: string().optional().default(""),
DISCORD_TOKENS: string().optional().default(""),
});

0 comments on commit 683d1bd

Please sign in to comment.