all repos — stealth-developers @ f8ee1bb02ee0c98c52a0ac3c5fbd70b6b7e27eaa

apps/api/scripts/bootstrap.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
import config from "@stealth-developers/config";

import { GroundWarWidget } from "../src/lib/widgets/meow";

const APP_ID = config.discord.app_id;
const BOT_TOKEN = config.discord.token;

async function bootstrapWidget() {
	const widgetConfigPayload = GroundWarWidget;

	const createRes = await fetch(
		`https://discord.com/api/v10/applications/${APP_ID}/widget-configs`,
		{
			method: "POST",
			headers: {
				Authorization: `Bot ${BOT_TOKEN}`,
				"Content-Type": "application/json",
			},
			body: JSON.stringify(widgetConfigPayload),
		},
	);

	if (!createRes.ok) {
		const errorText = await createRes.text();
		throw new Error(`failedd to push config: ${createRes.status} - ${errorText}`);
	}

	const createdConfig = (await createRes.json()) as { config_id: string; [key: string]: any };
	const configId = createdConfig.config_id;
	console.log(`created config (id: ${configId}).`);

	console.log("publishing widget config...");
	const publishRes = await fetch(
		`https://discord.com/api/v10/applications/${APP_ID}/widget-configs/${configId}/publish`,
		{
			method: "POST",
			headers: { Authorization: `Bot ${BOT_TOKEN}` },
		},
	);

	if (!publishRes.ok) {
		const errorText = await publishRes.text();
		throw new Error(`failed to publish: ${publishRes.status} - ${errorText}`);
	}

	const publishedConfig = await publishRes.json();
	console.log(publishedConfig);
}

bootstrapWidget().catch(console.error);