consensus/scripts/main.js
2025-05-02 12:40:32 +12:00

51 lines
1 KiB
JavaScript

import {
world,
system
} from "@minecraft/server";
import {
http,
HttpRequest,
HttpHeader,
HttpRequestMethod
} from "@minecraft/server-net";
// CHANGE THIS TO YOUR WEBHOOK URL!
const webhookUrl = ":webhook:";
async function makeMessage(author, message) {
const req = new HttpRequest(webhookUrl);
req.body = JSON.stringify({
username: author,
content: message
});
req.method = HttpRequestMethod.Post;
req.headers = [
new HttpHeader("Accept", "application/json"),
new HttpHeader("Content-Type", "application/json"),
];
await http.request(req);
}
world.beforeEvents.chatSend.subscribe((e) => {
e.cancel = true;
system.run(() => {
world.sendMessage(`${e.sender.name}: ${e.message}`);
makeMessage(e.sender.name, e.message);
});
});
world.afterEvents.playerJoin.subscribe((e) => {
e.cancel = true;
system.run(() => {
makeMessage("Server", `> ${e.playerName} joins`);
});
});
world.afterEvents.playerLeave.subscribe((e) => {
e.cancel = true;
system.run(() => {
makeMessage("Server", `> ${e.playerName} leaves`);
});
});