-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17cb534
commit 2666cd8
Showing
10 changed files
with
527 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
/src/test | ||
/target | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
# GDWebhook | ||
easy discord webhook library | ||
Discord Webhook library which create object and execute it. | ||
|
||
## Usage | ||
|
||
### Maven Repo | ||
#### Repository | ||
```xml | ||
|
||
``` | ||
|
||
#### Dependency | ||
```xml | ||
|
||
``` | ||
|
||
### Gradle Repo | ||
```xml | ||
|
||
``` | ||
|
||
### Code Example | ||
* Creating object and execute | ||
|
||
```java | ||
import xyz.geik.webhook.discord.Webhook; | ||
|
||
public static class Test { | ||
public static void main(String[] args) { | ||
Webhook webhook = new Webhook( | ||
"dataId", // Not Necessary | ||
"webhookUrl", // Webhook url which should be taken from discord integrations tab | ||
"userName", // Webhook username who sends | ||
"photoUrl", // Webhook profile photo who sends | ||
"thumbnail", // Thumbnail photo in embed | ||
"authorName", // gets photo from photoUrl and gets link from webUrl | ||
"description", // Embed message | ||
"content", // which is not in embed. | ||
"footer", // Footer text | ||
"color", // Possibilities (ORANGE, RED, BLACK, GREEN, YELLOW, CYAN, WHITE and BLUE ) | ||
"webUrl" // Website url | ||
); | ||
// Execute webhook | ||
webhook.execute(); | ||
} | ||
} | ||
``` | ||
|
||
## Example | ||
![](webhookexample.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package xyz.geik.webhook.discord; | ||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
new Webhook("dataId", "https://discord.com/api/webhooks/840009672756559883/q5dn6sZNBmWyDRm2_QpBYSIB_uno9yMnoUrOFG21suN_c7w7g9tanWFJbrkkp1rWzauw", | ||
"username", "https://cdn.discordapp.com/attachments/901908081569579029/901908177833058314/logo.png", | ||
"https://cdn.discordapp.com/attachments/901908081569579029/901908177833058314/logo.png", "title", | ||
"author", "description", "content", "footer", Webhook.GColor.CYAN, "https://geik.xyz").execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package xyz.geik.webhook.discord; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import xyz.geik.webhook.discord.utils.EmbedObject; | ||
import xyz.geik.webhook.discord.utils.JSONObject; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.awt.*; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Setter | ||
@Getter | ||
public class Webhook { | ||
|
||
List<EmbedObject> embeds = new ArrayList<>(); | ||
|
||
String dataId, url, userName, photoUrl, content; | ||
|
||
public Webhook(String dataId, String url, String userName, String photoUrl, | ||
String thumbnail, String title, String author, | ||
String description, String content, String footer, GColor color, String webUrl) { | ||
this.dataId = dataId; | ||
this.url = url; | ||
this.photoUrl = photoUrl; | ||
this.userName = userName; | ||
this.content = content; | ||
addEmbed(new EmbedObject() | ||
.setTitle(title) | ||
.setDescription(description) | ||
.setAuthor(author, webUrl, photoUrl) | ||
.setColor(getColor(color)) | ||
.setThumbnail(thumbnail) | ||
.setFooter(footer, "") | ||
.setUrl(webUrl)); | ||
} | ||
|
||
public void addEmbed(EmbedObject embed) { | ||
this.embeds.add(embed); | ||
} | ||
|
||
public enum GColor { | ||
ORANGE, | ||
RED, | ||
BLACK, | ||
GREEN, | ||
YELLOW, | ||
CYAN, | ||
WHITE, | ||
BLUE | ||
} | ||
|
||
private static Color getColor(GColor color) { | ||
Color color1 = Color.BLUE; | ||
switch (color) { | ||
case ORANGE: | ||
color1 = Color.ORANGE; | ||
break; | ||
case RED: | ||
color1 = Color.RED; | ||
break; | ||
case BLACK: | ||
color1 = Color.BLACK; | ||
break; | ||
case GREEN: | ||
color1 = Color.GREEN; | ||
break; | ||
case YELLOW: | ||
color1 = Color.YELLOW; | ||
break; | ||
case CYAN: | ||
color1 = Color.CYAN; | ||
break; | ||
case WHITE: | ||
color1 = Color.WHITE; | ||
break; | ||
case BLUE: | ||
color1 = Color.BLUE; | ||
break; | ||
} | ||
return color1; | ||
} | ||
|
||
public void execute() { | ||
try { | ||
if (this.content == null && this.embeds.isEmpty()) { | ||
throw new IllegalArgumentException("Set content or add at least one EmbedObject"); | ||
} | ||
|
||
JSONObject json = new JSONObject(); | ||
|
||
json.put("username", this.getUserName()); | ||
json.put("avatar_url", this.getPhotoUrl()); | ||
json.put("tts", false); | ||
|
||
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()); | ||
} | ||
|
||
json.put("content", this.content); | ||
|
||
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(StandardCharsets.UTF_8)); | ||
stream.flush(); | ||
stream.close(); | ||
connection.getInputStream().close(); | ||
connection.disconnect(); | ||
} | ||
catch (IOException e) { e.printStackTrace(); } | ||
} | ||
} |
Oops, something went wrong.