WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 900864b

Browse files
add command register script for vercel build
1 parent fea1a5a commit 900864b

File tree

7 files changed

+155
-13
lines changed

7 files changed

+155
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discraft",
3-
"version": "1.6.5-beta.4",
3+
"version": "1.6.5-beta.5",
44
"description": "Ultimate Discord bot framework",
55
"type": "module",
66
"packageManager": "[email protected]",

package/src/cli/vercel/build/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import consola from "consola";
33
import { build as esbuild } from "esbuild";
44
import { nodeExternalsPlugin } from "esbuild-node-externals";
5+
import { promises as fs } from "fs";
56
import path from "path";
67

78
import {
@@ -18,6 +19,63 @@ interface BuildOptions {
1819
builder?: Builder;
1920
}
2021

22+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23+
async function compileAndRunRegisterScript(isTS: boolean) {
24+
const registerTsPath = path.join(process.cwd(), "scripts", "register.ts");
25+
const registerJsPath = path.join(process.cwd(), "scripts", "register.js");
26+
const outPath = path.join(
27+
process.cwd(),
28+
".discraft",
29+
"commands",
30+
"register.js",
31+
);
32+
33+
let registerFile: string | null = null;
34+
try {
35+
await fs.access(registerTsPath);
36+
registerFile = registerTsPath;
37+
} catch {
38+
try {
39+
await fs.access(registerJsPath);
40+
registerFile = registerJsPath;
41+
} catch {
42+
consola.warn(
43+
"register.ts or register.js not found in scripts folder. Skipping compilation.",
44+
);
45+
return;
46+
}
47+
}
48+
49+
consola.info("Compiling and running register script...");
50+
51+
try {
52+
if (registerFile?.endsWith(".ts")) {
53+
// Compile register.ts to register.js
54+
await esbuild({
55+
entryPoints: [registerFile],
56+
outfile: outPath,
57+
platform: "node",
58+
format: "esm",
59+
bundle: true,
60+
plugins: [nodeExternalsPlugin()],
61+
});
62+
consola.verbose("register.ts compiled to .discraft/commands/register.js");
63+
} else {
64+
// Copy register.js to .discraft/commands/register.js
65+
await fs.copyFile(registerFile!, outPath);
66+
consola.verbose("register.js copied to .discraft/commands/register.js");
67+
}
68+
69+
// Run register.js
70+
await runSubprocess("node", [outPath]);
71+
consola.success("Command registration script ran successfully.");
72+
} catch (error: any) {
73+
consola.error(`Error during register script execution.`);
74+
consola.verbose(error);
75+
consola.warn("Continuing build process.");
76+
}
77+
}
78+
2179
async function startBuild(options?: BuildOptions) {
2280
consola.info("Starting Vercel build...");
2381
const currentWorkingDirectory = process.cwd();
@@ -36,6 +94,9 @@ async function startBuild(options?: BuildOptions) {
3694
await generateVercelCommandsIndex(isTS ? "ts" : "js");
3795
consola.success("Command index file generated.");
3896

97+
// Compile & Run register.ts or register.js if it exists.
98+
await compileAndRunRegisterScript(isTS);
99+
39100
let runner: Builder = options?.builder || "esbuild";
40101

41102
if (!options?.builder) {

templates/vercel-ts-ai/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
DISCORD_PUBLIC_KEY=''
66
# From `General Information > App ID` | https://discord.com/developers/applications
77
DISCORD_APP_ID=''
8+
# From `Bot > Token` | https://discord.com/developers/applications
9+
DISCORD_TOKEN=''
810

911
# From `Get API Key` | https://aistudio.google.com/app/apikey
1012
GOOGLE_AI_API_KEY=''

templates/vercel-ts-ai/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"build": "discraft vercel build"
1111
},
1212
"devDependencies": {
13-
"@types/node": "^22.10.5",
1413
"discraft": "latest",
1514
"typescript": "^5.7.3",
1615
"vercel": "^39.2.6"
@@ -21,6 +20,8 @@
2120
"consola": "^3.3.3",
2221
"discord-api-types": "^0.37.115",
2322
"discord-interactions": "^4.1.0",
23+
"discord.js": "^14.17.3",
24+
"dotenv": "^16.4.7",
2425
"raw-body": "^3.0.0"
2526
}
2627
}

templates/vercel-ts-ai/public/index.html

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,53 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Discraft Bot</title>
7+
<style>
8+
body {
9+
font: 16px sans-serif;
10+
line-height: 1.6;
11+
margin: 2rem;
12+
background: #f8f9fa;
13+
color: #343a40;
14+
display: flex;
15+
justify-content: center;
16+
align-items: center;
17+
min-height: 100vh;
18+
}
19+
.container {
20+
background: white;
21+
padding: 2rem;
22+
border-radius: 0.5rem;
23+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
24+
text-align: center;
25+
max-width: 600px;
26+
}
27+
a {
28+
color: #007bff;
29+
text-decoration: none;
30+
}
31+
a:hover {
32+
text-decoration: underline;
33+
}
34+
p {
35+
margin-bottom: 1rem;
36+
}
37+
h1 {
38+
margin-bottom: 2rem;
39+
color: #212529;
40+
font-size: 2.5rem;
41+
}
42+
</style>
743
</head>
844
<body>
9-
<p>
10-
This is a deployment for a bot created with
11-
<a href="https://github.com/The-Best-Codes/discraft-js">Discraft</a
12-
>. API route is /api.
13-
</p>
45+
<div class="container">
46+
<h1>Discraft Bot Deployment</h1>
47+
<p>
48+
This is a deployment for a bot created with
49+
<a href="https://github.com/The-Best-Codes/discraft-js"
50+
>Discraft</a
51+
>.
52+
</p>
53+
<p>The API route is: <code>/api</code></p>
54+
</div>
1455
</body>
1556
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { configDotenv } from "dotenv";
2+
configDotenv();
3+
4+
import {
5+
REST,
6+
type RESTPostAPIApplicationCommandsJSONBody,
7+
Routes,
8+
} from "discord.js";
9+
import commands from "../.discraft/commands/index.ts";
10+
11+
const token = process.env.DISCORD_TOKEN;
12+
const applicationId = process.env.DISCORD_APP_ID;
13+
14+
if (!token) {
15+
console.error("DISCORD_TOKEN is not set in your environment variables.");
16+
process.exit(1);
17+
}
18+
if (!applicationId) {
19+
console.error("DISCORD_APP_ID is not set in your environment variables.");
20+
process.exit(1);
21+
}
22+
23+
const rest = new REST({ version: "10" }).setToken(token);
24+
25+
const commandData = Object.values(commands).map((command) => command.data);
26+
27+
(async () => {
28+
try {
29+
console.log(
30+
`Started refreshing ${commandData.length} application (/) commands.`,
31+
);
32+
33+
const data = (await rest.put(Routes.applicationCommands(applicationId), {
34+
body: commandData,
35+
})) as RESTPostAPIApplicationCommandsJSONBody[];
36+
console.log(
37+
`Successfully reloaded ${data.length} application (/) commands.`,
38+
);
39+
} catch (error) {
40+
console.error(error);
41+
}
42+
})();

templates/vercel-ts-ai/vercel.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,5 @@
55
"source": "/(.*)",
66
"destination": "/api"
77
}
8-
],
9-
"functions": {
10-
"api/index.js": {
11-
"maxDuration": 60
12-
}
13-
}
8+
]
149
}

0 commit comments

Comments
 (0)