Welcome to part 2 of my series of using PowerShell to send Discord webhooks. In this post I will be going over how to send embeds. If you’re just getting started with the process, I recommend reading Part 1 first.
Table of Contents
I’ve created a module that makes it easy to work with embeds, store configurations that contain your webhook urls, and make the whole experience seamless by using PowerShell classes. It will get its own post, and if you’re interested check out the Github repo here.
Example of using the module:
Embeds!
If we take a look at the Discord developer documentation for webhook properties, this is what it looks like:
You can see there’s a field for embeds that accepts and array of embed objects. Embeds in Discord are a way to make your message look a little spiffier.
Here is the documentation for embeds:
Here is an example of what one looks like from the teaser at the end of part one:
There are a lot of different features of embeds, and we’ll be covering a few of them. You can use this link to see how many different fields there are in an embed available to you.
In this post I will be focusing on sending an embed that contains a color (on the left side), a thumbnail image, title, and a description (essentially the content of the embed).
Creating and Sending the Embed
To get started making embeds, be sure to keep that webhook url handy from part 1.
First let’s store that webhook url in a variable and create an empty array to add the embed(s) to.
$webHookUrl = "yourhookurlhere" [System.Collections.ArrayList]$embedArray = @()
2. Now let’s store values for the title, description, and color.
Title and description are strings so those are easy. Color is also a string, but a string that represents a decimal value for the color. For now it is important to understand the value for green is 4289797.
$color = '4289797' $title = 'Greetings from PowerShell!' $description = 'This is an embed. It looks much nicer than just sending text over!'
3. Now it’s time to create a PSCustomObject that contains those items to add to the array we created earlier.
$embedObject = [PSCustomObject]@{ color = $color title = $title description = $description }
4. Let’s add that to our array.
$embedArray.Add($embedObject)
5. Taking a peek at the array, we can see the contents of our embed in it.
6. Now let’s test it by constructing the payload and sending it over to the webhook url via Invoke-RestMethod.
$payload = [PSCustomObject]@{ embeds = $embedArray } Invoke-RestMethod -Uri $webHookUrl -Body ($payload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'application/json'
7. Looking in the channel it should send to, it looks like the send was successful!
Adding a Thumbnail
Now that we’ve constructed and sent an embed successfully, let’s send one with a thumbnail in it. To do this we’ll start from scratch, and apply much of what we did above to a new embed object.
First we’ll need to construct a thumbnail object. From the Discord developer documentation, we can see a thumbnail contains the following:
We’ll be using the url property to store a url to an image.
1. Create the thumbnail object.
$thumbnailObject = [PSCustomObject]@{ url = "https://static1.squarespace.com/static/5644323de4b07810c0b6db7b/t/5aa44874e4966bde3633b69c/1520715914043/webhook_resized.png" }
2. Create our embed object with the thumbnail in it.
$title = 'Greetings with a picture!' $description = 'This embed should now contain an image' $color = '9442302' $embedObject = [PSCustomObject]@{ title = $title description = $description color = $color thumbnail = $thumbnailObject }
3. Now we’ll create an array, add the embed object, create the payload, and send that over to the webhook url.
[System.Collections.ArrayList]$embedArray = @() $embedArray.Add($embedObject) $payload = [PSCustomObject]@{ embeds = $embedArray } Invoke-RestMethod -Uri $webHookUrl -Body ($payload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'application/json'
Looking in the channel, we can see that it worked!
Github Repo With Example Code
I’m going to start putting examples together in Github so I can better keep them updated. Use this url for updated examples: https://github.com/gngrninja/blog/blob/master/DiscordWebhook/embeds.ps1
Summary
Embeds are a little more complicated, but not too hard to work in with these webhooks and PowerShell. There is even more you can do, including adding fields to embeds and sending files. In part 3 I will be go over just how to do that!
Let me know if you have any ideas, questions, or feedback in the comments below!