Skip to content

Commit

Permalink
add command register script for vercel build
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Best-Codes committed Jan 13, 2025
1 parent fea1a5a commit 900864b
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discraft",
"version": "1.6.5-beta.4",
"version": "1.6.5-beta.5",
"description": "Ultimate Discord bot framework",
"type": "module",
"packageManager": "[email protected]",
Expand Down
61 changes: 61 additions & 0 deletions package/src/cli/vercel/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import consola from "consola";
import { build as esbuild } from "esbuild";
import { nodeExternalsPlugin } from "esbuild-node-externals";
import { promises as fs } from "fs";
import path from "path";

import {
Expand All @@ -18,6 +19,63 @@ interface BuildOptions {
builder?: Builder;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function compileAndRunRegisterScript(isTS: boolean) {
const registerTsPath = path.join(process.cwd(), "scripts", "register.ts");
const registerJsPath = path.join(process.cwd(), "scripts", "register.js");
const outPath = path.join(
process.cwd(),
".discraft",
"commands",
"register.js",
);

let registerFile: string | null = null;
try {
await fs.access(registerTsPath);
registerFile = registerTsPath;
} catch {
try {
await fs.access(registerJsPath);
registerFile = registerJsPath;
} catch {
consola.warn(
"register.ts or register.js not found in scripts folder. Skipping compilation.",
);
return;
}
}

consola.info("Compiling and running register script...");

try {
if (registerFile?.endsWith(".ts")) {
// Compile register.ts to register.js
await esbuild({
entryPoints: [registerFile],
outfile: outPath,
platform: "node",
format: "esm",
bundle: true,
plugins: [nodeExternalsPlugin()],
});
consola.verbose("register.ts compiled to .discraft/commands/register.js");
} else {
// Copy register.js to .discraft/commands/register.js
await fs.copyFile(registerFile!, outPath);
consola.verbose("register.js copied to .discraft/commands/register.js");
}

// Run register.js
await runSubprocess("node", [outPath]);
consola.success("Command registration script ran successfully.");
} catch (error: any) {
consola.error(`Error during register script execution.`);
consola.verbose(error);
consola.warn("Continuing build process.");
}
}

async function startBuild(options?: BuildOptions) {
consola.info("Starting Vercel build...");
const currentWorkingDirectory = process.cwd();
Expand All @@ -36,6 +94,9 @@ async function startBuild(options?: BuildOptions) {
await generateVercelCommandsIndex(isTS ? "ts" : "js");
consola.success("Command index file generated.");

// Compile & Run register.ts or register.js if it exists.
await compileAndRunRegisterScript(isTS);

let runner: Builder = options?.builder || "esbuild";

if (!options?.builder) {
Expand Down
2 changes: 2 additions & 0 deletions templates/vercel-ts-ai/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
DISCORD_PUBLIC_KEY=''
# From `General Information > App ID` | https://discord.com/developers/applications
DISCORD_APP_ID=''
# From `Bot > Token` | https://discord.com/developers/applications
DISCORD_TOKEN=''

# From `Get API Key` | https://aistudio.google.com/app/apikey
GOOGLE_AI_API_KEY=''
Expand Down
3 changes: 2 additions & 1 deletion templates/vercel-ts-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"build": "discraft vercel build"
},
"devDependencies": {
"@types/node": "^22.10.5",
"discraft": "latest",
"typescript": "^5.7.3",
"vercel": "^39.2.6"
Expand All @@ -21,6 +20,8 @@
"consola": "^3.3.3",
"discord-api-types": "^0.37.115",
"discord-interactions": "^4.1.0",
"discord.js": "^14.17.3",
"dotenv": "^16.4.7",
"raw-body": "^3.0.0"
}
}
51 changes: 46 additions & 5 deletions templates/vercel-ts-ai/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,53 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Discraft Bot</title>
<style>
body {
font: 16px sans-serif;
line-height: 1.6;
margin: 2rem;
background: #f8f9fa;
color: #343a40;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background: white;
padding: 2rem;
border-radius: 0.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 600px;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
p {
margin-bottom: 1rem;
}
h1 {
margin-bottom: 2rem;
color: #212529;
font-size: 2.5rem;
}
</style>
</head>
<body>
<p>
This is a deployment for a bot created with
<a href="https://github.com/The-Best-Codes/discraft-js">Discraft</a
>. API route is /api.
</p>
<div class="container">
<h1>Discraft Bot Deployment</h1>
<p>
This is a deployment for a bot created with
<a href="https://github.com/The-Best-Codes/discraft-js"
>Discraft</a
>.
</p>
<p>The API route is: <code>/api</code></p>
</div>
</body>
</html>
42 changes: 42 additions & 0 deletions templates/vercel-ts-ai/scripts/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { configDotenv } from "dotenv";
configDotenv();

import {
REST,
type RESTPostAPIApplicationCommandsJSONBody,
Routes,
} from "discord.js";
import commands from "../.discraft/commands/index.ts";

const token = process.env.DISCORD_TOKEN;
const applicationId = process.env.DISCORD_APP_ID;

if (!token) {
console.error("DISCORD_TOKEN is not set in your environment variables.");
process.exit(1);
}
if (!applicationId) {
console.error("DISCORD_APP_ID is not set in your environment variables.");
process.exit(1);
}

const rest = new REST({ version: "10" }).setToken(token);

const commandData = Object.values(commands).map((command) => command.data);

(async () => {
try {
console.log(
`Started refreshing ${commandData.length} application (/) commands.`,
);

const data = (await rest.put(Routes.applicationCommands(applicationId), {
body: commandData,
})) as RESTPostAPIApplicationCommandsJSONBody[];
console.log(
`Successfully reloaded ${data.length} application (/) commands.`,
);
} catch (error) {
console.error(error);
}
})();
7 changes: 1 addition & 6 deletions templates/vercel-ts-ai/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
"source": "/(.*)",
"destination": "/api"
}
],
"functions": {
"api/index.js": {
"maxDuration": 60
}
}
]
}

0 comments on commit 900864b

Please sign in to comment.