-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
another major rewrite of everything because i hate myself
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
Showing
114 changed files
with
19,037 additions
and
8,364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dist | ||
node_modules | ||
/.vscode/settings.json | ||
*.log | ||
*.env | ||
.dev.vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { PingSlashCommand } from './slash/ping'; | ||
|
||
export { | ||
// slash commands | ||
PingSlashCommand | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.