#cant send a discord webhook with node js

64 messages · Page 1 of 1 (latest)

ashen quest
#
{
    "embeds": [
      {
        "title": "asd",
        "description": "asd",
        "color": "#ffffff",
        "thumbnail": {
          "url": "https://raw.githubusercontent.com/matyii/hypixel.lol/main/hypixel.png"
        }
      }
    ]
}

this is my json payload, and i still cant get it to work

full oriole
#

Also, don't use sync fs in async

ashen quest
full oriole
#

Use readFile from fs/promises

ashen quest
#
const jsonData = JSON.parse(readFile(jsonPath));
``` like this?
full oriole
#

Need to add await

ashen quest
#
const jsonData = JSON.parse(await readFile(jsonPath));
full oriole
ashen quest
#

readFile is not defined

full oriole
#

You need to get it from fs/promises

#

Require or import

ashen quest
#

im kinda new to this, so sorry for my dumbness

full oriole
#

You can just do this:

#

const { readFile } = require('fs/promises')

#

You can only have one module per require

ashen quest
#

still

full oriole
#

What are you logging

ashen quest
#

json

#

response

full oriole
#

Show me

#

Btw please use code blocks instead of screenshot

ashen quest
#
if (!response.ok) {
        throw new Error(`Failed to send webhook: ${response.json}`);
    }
full oriole
#

That's a function

ashen quest
#
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)
full oriole
#

You need to await json() if you expect a json body

ashen quest
#
const body = JSON.stringify({
        embeds: embeds
    });
    const options = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: body
    };
full oriole
#

You're just logging the function itself rn

#

Not calling it

ashen quest
#

oooh

ashen quest
full oriole
#

await response.json()

#

That's actually a 400 error so you should be able to catch it instead

ashen quest
#
if (!response.ok) {
        throw new Error(`Failed to send webhook: ${await response.json}`);
    }
full oriole
#

Call the function

#

()

#

But still

ashen quest
#
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);
    }
full oriole
#

You should try catch

ashen quest
#
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

full oriole
#

Instead of throwing you should just console.log

#

Just console log the json body

ashen quest
#
if (!response.ok) {
            //throw new Error(`Failed to send webhook: ${await response.json(body)}`);
            console.log(await response.json(body))
        }
#

{ embeds: [ '0' ] }

#

wha-?

full oriole
#

no need to pass body

ashen quest
#

same

#

console.log(await response.json())

full oriole
#

so there's a response but it's failing?

#

hilarious.

ashen quest
#

hm?

full oriole
#

You need to look at discord docs

#

I've only used Discord.js for interacting with Discord, it's a bit more streamlined

ashen quest
#

maybe the filereading is the problem?

#

but how should i refer to the problem

ashen quest
#

got the solution

#

the color has to be an integer and decimal

full oriole