Skip to content

Commit

Permalink
Add webhook reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Ale32bit committed Jan 28, 2025
1 parent e3fe9d9 commit bd9067c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.16.9

# Mod Properties
mod_version=1.8.5
mod_version=1.8.6
maven_group=cc.reconnected
archives_base_name=rcc-chatbox

Expand All @@ -18,7 +18,7 @@ fabric_version=0.92.2+1.20.1

rcclibrary_version=1.0.0
rccdiscord_version=1.8.1
solstice_version=1.6.0+1.20.1
solstice_version=1.7.0+1.20.1

permissions_api_version=0.2-SNAPSHOT

Expand All @@ -27,7 +27,7 @@ permissions_api_version=0.2-SNAPSHOT
adventure_version=5.9.1
adventure_legacy_serializer_version=4.14.0

placeholderapi_version=2.1.3+1.20.1
placeholderapi_version=2.1.4+1.20.1

websocket_version=1.5.7
jda_version=5.0.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cc.reconnected.chatbox.packets.serverPackets.events.ChatboxChatEvent;
import cc.reconnected.chatbox.utils.DateUtils;
import cc.reconnected.chatbox.utils.TextComponents;
import cc.reconnected.chatbox.utils.Webhook;
import cc.reconnected.chatbox.ws.ClientErrors;
import cc.reconnected.library.data.PlayerMeta;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -56,6 +57,7 @@ private static void tickQueue(MinecraftServer server) {
}

if (msg.type == MessageTypes.SAY) {
Webhook.send(uuid, msg, null);
mcServer.getPlayerManager().getPlayerList().forEach(player -> player.sendMessage(msg.message));
msg.conn.send(RccChatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));

Expand Down Expand Up @@ -84,6 +86,7 @@ private static void tickQueue(MinecraftServer server) {
msg.conn.send(RccChatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, msg.id)));
continue;
}
Webhook.send(uuid, msg, player);
player.sendMessage(msg.message);

msg.conn.send(RccChatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cc/reconnected/chatbox/RccChatbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cc.reconnected.chatbox.packets.serverPackets.PingPacket;
import cc.reconnected.chatbox.state.StateSaverAndLoader;
import cc.reconnected.chatbox.license.LicenseManager;
import cc.reconnected.chatbox.utils.Webhook;
import cc.reconnected.chatbox.ws.WsServer;
import cc.reconnected.library.config.ConfigManager;
import com.google.gson.Gson;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cc/reconnected/chatbox/RccChatboxConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cc.reconnected.chatbox;

import cc.reconnected.library.config.Config;
import org.jetbrains.annotations.Nullable;

@Config(RccChatbox.MOD_ID)
public class RccChatboxConfig {
public String hostname = "127.0.0.1";
public short port = 25580;
public String guestAllowedAddress = "127.0.0.1";
@Nullable
public String webhook = null;
}
109 changes: 109 additions & 0 deletions src/main/java/cc/reconnected/chatbox/utils/Webhook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cc.reconnected.chatbox.utils;

import cc.reconnected.chatbox.ClientPacketsHandler;
import cc.reconnected.chatbox.RccChatbox;
import cc.reconnected.chatbox.license.License;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;

public class Webhook {
private final static HttpClient http = HttpClient.newHttpClient();

private static void send(JsonObject body) {
var url = RccChatbox.CONFIG.webhook;
if (url == null) return;

try {
var request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(body.toString()))
.uri(URI.create(url))
.header("Content-Type", "application/json")
.build();

var response = http.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) {
RccChatbox.LOGGER.error("Received {} as status code!\n{}", response.statusCode(), response.body());
}
} catch (Exception e) {
RccChatbox.LOGGER.error("Could not send webhook message!", e);
}
}

public static void send(UUID licenseId, ClientPacketsHandler.ClientMessage message, @Nullable ServerPlayerEntity recipient) {
var license = RccChatbox.licenseManager().getLicense(licenseId);
String label = null;
if (message.label() != null) {
label = PlainTextComponentSerializer.plainText().serialize(message.label());
}
var text = PlainTextComponentSerializer.plainText().serialize(message.message());

String recipientName = null;
if (recipient != null) {
recipientName = recipient.getGameProfile().getName();
}

var json = build(license, label, text, message.type(), recipientName);
send(json);
}

public static JsonObject build(License license, String label, String content, ClientPacketsHandler.MessageTypes type, @Nullable String toUser) {
var json = new JsonObject();

json.addProperty("flags", 4096); // no mention

var embed = new JsonObject();
embed.addProperty("title", label != null ? label : license.user.name);
embed.addProperty("description", content);

var color = switch (type) {
case TELL -> NamedTextColor.BLUE.value();
case SAY -> NamedTextColor.GOLD.value();
};
embed.addProperty("color", color);

var fields = new JsonArray();
var typeField = new JsonObject();
typeField.addProperty("name", "Type");
typeField.addProperty("value", type.toString());
typeField.addProperty("inline", true);

var licenseField = new JsonObject();
licenseField.addProperty("name", "License");
licenseField.addProperty("value", license.uuid().toString());
licenseField.addProperty("inline", true);

fields.add(typeField);
fields.add(licenseField);

if (toUser != null) {
var userField = new JsonObject();
userField.addProperty("name", "Recipient");
userField.addProperty("value", toUser);
userField.addProperty("inline", true);

fields.add(userField);
}

var author = new JsonObject();
author.addProperty("name", String.format("%s (%s)", license.user.name, license.user.uuid));

embed.add("fields", fields);
embed.add("author", author);

var embeds = new JsonArray();
embeds.add(embed);
json.add("embeds", embeds);

return json;
}
}

0 comments on commit bd9067c

Please sign in to comment.