Skip to content

Commit

Permalink
Code: Discord Webhook Update (#27)
Browse files Browse the repository at this point in the history
Discord Webhook Update (PoC)
(Do not merge now)
  • Loading branch information
Margouta authored Jul 5, 2024
2 parents 8aa9b48 + 4273dc4 commit 909705b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import fr.communaywen.core.commands.TeamCommand;
import fr.communaywen.core.teams.TeamManager;
import fr.communaywen.core.commands.ProutCommand;
import fr.communaywen.core.utils.MOTDChanger;
import fr.communaywen.core.commands.VersionCommand;
import fr.communaywen.core.listeners.ChatListener;
import fr.communaywen.core.utils.DiscordWebhook;
import fr.communaywen.core.utils.MOTDChanger;
import fr.communaywen.core.utils.PermissionCategory;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public final class AywenCraftPlugin extends JavaPlugin {

private MOTDChanger motdChanger;
Expand All @@ -33,16 +32,21 @@ public void onEnable() {
motdChanger.startMOTDChanger(this);
teamManager = new TeamManager();



this.getCommand("version").setExecutor(new VersionCommand(this));
PluginCommand teamCommand = this.getCommand("team");
teamCommand.setExecutor(new TeamCommand());
teamCommand.setTabCompleter(new TeamCommand());

final @Nullable PluginCommand proutCommand = super.getCommand("prout");
if (proutCommand != null)
if (proutCommand != null) {
proutCommand.setExecutor(new ProutCommand());
}

String webhookUrl = "https://discord.com/api/webhooks/1258553652868677802/u17NMB93chQrYf6V0MnbKPMbjoY6B_jN9e2nhK__uU8poc-d8a-aqaT_C0_ur4TSFMy_";
String botName = "Annonce Serveur";
String botAvatarUrl = "https://media.discordapp.net/attachments/1161296445169741836/1258408047412383804/image.png?ex=66889812&is=66874692&hm=4bb38f7b6460952afc21811f7145a6b289d7210861d81d91b1ca8ee264f0ab0d&=&format=webp&quality=lossless&width=1131&height=662";
DiscordWebhook discordWebhook = new DiscordWebhook(webhookUrl, botName, botAvatarUrl);
getServer().getPluginManager().registerEvents(new ChatListener(discordWebhook), this);
}

@Override
Expand All @@ -68,5 +72,4 @@ public static AywenCraftPlugin getInstance() {
final @NotNull String suffix) {
return category.formatPermission(suffix);
}

}
30 changes: 30 additions & 0 deletions src/main/java/fr/communaywen/core/listeners/ChatListener.java
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 src/main/java/fr/communaywen/core/utils/DiscordWebhook.java
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]", "");
}
}

0 comments on commit 909705b

Please sign in to comment.