Skip to content

Commit

Permalink
another major rewrite of everything because i hate myself
Browse files Browse the repository at this point in the history
not even close to finished but im comitting it to avoid
this commit growing even larger because 110 changed files is
already insane
  • Loading branch information
Dougley committed Oct 22, 2024
1 parent c1f52dc commit 07902d2
Show file tree
Hide file tree
Showing 114 changed files with 19,037 additions and 8,364 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/base:jammy as base
FROM mcr.microsoft.com/devcontainers/base:ubuntu as base

# install sqlite3 and libc++1
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"ghcr.io/devcontainers/features/git:1": {},
"ghcr.io/devcontainers/features/common-utils:2": {},
"ghcr.io/devcontainers/features/node:1": {
"version": "18.3"
"version": "lts"
},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
Expand Down
50 changes: 0 additions & 50 deletions .github/workflows/stripe-build.yml

This file was deleted.

9 changes: 0 additions & 9 deletions .github/workflows/workers-prd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ jobs:
with:
environment: production

stripe:
name: Deploy Stripe Webhooks
needs: deploy
uses: dougley/frugal/.github/workflows/stripe-build.yml@dev
if: github.event.inputs.skipdeploy != 'true'
secrets: inherit
with:
environment: production

sentry:
name: Create Sentry release
needs: deploy
Expand Down
9 changes: 0 additions & 9 deletions .github/workflows/workers-stg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ jobs:
with:
environment: staging

stripe:
name: Deploy Stripe Webhooks
needs: deploy
uses: dougley/frugal/.github/workflows/stripe-build.yml@dev
if: github.event.inputs.skipdeploy != 'true'
secrets: inherit
with:
environment: staging

sentry:
name: Create Sentry release
needs: deploy
Expand Down
3 changes: 2 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
auto-install-peers=true
auto-install-peers=true
link-workspace-packages=true
3 changes: 1 addition & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"IronGeek.vscode-env",
"bungcip.better-toml",
"tamasfe.even-better-toml",
"esbenp.prettier-vscode",
"ms-azuretools.vscode-docker",
"github.vscode-github-actions",
"stripe.vscode-stripe",
"bradlc.vscode-tailwindcss",
"mtxr.sqltools-driver-sqlite",
"mtxr.sqltools"
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions apps/discord-new/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist
node_modules
/.vscode/settings.json
*.log
*.env
.dev.vars
9 changes: 9 additions & 0 deletions apps/discord-new/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["prettier-plugin-organize-imports"],
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
"trailingComma": "none",
"printWidth": 120
}
39 changes: 39 additions & 0 deletions apps/discord-new/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@dougley/frugal-discord-new",
"type": "module",
"version": "0.0.0",
"private": true,
"main": "dist/index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "wrangler dev --persist-to='../../.mf' src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts",
"lint": "eslint --ext .ts .",
"lint:fix": "eslint --ext .ts . --fix",
"format": "prettier -w ./src",
"db:migrate:dev": "wrangler d1 migrations apply frugal --local --persist-to='../../.mf'",
"db:migrate": "wrangler d1 migrations apply frugal"
},
"repository": {
"type": "git",
"url": "https://github.com/Dougley/frugal.git",
"directory": "apps/discord"
},
"dependencies": {
"@discord-interactions/api": "^0.3.21",
"@discord-interactions/builders": "^0.3.18",
"@discord-interactions/core": "^0.3.23",
"@dougley/frugal-savestate": "workspace:^",
"@sentry/cloudflare": "^8.34.0",
"hono": "^4.6.4",
"kysely": "catalog:",
"kysely-d1": "catalog:"
},
"devDependencies": {
"@cloudflare/workers-types": "catalog:",
"@dougley/tsconfig": "workspace:^",
"@dougley/types": "workspace:^",
"better-sqlite3": "^11.3.0",
"wrangler": "catalog:"
}
}
6 changes: 6 additions & 0 deletions apps/discord-new/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PingSlashCommand } from './slash/ping';

export {
// slash commands
PingSlashCommand
};
16 changes: 16 additions & 0 deletions apps/discord-new/src/commands/slash/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MessageBuilder, SlashCommandBuilder } from '@discord-interactions/builders';
import { ISlashCommand, SlashCommandContext } from '@discord-interactions/core';

export class PingSlashCommand implements ISlashCommand {
public builder = new SlashCommandBuilder('ping', "Test the bot's latency");

public handler = async (ctx: SlashCommandContext): Promise<void> => {
await ctx.defer();
const msg = await ctx.send(new MessageBuilder().setContent('🏓 Pinging...'));
const sigOffset = Date.now() - ctx.signedAt.getTime();
const rtt = new Date(msg.timestamp).getTime() - ctx.receivedAt.getTime();
await ctx.edit(
new MessageBuilder().setContent(`🏓 Pong! Signature offset is \`${sigOffset}\`ms, RTT is \`${rtt}\`ms`)
);
};
}
122 changes: 122 additions & 0 deletions apps/discord-new/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
CommandManager,
DiscordApplication,
InteractionHandlerError,
InteractionHandlerNotFound,
InteractionHandlerTimedOut,
UnauthorizedInteraction,
UnknownApplicationCommandType,
UnknownComponentType,
UnknownInteractionType
} from '@discord-interactions/core';

import * as Sentry from '@sentry/cloudflare';
import * as Commands from './commands';

export default Sentry.withSentry(
(env) => ({
dsn: env.SENTRY_DSN,
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
tracesSampleRate: 1.0
}),
{
async fetch(req: Request, env, ctx) {
if (req.method !== 'POST') return new Response('Method not allowed', { status: 405 });

Sentry.instrumentD1WithSentry(env.D1);

const app = new DiscordApplication({
clientId: env.DISCORD_APP_ID,
token: env.DISCORD_BOT_TOKEN,
publicKey: env.DISCORD_PUBLIC_KEY,

//syncMode: SyncMode.Disabled,

cache: {
get: async (key: string) => env.KV.get(key, 'text'),
set: async (key: string, ttl: number, value: string) => {
await env.KV.put(key, value, { expirationTtl: ttl });
}
}
});

const commandsToRegister = Object.values(Commands).map((Command) => new Command());

if (env.DEVELOPMENT_GUILD) {
console.log('Registering commands in development guild');
const guild = new CommandManager(app, env.DEVELOPMENT_GUILD);
await guild.register(...commandsToRegister);
} else {
await app.commands.register(...commandsToRegister);
}

const signature = req.headers.get('x-signature-ed25519');
const timestamp = req.headers.get('x-signature-timestamp');

const body = await req.text();

if (typeof body !== 'string' || typeof signature !== 'string' || typeof timestamp !== 'string') {
return new Response('Invalid request', { status: 400 });
}

try {
const [getResponse, handling] = await app.handleInteraction(body, signature, timestamp);

ctx.waitUntil(handling);
const response = await getResponse;

if (response instanceof FormData) {
return new Response(response);
}

return new Response(JSON.stringify(response), {
headers: {
'content-type': 'application/json;charset=UTF-8'
}
});
} catch (err) {
if (err instanceof UnauthorizedInteraction) {
console.error('Unauthorized Interaction');
return new Response('Invalid request', { status: 401 });
}

if (err instanceof InteractionHandlerNotFound) {
console.error('Interaction Handler Not Found');
console.dir(err.interaction);

new Response('Invalid request', { status: 404 });
}

if (err instanceof InteractionHandlerTimedOut) {
console.error('Interaction Handler Timed Out');

new Response('Timed Out', { status: 408 });
}

if (
err instanceof UnknownInteractionType ||
err instanceof UnknownApplicationCommandType ||
err instanceof UnknownComponentType
) {
console.error('Unknown Interaction - Library may be out of date.');
console.dir(err.interaction);

new Response('Server Error', { status: 500 });
}

if (err instanceof InteractionHandlerError) {
console.error('Interaction Handler Error');
console.error(err.cause);

new Response('Server Error', { status: 500 });
}

console.error(err);
}

return new Response('Unknown Error', { status: 500 });
}
} satisfies ExportedHandler<Env>
);

// durable objects
File renamed without changes.
30 changes: 26 additions & 4 deletions apps/stripe/wrangler.toml → apps/discord-new/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
name = "frugal-stripe-dev"
# --- Defaults --- #

name = "frugal-core-dev"
main = "src/index.ts"
compatibility_date = "2023-04-09"
compatibility_date = "2024-04-05"
workers_dev = true
# no_bundle = true

compatibility_flags = ["nodejs_als"]

[[d1_databases]]
binding = "D1"
Expand All @@ -10,20 +15,37 @@ database_id = "ddc7d747-ada2-4e1c-9946-62856277f9a1"
preview_database_id = "ddc7d747-ada2-4e1c-9946-62856277f9a1"
migrations_dir = "../../packages/d1-database/migrations"

[[kv_namespaces]]
binding = "KV"
id = "dad8c886278d4341aed8926d38747ea4"
preview_id = "dad8c886278d4341aed8926d38747ea4"

# --- Production --- #

[env.production]
name = "frugal-stripe-prod"
name = "frugal-core-prod"

[[env.production.d1_databases]]
binding = "D1"
database_name = "frugal"
database_id = "a09d43b8-850e-4f4d-ba91-fe47d3be0ec9"
migrations_dir = "../../packages/d1-database/migrations"

[[env.production.kv_namespaces]]
binding = "KV"
id = "dad8c886278d4341aed8926d38747ea4"

# --- Staging --- #

[env.staging]
name = "frugal-stripe-stg"
name = "frugal-core-stg"

[[env.staging.d1_databases]]
binding = "D1"
database_name = "frugal"
database_id = "b3f0981c-e213-4b28-be7f-1ba9ad731024"
migrations_dir = "../../packages/d1-database/migrations"

[[env.staging.kv_namespaces]]
binding = "KV"
id = "dad8c886278d4341aed8926d38747ea4"
Loading

0 comments on commit 07902d2

Please sign in to comment.