-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added hook with Discord for sending game start messages (#82)
- Loading branch information
1 parent
a4d5103
commit 993502a
Showing
8 changed files
with
82 additions
and
3 deletions.
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
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
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
51 changes: 51 additions & 0 deletions
51
src/main/java/me/roinujnosde/titansbattle/hooks/discord/DiscordWebhook.java
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,51 @@ | ||
package me.roinujnosde.titansbattle.hooks.discord; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URL; | ||
|
||
/** | ||
* Class used to execute Discord Webhooks | ||
*/ | ||
public class DiscordWebhook { | ||
|
||
private final String url; | ||
private String content; | ||
|
||
/** | ||
* 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 execute() throws IOException { | ||
JsonObject json = new JsonObject(); | ||
json.addProperty("content", content); | ||
|
||
URL url = new URL(this.url); | ||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); | ||
connection.addRequestProperty("Content-Type", "application/json"); | ||
connection.addRequestProperty("User-Agent", "TitansBattle"); | ||
connection.setDoOutput(true); | ||
connection.setRequestMethod("POST"); | ||
|
||
OutputStream stream = connection.getOutputStream(); | ||
stream.write(json.toString().getBytes()); | ||
stream.flush(); | ||
stream.close(); | ||
|
||
connection.getInputStream().close(); | ||
connection.disconnect(); | ||
} | ||
|
||
} |
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
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