Create Discord commands registration endpoint
cgen-2af3f97b6dbd4f1181df5e902c8eb03c
This commit is contained in:
parent
363337bc3e
commit
7659589b7e
1 changed files with 111 additions and 0 deletions
111
api/discord/register-commands.ts
Normal file
111
api/discord/register-commands.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
|
||||
const DISCORD_API_BASE = "https://discord.com/api/v10";
|
||||
|
||||
const commands = [
|
||||
{
|
||||
name: "creators",
|
||||
description: "Browse AeThex creators across all arms",
|
||||
type: 1,
|
||||
options: [
|
||||
{
|
||||
name: "arm",
|
||||
description: "Filter creators by arm",
|
||||
type: 3,
|
||||
required: false,
|
||||
choices: [
|
||||
{ name: "Labs", value: "labs" },
|
||||
{ name: "GameForge", value: "gameforge" },
|
||||
{ name: "Corp", value: "corp" },
|
||||
{ name: "Foundation", value: "foundation" },
|
||||
{ name: "Nexus", value: "nexus" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "opportunities",
|
||||
description: "Find job opportunities and collaborations on Nexus",
|
||||
type: 1,
|
||||
options: [
|
||||
{
|
||||
name: "arm",
|
||||
description: "Filter opportunities by arm",
|
||||
type: 3,
|
||||
required: false,
|
||||
choices: [
|
||||
{ name: "Labs", value: "labs" },
|
||||
{ name: "GameForge", value: "gameforge" },
|
||||
{ name: "Corp", value: "corp" },
|
||||
{ name: "Foundation", value: "foundation" },
|
||||
{ name: "Nexus", value: "nexus" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "nexus",
|
||||
description: "Explore the AeThex Talent Marketplace",
|
||||
type: 1,
|
||||
},
|
||||
];
|
||||
|
||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||
// Verify auth token to prevent unauthorized registration
|
||||
const authToken = req.query.token as string;
|
||||
if (authToken !== process.env.DISCORD_REGISTER_TOKEN) {
|
||||
return res.status(401).json({ error: "Unauthorized" });
|
||||
}
|
||||
|
||||
const botToken = process.env.DISCORD_BOT_TOKEN;
|
||||
const clientId = process.env.DISCORD_CLIENT_ID;
|
||||
|
||||
if (!botToken || !clientId) {
|
||||
return res.status(400).json({
|
||||
error: "Missing DISCORD_BOT_TOKEN or DISCORD_CLIENT_ID env variables",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("[Discord] Registering slash commands...");
|
||||
|
||||
const response = await fetch(
|
||||
`${DISCORD_API_BASE}/applications/${clientId}/commands`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
Authorization: `Bot ${botToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(commands),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error("[Discord] Registration error:", errorData);
|
||||
return res.status(response.status).json({
|
||||
error: "Failed to register commands",
|
||||
details: errorData,
|
||||
});
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log("[Discord] Successfully registered commands:", data);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: "Successfully registered 3 slash commands",
|
||||
commands: data.map((cmd: any) => ({
|
||||
name: cmd.name,
|
||||
description: cmd.description,
|
||||
})),
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("[Discord] Registration endpoint error:", error);
|
||||
return res.status(500).json({
|
||||
error: "Server error during command registration",
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue