-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discord Webhook Update (PoC) (Do not merge now)
- Loading branch information
Showing
3 changed files
with
135 additions
and
8 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
30 changes: 30 additions & 0 deletions
30
src/main/java/fr/communaywen/core/listeners/ChatListener.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,30 @@ | ||
package fr.communaywen.core.listeners; | ||
|
||
import fr.communaywen.core.utils.DiscordWebhook; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.AsyncPlayerChatEvent; | ||
import org.bukkit.event.server.BroadcastMessageEvent; | ||
|
||
public class ChatListener implements Listener { | ||
|
||
private final DiscordWebhook discordWebhook; | ||
|
||
public ChatListener(DiscordWebhook discordWebhook) { | ||
this.discordWebhook = discordWebhook; | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerChat(AsyncPlayerChatEvent event) { | ||
String username = event.getPlayer().getName(); | ||
String avatarUrl = "https://minotar.net/helm/" + username; | ||
String message = event.getMessage(); | ||
|
||
discordWebhook.sendMessage(username, avatarUrl, message); | ||
} | ||
|
||
@EventHandler | ||
public void onBroadcastMessage(BroadcastMessageEvent event) { | ||
discordWebhook.sendBroadcast(event.getMessage()); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/fr/communaywen/core/utils/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,94 @@ | ||
package fr.communaywen.core.utils; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
|
||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class DiscordWebhook { | ||
|
||
private final String webhookUrl; | ||
private final String botName; | ||
private final String botAvatarUrl; | ||
|
||
public DiscordWebhook(String webhookUrl, String botName, String botAvatarUrl) { | ||
this.webhookUrl = webhookUrl; | ||
this.botName = botName; | ||
this.botAvatarUrl = botAvatarUrl; | ||
} | ||
|
||
public void sendMessage(String username, String avatarUrl, String message) { | ||
try { | ||
URL url = new URL(webhookUrl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
connection.setDoOutput(true); | ||
|
||
JsonObject jsonPayload = new JsonObject(); | ||
jsonPayload.addProperty("username", username); | ||
jsonPayload.addProperty("avatar_url", avatarUrl); | ||
jsonPayload.addProperty("content", "``" + removeColorCodes(message) + "``"); | ||
|
||
try (OutputStream os = connection.getOutputStream()) { | ||
os.write(jsonPayload.toString().getBytes()); | ||
os.flush(); | ||
} | ||
|
||
int responseCode = connection.getResponseCode(); | ||
if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) { | ||
System.err.println("Failed to send message to Discord webhook. Response code: " + responseCode); | ||
} | ||
|
||
connection.disconnect(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void sendBroadcast(String message) { | ||
try { | ||
URL url = new URL(webhookUrl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
connection.setDoOutput(true); | ||
|
||
JsonObject jsonPayload = new JsonObject(); | ||
jsonPayload.addProperty("username", botName); | ||
jsonPayload.addProperty("avatar_url", botAvatarUrl); | ||
|
||
JsonObject embed = new JsonObject(); | ||
String cleanedMessage = removeColorCodes(message); | ||
embed.addProperty("description", cleanedMessage); | ||
|
||
if (cleanedMessage.startsWith("[a] PROUT !!!")) { | ||
embed.addProperty("color", 65280); | ||
} | ||
|
||
JsonArray embeds = new JsonArray(); | ||
embeds.add(embed); | ||
jsonPayload.add("embeds", embeds); | ||
|
||
try (OutputStream os = connection.getOutputStream()) { | ||
os.write(jsonPayload.toString().getBytes()); | ||
os.flush(); | ||
} | ||
|
||
int responseCode = connection.getResponseCode(); | ||
if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) { | ||
System.err.println("Failed to send broadcast message to Discord webhook. Response code: " + responseCode); | ||
} | ||
|
||
connection.disconnect(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private String removeColorCodes(String message) { | ||
return message.replaceAll("§[0-9a-fk-or]", ""); | ||
} | ||
} |