Skip to content

Instantly share code, notes, and snippets.

@k3kdude
Created August 17, 2017 15:31
Show Gist options
  • Save k3kdude/fba6f6b37594eae3d6f9475330733bdb to your computer and use it in GitHub Desktop.
Save k3kdude/fba6f6b37594eae3d6f9475330733bdb to your computer and use it in GitHub Desktop.
Java DiscordWebhook class to easily execute Discord Webhooks
import javax.net.ssl.HttpsURLConnection;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Class used to execute Discord Webhooks with low effort
*/
public class DiscordWebhook {
private final String url;
private String content;
private String username;
private String avatarUrl;
private boolean tts;
private List<EmbedObject> embeds = new ArrayList<>();
/**
* Constructs a new DiscordWebhook instance
*
* @param url The webhook URL obtained in Discord
*/
public DiscordWebhook(String url) {
this.url = url;
}
public void setContent(String content) {
this.content = content;
}
public void setUsername(String username) {
this.username = username;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public void setTts(boolean tts) {
this.tts = tts;
}
public void addEmbed(EmbedObject embed) {
this.embeds.add(embed);
}
public void execute() throws IOException {
if (this.content == null && this.embeds.isEmpty()) {
throw new IllegalArgumentException("Set content or add at least one EmbedObject");
}
JSONObject json = new JSONObject();
json.put("content", this.content);
json.put("username", this.username);
json.put("avatar_url", this.avatarUrl);
json.put("tts", this.tts);
if (!this.embeds.isEmpty()) {
List<JSONObject> embedObjects = new ArrayList<>();
for (EmbedObject embed : this.embeds) {
JSONObject jsonEmbed = new JSONObject();
jsonEmbed.put("title", embed.getTitle());
jsonEmbed.put("description", embed.getDescription());
jsonEmbed.put("url", embed.getUrl());
if (embed.getColor() != null) {
Color color = embed.getColor();
int rgb = color.getRed();
rgb = (rgb << 8) + color.getGreen();
rgb = (rgb << 8) + color.getBlue();
jsonEmbed.put("color", rgb);
}
EmbedObject.Footer footer = embed.getFooter();
EmbedObject.Image image = embed.getImage();
EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
EmbedObject.Author author = embed.getAuthor();
List<EmbedObject.Field> fields = embed.getFields();
if (footer != null) {
JSONObject jsonFooter = new JSONObject();
jsonFooter.put("text", footer.getText());
jsonFooter.put("icon_url", footer.getIconUrl());
jsonEmbed.put("footer", jsonFooter);
}
if (image != null) {
JSONObject jsonImage = new JSONObject();
jsonImage.put("url", image.getUrl());
jsonEmbed.put("image", jsonImage);
}
if (thumbnail != null) {
JSONObject jsonThumbnail = new JSONObject();
jsonThumbnail.put("url", thumbnail.getUrl());
jsonEmbed.put("thumbnail", jsonThumbnail);
}
if (author != null) {
JSONObject jsonAuthor = new JSONObject();
jsonAuthor.put("name", author.getName());
jsonAuthor.put("url", author.getUrl());
jsonAuthor.put("icon_url", author.getIconUrl());
jsonEmbed.put("author", jsonAuthor);
}
List<JSONObject> jsonFields = new ArrayList<>();
for (EmbedObject.Field field : fields) {
JSONObject jsonField = new JSONObject();
jsonField.put("name", field.getName());
jsonField.put("value", field.getValue());
jsonField.put("inline", field.isInline());
jsonFields.add(jsonField);
}
jsonEmbed.put("fields", jsonFields.toArray());
embedObjects.add(jsonEmbed);
}
json.put("embeds", embedObjects.toArray());
}
URL url = new URL(this.url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStream stream = connection.getOutputStream();
stream.write(json.toString().getBytes());
stream.flush();
stream.close();
connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream
connection.disconnect();
}
public static class EmbedObject {
private String title;
private String description;
private String url;
private Color color;
private Footer footer;
private Thumbnail thumbnail;
private Image image;
private Author author;
private List<Field> fields = new ArrayList<>();
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
public Color getColor() {
return color;
}
public Footer getFooter() {
return footer;
}
public Thumbnail getThumbnail() {
return thumbnail;
}
public Image getImage() {
return image;
}
public Author getAuthor() {
return author;
}
public List<Field> getFields() {
return fields;
}
public EmbedObject setTitle(String title) {
this.title = title;
return this;
}
public EmbedObject setDescription(String description) {
this.description = description;
return this;
}
public EmbedObject setUrl(String url) {
this.url = url;
return this;
}
public EmbedObject setColor(Color color) {
this.color = color;
return this;
}
public EmbedObject setFooter(String text, String icon) {
this.footer = new Footer(text, icon);
return this;
}
public EmbedObject setThumbnail(String url) {
this.thumbnail = new Thumbnail(url);
return this;
}
public EmbedObject setImage(String url) {
this.image = new Image(url);
return this;
}
public EmbedObject setAuthor(String name, String url, String icon) {
this.author = new Author(name, url, icon);
return this;
}
public EmbedObject addField(String name, String value, boolean inline) {
this.fields.add(new Field(name, value, inline));
return this;
}
private class Footer {
private String text;
private String iconUrl;
private Footer(String text, String iconUrl) {
this.text = text;
this.iconUrl = iconUrl;
}
private String getText() {
return text;
}
private String getIconUrl() {
return iconUrl;
}
}
private class Thumbnail {
private String url;
private Thumbnail(String url) {
this.url = url;
}
private String getUrl() {
return url;
}
}
private class Image {
private String url;
private Image(String url) {
this.url = url;
}
private String getUrl() {
return url;
}
}
private class Author {
private String name;
private String url;
private String iconUrl;
private Author(String name, String url, String iconUrl) {
this.name = name;
this.url = url;
this.iconUrl = iconUrl;
}
private String getName() {
return name;
}
private String getUrl() {
return url;
}
private String getIconUrl() {
return iconUrl;
}
}
private class Field {
private String name;
private String value;
private boolean inline;
private Field(String name, String value, boolean inline) {
this.name = name;
this.value = value;
this.inline = inline;
}
private String getName() {
return name;
}
private String getValue() {
return value;
}
private boolean isInline() {
return inline;
}
}
}
private class JSONObject {
private final HashMap<String, Object> map = new HashMap<>();
void put(String key, Object value) {
if (value != null) {
map.put(key, value);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
builder.append("{");
int i = 0;
for (Map.Entry<String, Object> entry : entrySet) {
Object val = entry.getValue();
builder.append(quote(entry.getKey())).append(":");
if (val instanceof String) {
builder.append(quote(String.valueOf(val)));
} else if (val instanceof Integer) {
builder.append(Integer.valueOf(String.valueOf(val)));
} else if (val instanceof Boolean) {
builder.append(val);
} else if (val instanceof JSONObject) {
builder.append(val.toString());
} else if (val.getClass().isArray()) {
builder.append("[");
int len = Array.getLength(val);
for (int j = 0; j < len; j++) {
builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : "");
}
builder.append("]");
}
builder.append(++i == entrySet.size() ? "}" : ",");
}
return builder.toString();
}
private String quote(String string) {
return "\"" + string + "\"";
}
}
}
@Wirlie
Copy link

Wirlie commented Feb 26, 2020

I get a 400 HTTP error when using this after 1 time.

In my case, I get a 400 HTTP error because I have used UTF-8 characters, to solve that I have changed this line:
https://gist.github.com/k3kdude/fba6f6b37594eae3d6f9475330733bdb#file-discordwebhook-java-L148

with:

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

@AbdulRIsmail
Copy link

How can I pass an image from the same directory?

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://discordapp.com/api/webhooks/hookurl
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
	at DiscordWebhook.execute(DiscordWebhook.java:152)
	at WebHook.main(WebHook.java:30)

The line that's causing it

.setImage("data/file.jpg"));

@Jaimss
Copy link

Jaimss commented Aug 6, 2020

Anyone using kotlin: https://gist.github.com/Jaimss/42fed5695f5bfb6a3282abe8bfc7c229#file-discordwebhook-kt
I forked this and sorta made it in kotlin. It's working for me, but if you got issues comment on it and ill fix them

@ZombieCloud
Copy link

It's just works !!!

@pmp94
Copy link

pmp94 commented Nov 16, 2020

Hey, can someone help me out please. Where should I run Example usage? Thanks

@scswskid
Copy link

is there a way to send a file

@LouieMartin
Copy link

This was helpful allot, thank you!

@InstFurry
Copy link

It's very nice class, thank you! But I have a problem with sending cyrilic characters - it gives BAD REQUEST error qwo

@faderook
Copy link

nice

@Feniksovich
Copy link

Works perfect, thank you!

For other users: be sure you creating the DiscordWebhook object (message) inside the method and you won't get errors like
Cannot resolve method 'addField' in '*Classname*'.

@Dogloverblue
Copy link

Dogloverblue commented May 21, 2021

I can't get this to work? I wanted to use the example they provided, but I can't find where to put it, whatever I do, I just get the error Syntax error on token ".", @ expected after this token Does anyone know a fix for this?
Edit: Solved, I was being dumb and forgot to add main

@Dimflix
Copy link

Dimflix commented Jun 6, 2021

God bless you

@Whanos
Copy link

Whanos commented Jul 24, 2021

Hey there. What's the license on this code?

@Feniksovich
Copy link

@Whanos

Hey there. What's the license on this code?

So, gists don't have licenses and if they aren't private, they can be used by everyone.
But in any case, it is most respectful to point to the source or the author :)

@getQueryString
Copy link

I love it! It works. But can this script also be called an "API"?

@Whanos
Copy link

Whanos commented Aug 6, 2021 via email

@MikMuellerDev
Copy link

Thank you for the awesome Class, I am pretty new to Java and this insipred me to keep learning!

@itIsMaku
Copy link

Hi, did lombok implementation and little code cleanup here. :) https://gist.github.com/itIsMaku/5e4edef950e0b83374f79c9675444866

@Khuirul-Huda
Copy link

HTTP Error 400 ... ????

@RexAliis
Copy link

same

@DxveDE
Copy link

DxveDE commented Feb 25, 2022

Has anyone a fix for HTTP Error 400?
Error-Code:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://discord.com/api/webhooks/XXX

@FelixSchick
Copy link

No I have the error too
Server returned HTTP response code: 400 for URL

@juniorkrz
Copy link

Has anyone a fix for HTTP Error 400? Error-Code: java.io.IOException: Server returned HTTP response code: 400 for URL: https://discord.com/api/webhooks/XXX

This error is caused by special characters, accents for example, to correct it just change line 148 this way:

before:
stream.write(json.toString().getBytes());

later:
stream.write(json.toString().getBytes("UTF-8"));

;)

@mattybdidit
Copy link

Continuing from above comment
It's now line 150, and change it to

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

@zyonNZ
Copy link

zyonNZ commented Jun 11, 2022

How to make a new line in setDescription?

I've tried using \n but without success.

@Khuirul-Huda
Copy link

Khuirul-Huda commented Jun 11, 2022

new line

System.getProperty("line.separator")

@zyonNZ
Copy link

zyonNZ commented Jun 11, 2022

new line

System.getProperty("line.separator")

Thanks!

@Khuirul-Huda
Copy link

What's the license on this code? According to this topic "Gists are considered like repositories with regards to licensing." And from this If you find software that doesn’t have a license, that generally means you have no permission from the creators of the software to use, modify, or share the software. Although a code host such as GitHub may allow you to view and fork the code, this does not imply that you are permitted to use, modify, or share the software for any purpose.

@Eejit43
Copy link

Eejit43 commented Jul 3, 2022

What's the license on this code? According to this topic "Gists are considered like repositories with regards to licensing." And from this If you find software that doesn’t have a license, that generally means you have no permission from the creators of the software to use, modify, or share the software. Although a code host such as GitHub may allow you to view and fork the code, this does not imply that you are permitted to use, modify, or share the software for any purpose.

Despite choosealicense.com saying otherwise, I'd assume that (as the original author hasn't said anything in regards to licensing) the author intended this to be able to be used and modified by all. I'd still credit it of course. It is up to you however.

@Pocanistaken
Copy link

Continuing from above comment It's now line 150, and change it to

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

Done but still got 404 Error

@akaiomoi
Copy link

akaiomoi commented Sep 6, 2022

Continuing from above comment It's now line 150, and change it to

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

did this, still getting the same issue.
but now on connection.getInputStream().close();?

@VikkarDev
Copy link

How can i make a new line in setContent?

@Jaimss
Copy link

Jaimss commented Jan 18, 2023

new line

System.getProperty("line.separator")

Probably use this

@kyle2542
Copy link

Continuing from above comment It's now line 150, and change it to

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

did this, still getting the same issue. but now on connection.getInputStream().close();?

I have the same problem, did you find a fix?

@benyamin3219
Copy link

Continuing from above comment It's now line 150, and change it to

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

did this, still getting the same issue. but now on connection.getInputStream().close();?

I have the same problem, did you find a fix?

send us your code so we can help u

@kennethivantm
Copy link

i dont know anything about java or making my own code😭, does anyone know how I'm able to change the color of the messages in discord or if possible anyway to use discords embedded messages code

@RodriDevs
Copy link

Thanks! very usefull

@im-a-blobfish
Copy link

im-a-blobfish commented Apr 20, 2023

new line

System.getProperty("line.separator")

for me, that just caused an error 400 - dont do that.

instead, i did "\\n" after a lot of trial and error which is what discord needs, as once java formats it, it is now "\n" in the request which is what discord uses for a newline, not an actual newline character

@Clueeng
Copy link

Clueeng commented May 7, 2023

Anyone knows how to add files to a message?

@horumons
Copy link

horumons commented Jun 2, 2023

new line

System.getProperty("line.separator")

for me, that just caused an error 400 - dont do that.

instead, i did "\n" after a lot of trial and error which is what discord needs, as once java formats it, it is now "\n" in the request which is what discord uses for a newline, not an actual newline character

me too

@horumons
Copy link

horumons commented Jun 2, 2023

in toString()
return builder.toString(); → return builder.toString().replace("\n","\n");

@kenny59
Copy link

kenny59 commented Jun 23, 2023

If using libraries is an option, you can replace JSONObject with org.codehaus.jettison:jettison's implementation of JSONObject.
That seems to work more reliably than the one provided.

@RageOfFire
Copy link

Is anyone still using this ? I still got Server returned HTTP response code: 400 for URL:
Already edit code base on comment

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

@kenny59
Copy link

kenny59 commented Aug 14, 2023

Is anyone still using this ? I still got Server returned HTTP response code: 400 for URL: Already edit code base on comment

stream.write(json.toString().getBytes(StandardCharsets.UTF_8));

Yes, I am using it and it works great.
You can try to use \\n instead of \n.
Or remove JSONObject class from DiscordWebhook class and use org.codehaus.jettison.json.JSONObject instead.
You can use either one (but not both).

@RageOfFire
Copy link

I don't know where to find \n but when I replace JSONObject to org.codehaus.jettison.json.JSONObject I still got Server returned HTTP response code: 400 for URL:

@kenny59
Copy link

kenny59 commented Aug 15, 2023

I don't know where to find \n but when I replace JSONObject to org.codehaus.jettison.json.JSONObject I still got Server returned HTTP response code: 400 for URL:

Please post a minimum reproducible example then I may be able to help.

@RageOfFire
Copy link

I don't know where to find \n but when I replace JSONObject to org.codehaus.jettison.json.JSONObject I still got Server returned HTTP response code: 400 for URL:

Please post a minimum reproducible example then I may be able to help.

Here is the source code using Discord Webhook the java class is in utils folder

@kenny59
Copy link

kenny59 commented Aug 15, 2023

I don't know where to find \n but when I replace JSONObject to org.codehaus.jettison.json.JSONObject I still got Server returned HTTP response code: 400 for URL:

Please post a minimum reproducible example then I may be able to help.

Here is the source code using Discord Webhook the java class is in utils folder

It seems your issue is trying to get plugin.getConfig().getString("discord-webhook.thumbnail") instead of plugin.getConfig().getString("discord-webhook.thumbnail.icon").
That will result in invalid value for the thumbnail url.

It is always a good idea to put a breakpoint at DiscordWebhook line 150, so you can check the json it is sending and compare it with the docs.

@RageOfFire
Copy link

Oh I get wrong string on that 😅 thank for tell me about it

@ToniIsmaili
Copy link

Any way to use BufferedImage instead of an URL in setImage?

@berk313134
Copy link

Can you send me a working sample embed code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment