DEV Community

Send automated Discord messages through Webhooks using JavaScript (Part 2 - Embeds)

Oskar Codes on April 03, 2020

In a recent post, I explained how one could send messages on Discord through a Webhook using JavaScript XMLHttpRequests. In this post, I'm gonna co...
Collapse
 
tr11 profile image
Tiago Rangel

This is cool, +1!

Collapse
 
notzastix profile image
ZastixYT

Hey im having a problem with including fields, when i try to use them and i run the code it doesnt show the field(s), it doesnt return any error, but it runs everything else fine. Im not sure if im putting it in wrong or somthing else.

here is my code: pastebin.com/5KCc1ET7

Collapse
 
oskarcodes profile image
Oskar Codes

So I took a look at your code and it seems fine, so I think the issue is in the Webhook endpoint URL. Check if you’ve got it correct, because browsing to that URL returns a 'Unknown Webhook' response.

Collapse
 
notzastix profile image
ZastixYT • Edited

Yeah i have the webhook url correct because when i run the code it sends the all of the other data, but it doesnt send the field. picture below of what the code i sent shows:

cdn.discordapp.com/attachments/945...

Collapse
 
notzastix profile image
ZastixYT

Also if you dont mind can you send me an example of code that successfully sends the field, Thanks.

Collapse
 
kobulwidodo profile image
kobulwidodo

i use fileds and its not working

Collapse
 
oskarcodes profile image
Oskar Codes

Okay and? Please develop your question if you need help.

Collapse
 
trades4x profile image
Abhay

Can the webhook be used to read the message in a channel? I am a subscriber but not the owner of the Discord Channel but want to be notified by SMS or Email when a new post appears in the channel

Thread Thread
 
oskarcodes profile image
Oskar Codes

That isn’t possible using Webhooks, as they are made to only send data in server channels, and not receive. However, you can setup a Discord Bot to read messages, but for that you’ll need to host a server.

Thread Thread
 
trades4x profile image
Abhay

if I host a server and build this discord bot, can this read messages from other channels of which I am not the admin?

Thread Thread
 
oskarcodes profile image
Oskar Codes • Edited

Yes absolutely, the only permission the bot needs is to be able to read messages from that channel, and if that permission is granted then it’ll work

Collapse
 
dreamkickz1 profile image
DREAMKICKZ

For fields, I do not want a value, what would I place there instead?

Collapse
 
oskarcodes profile image
Oskar Codes

Every property of the embed object is optional, so you can just remove the fields array if you don’t want it.

Collapse
 
dreamkickz1 profile image
DREAMKICKZ

I have tried to remove the value, but when I save and send the webhook, the message does not appear in my server. I am not too sure what you mean by fields array.

Collapse
 
mmmm2006 profile image
Mmmm2006

Waw

Collapse
 
merlode profile image
Merlode

Hello !
How can I include the forms informations on the webhook ?
Thank you for help !

Collapse
 
oskarcodes profile image
Oskar Codes

You mean taking data from an HTML form and sending that in the webhook?
Well you first have to get the raw text that is in each input of that form, and put that in the webhook payload.
For example, if you have an input on your page and want to tell the webhook to send that as the embed description, you would do it like so:

description: document.querySelector("input").value
Collapse
 
konrad_szacha_96d4d2fe379 profile image
Konrad Szacha

i dont understand it at all

Collapse
 
plug18343602 profile image
BoiDevis

it doesn't work for me at all, not showing signs of life. Only the one without color works in the first tutorial.

Collapse
 
pealco821103 profile image
Pedro Costa

Hello @BoiDevis just change color: hexToDecimal("#ff0000") with color : 16711680

Collapse
 
oskarcodes profile image
Oskar Codes • Edited

@plug18343602 yeah you can use a decimal color value like @pealco821103 said, but you can also include the hexToDecimal function if you want to use a HEX color code:

function hexToDecimal(hex) {
  return parseInt(hex.replace("#",""), 16)
}
Collapse
 
mmmm2006 profile image
Mmmm2006

I created a page of my site where I tried to integrate all the functions -> mmmm.world-heberg.com/webhook/ (in french)

Collapse
 
merlode profile image
Merlode

Your link is expired.

Collapse
 
sakusuperball profile image
SakuSuperball

it doesn't work for me the embed

Collapse
 
oskarcodes profile image
Oskar Codes

Does the browser console return any error ?

Collapse
 
sakusuperball profile image
SakuSuperball

i gave up on it, so i'm using discohook instead, it makes things easier for me

Thread Thread
 
oskarcodes profile image
Oskar Codes

Oh yeah that's totally an option if you just want to send one specific Webhook message, but this post is about learning how to code that, which is useful if you want to integrate that to your own application, so your Webhook can send messages when events occur in it.

Thread Thread
 
pealco821103 profile image
Pedro Costa • Edited

Hello @oskarcodes I found the issue.

We can't use color: hexToDecimal("#ff0000"), instead we have to use decimal code so just change color: hexToDecimal("#ff0000") with color : 16711680

Probably you have other script to execute that hexToDecimal command.

Hope it helps

Thread Thread
 
oskarcodes profile image
Oskar Codes

Oh well yeah the hexTodecimal function is just this:

function hexToDecimal(hex) {
  return parseInt(hex.replace("#",""), 16)
}
Collapse
 
kisaragiarya profile image
kisaragiarya

hello how can i add reaction to my embed message?

Collapse
 
oskarcodes profile image
Oskar Codes

That isn’t possible using Webhooks. Use discord bots instead. However for that you’ll need to host a server.

Collapse
 
thebrain04 profile image
TheBrain04

How can i use in the Embet my variables ?

Collapse
 
19216800 profile image
19216800

Hey how would one add a button that can send a user to a URL? Thanks in advance.

Collapse
 
piesekchlebek profile image
Piesek Chlebek • Edited

Will you make a tutorial on how to write messages in HTML not changing code