diff --git a/client.js b/client.js index 20d96ab..4cb52be 100755 --- a/client.js +++ b/client.js @@ -14,7 +14,8 @@ const emitter = require("events"); const deek = new emitter(); const name = "rena_chan"; -const password = "renachanlovesyou"; +const password = "rena"; +const mc_password = "rena"; const userid = 12; var channels = []; var ws = []; @@ -36,6 +37,28 @@ var headers = { "Origin": "https://deek.chat" }; +/* + Minecraft functions +*/ +async function send_mc(username, message){ + + if(mc_logged_in){ + + var txt = message.split("\n"); + + var txt_out = ""; + txt.forEach(function(a){ + + mc.chat('/tellraw @a ["<",{"text":"' + mc_escape(username) + '","color":"gold"},"> ' + mc_escape(a) + '"]'); + }); + } +} + +function mc_escape(str){ + + return str.replaceAll('"', '\"').replaceAll("\n", " "); +} + /* Helper functions */ @@ -132,6 +155,16 @@ async function send(channel, text, file = null, forcemime = null){ channel = channel.channel; } + + if(channel === 1){ + + send_mc(name, text); + } + + if(channel === -1){ + + channel = 1; + } if(file === null){ @@ -174,7 +207,7 @@ async function send(channel, text, file = null, forcemime = null){ method: 'POST', body: form_data, headers: headers_copy - }) + }); } /* @@ -343,6 +376,30 @@ async function handshake(name, password){ }); } +/* + Minecraft => Deekchat bridge +*/ +const mineflayer = require('mineflayer'); +var mc_logged_in = false; + +const mc = mineflayer.createBot({ + host: 'deek.chat', + username: name, + auth: 'offline', + port: 25565 +}); + +mc.on("login", function(){ + + mc_logged_in = true; + mc.chat("/login " + mc_password); + console.log("Logged to minecraft server as " + mc.username); +}); + +mc.on("death", function(user){ + + mc.respawn(); +}); /* User defined FUN-ctions!!! @@ -350,6 +407,55 @@ async function handshake(name, password){ deek.on("login", function(channel){ console.log("Connected to websocket xoxo"); + + mc.on("chat", function(username, message){ + if(username === mc.username){ + return; + } + + send(-1, "<" + username + "> " + message); + + deek.emit( + "message", + { + text: message, + id: 0, + files: [], + mentions: [], + replies: [], + channel: 1, + is_mc: true + }, + { + name: username, + id: -1, + picture: null + } + ); + }); + + mc.on("playerJoined", function(user){ + + send(-1, ">" + user.username + " joined deekcraft"); + }); + + mc.on("playerLeft", function(user){ + + send(-1, ">" + user.username + " left deekcraft"); + }); + + // Log errors and kick reasons: + mc.on("kicked", function(msg){ + + mc_logged_in = false; + console.log(msg); + }); + + mc.on("error", function(msg){ + + mc_logged_in = false; + console.log(msg); + }); }); deek.on("message", async function(msg, user){ @@ -381,6 +487,14 @@ deek.on("message", async function(msg, user){ msg.text = msg.text.split(">").splice(1).join(">").trim(); }*/ + if( + msg.channel === 1 && + typeof msg.is_mc === "undefined" + ){ + + send_mc(user.name, msg.text); + } + switch(msg.text.toLowerCase()){ case "hello rena": @@ -563,9 +677,9 @@ deek.on("message", async function(msg, user){ if( ( user.id === 13 || // me lol - user.id === 14 || // aves - user.id === 8 || // aves again - user.id === 30 || // eagle + user.id === 14 || // aves + user.id === 8 || // aves again + user.id === 30 || // eagle user.id === 2 // deek ) === false ){