Oct 6, 2022

Posting to a Discord Channel using NodeJS

Here's a quick snippet that allows you to send messages to a Discord channel using a webhook.

To get a webhook for your channel, visit Server Settings, then Integrations.

Integrations
Integrations

With the URL this returns, fill out the following snippet:

const DISCORD_HOOK = "https://...";

export async function postMessage(message: string) {
	if (DISCORD_HOOK) {
		await fetch(DISCORD_HOOK, {
			method: "POST",
			headers: {
				Accept: "application/json",
				"Content-Type": "application/json",
			},
			body: JSON.stringify({
				content: message,
			}),
		});
	}
}

You're done. By calling postMessage(), you can now send messages to the Discord channel.