chomens-bot-js/plugins/proxy.js
2022-11-16 18:09:37 +07:00

95 lines
2.5 KiB
JavaScript

/* eslint-disable require-jsdoc */
const mc = require('minecraft-protocol');
function inject(bot, dcclient, config) {
config.servers.forEach((server, index) => {
if (server.host !== bot.options.host) return;
const {MessageBuilder} = require('prismarine-chat')(bot.version);
const version = bot.version;
const srv = mc.createServer({
'online-mode': false,
'port': 25566 + index,
'keepAlive': false,
version,
});
srv.on('login', function(client) {
bot.console.info(`[Proxy] ${client.username} connected to proxy`);
let endedClient = false;
let endedTarget = false;
const targetClient = mc.createClient({
username: client.username,
host: bot.options.host,
version,
});
targetClient.on('packet', (data, meta) => {
if (!endedClient &&
meta.state === mc.states.PLAY &&
client.state === mc.states.PLAY
) client.write(meta.name, data);
});
targetClient.on('error', () => {});
targetClient.on('end', () => {
targetClient.end();
endedTarget = true;
});
client.on('end', function() {
endedClient = true;
targetClient.end();
bot.console.info(`[Proxy] ${client.username} ended`);
});
client.on('error', function() {
endedClient = true;
bot.console.info(`[Proxy] ${client.username} got error`);
});
client.on('packet', (data, meta) => {
if (meta.name === 'chat' && !data.message?.startsWith('/')) {
if (data.message.startsWith('.')) {
return bot.command_handler.run(
client.username,
client.username,
'*' + data.message.substring(1),
client.uuid,
null,
'h',
'o',
);
}
bot.tellraw('@a', {
color: 'dark_gray',
translate: '[%s] [%s] %s \u203a %s',
with: [
{
text: 'Chat',
color: 'gray',
},
{
text: 'Proxy',
color: 'gray',
},
{
text: client.username,
color: 'green',
},
MessageBuilder.fromString('&7' + data.message),
],
});
return;
}
targetClient.write(meta.name, data);
});
bot.on('end', () => srv.close());
});
});
};
module.exports = {inject};