#cant send a discord webhook with node js
64 messages · Page 1 of 1 (latest)
Use readFile from fs/promises
const jsonData = JSON.parse(readFile(jsonPath));
``` like this?
Need to add await
const jsonData = JSON.parse(await readFile(jsonPath));

readFile is not defined
You can just do this:
const { readFile } = require('fs/promises')
You can only have one module per require
What are you logging
if (!response.ok) {
throw new Error(`Failed to send webhook: ${response.json}`);
}
That's a function
Error: Failed to send webhook: async json() {
if (!(this instanceof instance)) {
throw new TypeError("Illegal invocation");
}
throwIfAborted(this[kState]);
return JSON.parse(await this.text());
}
at sendEmbed (C:\Users\Kristóf Mátyás\Desktop\hypixel.lol\src\server.js:73:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
You need to await json() if you expect a json body
const body = JSON.stringify({
embeds: embeds
});
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: body
};
oooh
how do i do it?
await response.json()
That's actually a 400 error so you should be able to catch it instead
if (!response.ok) {
throw new Error(`Failed to send webhook: ${await response.json}`);
}
try {
const response = await fetch(webhookURL, options);
if (!response.ok) {
throw new Error(`Failed to send webhook: ${await response.json()}`);
}
} catch(error) {
console.log(error);
}
You should try catch
Error: Failed to send webhook: [object Object]
at sendEmbed (C:\Users\Kristóf Mátyás\Desktop\hypixel.lol\src\server.js:74:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
async function sendEmbed(webhookURL, jsonPath) {
const jsonData = JSON.parse(await readFile(jsonPath));
const embeds = jsonData.embeds;
const body = JSON.stringify({
embeds: embeds
});
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: body
};
try {
const response = await fetch(webhookURL, options);
if (!response.ok) {
throw new Error(`Failed to send webhook: ${await response.json()}`);
}
} catch(error) {
console.log(error);
}
}
{
"embeds": [
{
"title": "asd",
"description": "asd",
"color": "#ffffff",
"thumbnail": {
"url": "https://raw.githubusercontent.com/matyii/hypixel.lol/main/hypixel.png"
}
}
]
}
can you help me fixing it?
im still having issues
if (!response.ok) {
//throw new Error(`Failed to send webhook: ${await response.json(body)}`);
console.log(await response.json(body))
}
{ embeds: [ '0' ] }
wha-?
no need to pass body
hm?
You need to look at discord docs
I've only used Discord.js for interacting with Discord, it's a bit more streamlined
Ah, yes