I'm trying to access a discord Webhook using Nodejs for simple messages (for now).
I have looked at several attempts here and at other places, but didn't quite understand them or was able to replicate them myself. Reading through the docs and searching online I found node-fetch which in my eyes should work fine in principle, while seemingly simpler.
const fetch = require('node-fetch');
var webhook = {
"id":"my webhook id",
"token":"my webhook token"
};
var URL = `https://discordapp.com/api/webhooks/${webhook.id}/${webhook.token}`;
fetch(URL, {
"method":"POST",
"payload": JSON.stringify({
"content":"test"
})
})
.then(res=> console.log(res));
The only output I ever get is a Response Object with status code 400. The only time I do get something else is when I remove the method, then I get code 200 which doesn't help much... Is my payload somehow completely wrong or did I make a mistake with the URL or fetch syntax?
Instead of making your own POST request, you can use the WebhookClient
built into Discord.js like so...
const id = '';
const token = '';
const webhook = new Discord.WebhookClient(id, token);
webhook.send('Hello world.')
.catch(console.error);