import config from "@stealth-developers/config"; import fs from "fs"; import path from "path"; const args = process.argv.slice(2); const filepath = args[0]; const assetKey = args[1]; const APP_ID = config.discord.app_id; const BOT_TOKEN = config.discord.token; const FILE_PATH = filepath; const ASSET_KEY = assetKey; const MIME_TYPES = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", } as const; async function uploadApplicationAsset() { if (!FILE_PATH || !ASSET_KEY) { console.error("usage: command "); process.exit(1); } const stats = fs.statSync(FILE_PATH); const fileSize = stats.size; const fileName = path.basename(FILE_PATH); const fileExt = path.extname(FILE_PATH); const fileMimeType = MIME_TYPES[fileExt as keyof typeof MIME_TYPES]; if (!fileMimeType) throw new Error("Unsupported file type"); const initResponse = await fetch( `https://discord.com/api/v10/applications/${APP_ID}/assets/upload`, { method: "POST", headers: { Authorization: `Bot ${BOT_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ filename: fileName, file_size: fileSize, }), }, ); if (!initResponse.ok) { const errorText = await initResponse.text(); throw new Error(`Step 1 Failed: ${initResponse.status} - ${errorText}`); } const uploadMetadata = (await initResponse.json()) as { upload_url: string; upload_filename: string; }; const { upload_url, upload_filename } = uploadMetadata; const fileStream = fs.createReadStream(FILE_PATH); const uploadResponse = await fetch(upload_url, { method: "PUT", headers: { "Content-Length": fileSize.toString(), "Content-Type": fileMimeType, }, body: fileStream, }); if (!uploadResponse.ok) { const errorText = await uploadResponse.text(); throw new Error(`Step 2 Failed: ${uploadResponse.status} - ${errorText}`); } console.log("uploaded file to GCS."); const registerResponse = await fetch( `https://discord.com/api/v10/applications/${APP_ID}/assets`, { method: "POST", headers: { Authorization: `Bot ${BOT_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ key: ASSET_KEY, upload_filename: upload_filename, visibility: "public", }), }, ); if (!registerResponse.ok) { const errorText = await registerResponse.text(); throw new Error( `failed to register application asset: ${registerResponse.status} - ${errorText}`, ); } const finalisedAsset = await registerResponse.json(); console.log("registered application asset:", finalisedAsset); } uploadApplicationAsset().catch(console.error);