for anyone who might find it interesting, here's the script I use to afk on the cohost minecraft server: https://gist.github.com/nycki93/a64f79935e81efd28c621e070d95bbf7
import { readFileSync } from 'node:fs';
import { createInterface, moveCursor } from 'node:readline';
import { createBot } from 'mineflayer';
const config = JSON.parse(readFileSync('config.json'));
const { host, port, username, password } = config;
const PROMPT = '> ';
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
function write(text) {
moveCursor(process.stdout, -1 * PROMPT.length, 0);
console.log(text);
rl.prompt();
}
const bot = createBot({ host, port, username, auth: 'offline' });
console.log('bot created.');
bot.on('spawn', () => {
bot.chat(`/login ${password}`);
rl.setPrompt(PROMPT);
rl.prompt();
});
bot.on('resourcePack', () => {
bot.denyResourcePack();
});
bot.on('message', (message) => {
if (!message.unsigned && !message.toString().trim()) return;
let messageStr = message.toAnsi().trim();
if (message.unsigned) {
const whole = message.unsigned.getText();
const text = message.getText();
const user = whole.slice(0, whole.length - text.length);
messageStr = `<${user}> ${messageStr}`;
}
write(messageStr);
});
rl.on('line', (line) => {
if (line === '/list') {
const players = Object.keys(bot.players);
write(players.join(', '));
} else {
bot.chat(line.toString());
}
});
bot.on('playerJoined', (player) => {
write(`${player.displayName} joined.`);
});
bot.on('playerLeft', (player) => {
write(`${player.displayName} left.`);
});
bot.on('error', console.log)
bot.on('kicked', console.log)
syntax highlighting by codehost