itzrealviktor-bot/index.js
2024-10-27 05:09:53 -04:00

206 lines
No EOL
7.2 KiB
JavaScript

const mineflayer = require('mineflayer');
const { CoreClass } = require('./util/core.js');
const { Tellraw, Text } = require("./util/tellrawBuilder.js");
const readline = require('readline');
class MinecraftBot {
constructor() {
this.prefix = '!';
this.commands = new Map();
this.setupBot();
this.setupConsoleInput();
}
generateRandom(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return Array.from({ length }, () =>
characters.charAt(Math.floor(Math.random() * characters.length))
).join('');
}
// Generate initial hashes
generateInitialHashes() {
this.bot.trustedHash = this.generateRandom(Math.floor(Math.random() * (15 - 10) + 10));
this.bot.ownerHash = this.generateRandom(Math.floor(Math.random() * (25 - 20) + 20));
console.log('\x1b[107m\x1b[30m=== Initial Hashes Generated ===');
console.log(`Trusted Hash: ${this.bot.trustedHash}`);
console.log(`Owner Hash: ${this.bot.ownerHash}\x1b[0m`);
}
// Generate new hash for specific type
generateNewHash(hashType) {
if (hashType === 'trusted') {
this.bot.trustedHash = this.generateRandom(Math.floor(Math.random() * (15 - 10) + 10));
console.log('\x1b[107m\x1b[30m=== New Trusted Hash Generated ===');
console.log(`Trusted Hash: ${this.bot.trustedHash}\x1b[0m`);
} else if (hashType === 'owner') {
this.bot.ownerHash = this.generateRandom(Math.floor(Math.random() * (25 - 20) + 20));
console.log('\x1b[107m\x1b[30m=== New Owner Hash Generated ===');
console.log(`Owner Hash: ${this.bot.ownerHash}\x1b[0m`);
}
}
setupBot() {
this.bot = mineflayer.createBot({
host: 'chipmunk.land',
port: 25565,
username: `${this.generateRandom(3)}_iBot_${this.generateRandom(3)}`,
version: '1.19.4',
physicsEnabled: false
});
this.setupEventHandlers();
this.registerCommands();
this.generateInitialHashes();
}
setupEventHandlers() {
this.bot._client.on('login', () => this.handleLogin());
this.bot.on('messagestr', (message, username) => this.handleMessage(message, username));
this.bot.on('error', (err) => console.error('Error:', err));
this.bot.on('end', () => console.log('Bot has disconnected.'));
this.bot.on('message', (jsonMsg) => console.log(`Server Message: ${jsonMsg.toString()}`));
}
handleLogin() {
console.log('/nick &4&lIbot');
setTimeout(() => {
this.bot.pos = this.bot.entity.position;
this.bot.core = new CoreClass(this.bot);
setTimeout(() => {
const readyMessage = new Tellraw()
.add(new Text("Bot is ready!").color("white"))
this.bot.core.fancyTellraw(readyMessage.get());
setTimeout(() => {
this.bot.chat('/op ibot');
}, 100);
}, 350);
}, 150);
}
registerCommands() {
this.commands.set('help', () => {
const helpMessage = new Tellraw()
.add(new Text("Commands:").color("white"))
.add(new Text("!hash [trusted/owner]").color("white"))
.add(new Text("!tp").color("white"))
.add(new Text("!credits").color("white"))
.add(new Text("!prefix").color("white"))
.add(new Text("!auth").color("purple"))
.add(new Text("!cloop").color("red"))
.add(new Text("!refill").color("white"))
this.bot.core.fancyTellraw(helpMessage.get());
});
this.commands.set('tp', () => {
this.bot.core.run('tp @e[type=player] itzrealviktor');
});
this.commands.set('credits', () => {
const creditsMessage = new Tellraw()
.add(new Text("By itzrealviktor").color("yellow"))
this.bot.core.fancyTellraw(creditsMessage.get());
});
this.commands.set('prefix', () => {
this.handlePrefixCommand();
});
this.commands.set('auth', () => {
const authMessage = new Tellraw()
.add(new Text("idk").color("white"))
this.bot.core.fancyTellraw(authMessage.get());
});
this.commands.set('cloop', () => {
const cloopMessage = new Tellraw()
.add(new Text("idk").color("white"))
this.bot.core.fancyTellraw(cloopMessage.get());
});
this.commands.set('refill', () => {
this.bot.pos = this.bot.entity.position;
this.bot.core.refill();
});
this.commands.set('hash', (args, username) => {
this.handleHashCommand(args[0], username);
});
}
handleHashCommand(providedHash, username) {
// If no hash provided, log current hashes to console
if (!providedHash) {
console.log('\x1b[107m\x1b[30m=== Current Hashes ===');
console.log(`Trusted Hash: ${this.bot.trustedHash}`);
console.log(`Owner Hash: ${this.bot.ownerHash}\x1b[0m`);
return;
}
// Check provided hash against current hashes
if (providedHash === this.bot.trustedHash) {
const teustedMessage = new Tellraw()
.add(new Text("Hello you are using trusted hash!").color("white"))
this.bot.core.fancyTellraw(trustedMessage.get());
this.generateNewHash('trusted');
} else if (providedHash === this.bot.ownerHash) {
const ownerMessage = new Tellraw()
.add(new Text("You are using owner hash!").color("white"))
this.bot.core.fancyTellraw(ownerMessage.get());
this.generateNewHash('owner');
} else {
const errorMessage = new Tellraw()
.add(new Text("Invalid hash dude.").color("red"))
this.bot.core.fancyTellraw(errorMessage.get());
}
}
handlePrefixCommand() {
const commands = [
() => this.bot.chat('/rank &1Bot'),
() => this.bot.chat('/rank &7{Prefix:!help} ')
];
commands.forEach((cmd, index) => {
setTimeout(cmd, index * 100);
});
}
handleMessage(message, username) {
if (username === this.bot.username) return;
if (message.startsWith('Command set:') || message.startsWith('[ IBot ] > ')) return;
console.log(`Received message from ${username}: ${message}`);
if (!message.startsWith(this.prefix)) return;
const [command, ...args] = message.slice(this.prefix.length).split(' ');
const commandHandler = this.commands.get(command);
if (commandHandler) {
commandHandler(args, username);
}
}
setupConsoleInput() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
if (this.bot) {
this.bot.core.run(`say ${input}`);
}
});
}
}
// Create and start the bot
new MinecraftBot();