From 3d49e6f1a6aea05ae08a060e4a0f3be67c4acd69 Mon Sep 17 00:00:00 2001 From: lolcat Date: Sun, 3 Nov 2024 16:22:12 -0500 Subject: [PATCH] re-added minecraft bridge --- client.js | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 8 deletions(-) mode change 100755 => 100644 client.js diff --git a/client.js b/client.js old mode 100755 new mode 100644 index 72d43cc..db143b1 --- a/client.js +++ b/client.js @@ -8,6 +8,16 @@ const ddg = require("duckduckgo-images-api"); var mmm = require("mmmagic"); var Magic = mmm.Magic; +const mineflayer = require("mineflayer"); +const mc_bridge_enabled = true; + +const mc_options = { + "host": "deek.chat", + "port": 25565, + "username": "rena_chan" + // offline mode +}; + var http = require("http"); const emitter = require("events"); @@ -49,7 +59,6 @@ var fortunes = [ "Your fortune: You may rely on it.", "Your fortune: Concentrate and ask again.", "Your fortune: The future is uncertain.", - "Your fortune: :fuck-off: fuck you", "Your fortune: it's over" ]; @@ -61,7 +70,7 @@ var headers = { /* HTTP server bullshit */ -process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; +//process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; const http_bullshit_handler = function(req, res){ @@ -306,21 +315,30 @@ async function handshake(name, password){ } ); + var pong_time = Date.now(); + ws.once("open", function(){ deek.emit("login"); - - ws._socket.setTimeout(10000); - ws._socket.on("timeout", function(){ - - deek.emit("error", "timed out") - }); const keep_alive = setInterval(function(){ + //console.log("ping!"); + if(pong_time + 10000 < Date.now()){ + + deek.emit("error", "Did not receive ping frame"); + return; + } ws.ping(); + }, 5000); }); + + ws.on("pong", function(){ + + pong_time = Date.now(); + //console.log(pong_time + ": pong!"); + }); ws.once("close", function(){ @@ -422,6 +440,60 @@ async function handshake(name, password){ }); } +/* + Mineflayer bot code +*/ +var mc_logged_in = false; +var mc_bot = null; + +function mc_login(){ + + if(mc_bridge_enabled === false){ return; } + + mc_bot = mineflayer.createBot(mc_options); + + mc_bot.once("login", function(){ + + console.log("Logged into minecraft server as " + mc_options.username); + mc_logged_in = true; + }); + + mc_bot.on("chat", function(username, message){ + + if(username !== mc_bot.username){ + + send(1, "<" + username + "> " + message); + } + }); + + mc_bot.on("end", function(){ + + mc_logged_in = false; + console.log("Minecraft: Kicked from server"); + setTimeout(mc_login, 5000); // reconnect after 5 seconds + }); + + mc_bot.on("error", function(){ + + console.log("Minecraft: error occured, disconnecting"); + mc_bot.end(); + mc_logged_in = false; + setTimeout(mc_login, 5000); // reconnect after 5 seconds + }); +} + +mc_login(); + +function mc_escape(str){ + + return str.replace( + /[^-!\"#$%&'()*+,.\/0123456789:;<=>?@[\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«» ]/gi, + "" + ).replace(/[\\"']/g, '\\$&') + .replace(/\u0000/g, '\\0') + .replace("\n", " "); +} + /* User defined FUN-ctions!!! */ @@ -483,6 +555,39 @@ deek.on("message", async function(msg, user){ msg.text = msg.text.split(">").splice(1).join(">").trim(); }*/ + if( + mc_bridge_enabled && + mc_logged_in + ){ + + // ascii filter + var mc_message = msg.text.replace( + /[^\n-!\"#$%&'()*+,.\/0123456789:;<=>?@[\\\]^_'abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«» ]/gi, + "?" + ); + + var mc_username = mc_escape(user.name); + + mc_message.split("\n").forEach(function(a){ + + // split message into newlines + // make sure message gets sent in chunks + + var chunks = []; + + for(var i=0; i ' + mc_escape(a.slice(i, i + 100)).trim() + '"]'); + }else{ + + mc_bot.chat('/tellraw @a "' + mc_escape(a.slice(i, i + 100)) + '"'); + } + } + }); + } + switch(msg.text.toLowerCase()){ case "hello rena":