Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mineflayer = require('mineflayer');
- const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder');
- const axios = require('axios');
- const { Vec3 } = require('vec3'); // Correct way to import Vec3
- const bot = mineflayer.createBot({
- host: 'localhost', // Replace with your server address
- port: 25565, // Replace with your server port
- username: 'BashBot' // Replace with your bot's name
- });
- bot.loadPlugin(pathfinder);
- // Function to get response from OpenAI API
- async function getOpenAIResponse(message) {
- try {
- const response = await axios.post('http://localhost:9090/v1/chat/completions', {
- model: 'gemma-2-2b-it-q8_0', // Use the appropriate model
- messages: [
- { role: 'system', content: 'You are a helpful Minecraft bot. Keep responses down to one to two lines. Your username is BashBot.' },
- { role: 'user', content: message }
- ]
- }, {
- headers: {
- 'Authorization': `Bearer lm-studio`, // Replace with your OpenAI API key
- 'Content-Type': 'application/json'
- }
- });
- return response.data.choices[0].message.content;
- } catch (error) {
- console.error('Error fetching response from OpenAI:', error);
- return "Sorry, I couldn't process that.";
- }
- }
- // Function to clear grass blocks
- async function clearGrassBlocks() {
- const grassBlocks = bot.findBlocks({
- matching: bot.registry.blocksByName.grass.id,
- maxDistance: 64,
- count: 100
- });
- const defaultMove = new Movements(bot, bot.registry);
- for (const blockId of grassBlocks) {
- const block = bot.blockAt(blockId);
- if (block) {
- // Move to the block position
- bot.pathfinder.setMovements(defaultMove);
- await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
- try {
- await bot.dig(block);
- console.log(`Cleared grass block at ${block.position}`);
- } catch (err) {
- console.error(`Failed to clear block at ${block.position}:`, err);
- }
- }
- }
- }
- // Function to clear wood blocks
- async function clearWoodBlocks() {
- const woodBlocks = bot.findBlocks({
- matching: bot.registry.blocksByName.oak_log.id, // Use the correct wood block name
- maxDistance: 64,
- count: 100
- });
- const defaultMove = new Movements(bot, bot.registry);
- for (const blockId of woodBlocks) {
- const block = bot.blockAt(blockId);
- if (block) {
- // Move to the block position
- bot.pathfinder.setMovements(defaultMove);
- await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
- try {
- await bot.dig(block);
- console.log(`Chopped wood block at ${block.position}`);
- } catch (err) {
- console.error(`Failed to chop block at ${block.position}:`, err);
- }
- }
- }
- }
- // Function to clear dirt blocks
- async function clearDirtBlocks() {
- const dirtBlocks = bot.findBlocks({
- matching: bot.registry.blocksByName.dirt.id, // Use the correct wood block name
- maxDistance: 64,
- count: 64
- });
- const defaultMove = new Movements(bot, bot.registry);
- for (const blockId of dirtBlocks) {
- const block = bot.blockAt(blockId);
- if (block) {
- // Move to the block position
- bot.pathfinder.setMovements(defaultMove);
- await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
- try {
- await bot.dig(block);
- console.log(`Chopped dirt block at ${block.position}`);
- } catch (err) {
- console.error(`Failed to chop block at ${block.position}:`, err);
- }
- }
- }
- }
- // Function to return to the player
- async function returnToPlayer(username) {
- const player = bot.players[username];
- if (player) {
- const playerPosition = player.entity.position;
- const defaultMove = new Movements(bot, bot.registry);
- bot.pathfinder.setMovements(defaultMove);
- await bot.pathfinder.goto(new GoalNear(playerPosition.x, playerPosition.y, playerPosition.z, 1));
- bot.chat('I have returned to you!');
- } else {
- bot.chat("I can't find you!");
- }
- }
- // Listen for chat messages
- bot.on('chat', async (username, message) => {
- if (username === bot.username) return; // Ignore messages from the bot itself
- if (message.toLowerCase() === 'clear grass') {
- await clearGrassBlocks();
- bot.chat('All grass blocks have been cleared!');
- } else if (message.toLowerCase() === 'dig dirt') {
- await clearDirtBlocks();
- bot.chat('I have a fill dirt blocks inventory!');
- } else if (message.toLowerCase() === 'chop wood') {
- await clearWoodBlocks();
- bot.chat('All wood blocks have been chopped!');
- } else if (message.toLowerCase() === 'return') {
- await returnToPlayer(username);
- } else {
- const response = await getOpenAIResponse(message);
- bot.chat(response);
- }
- });
- // Handle bot spawn event
- bot.on('spawn', () => {
- console.log('Bot has spawned and is ready to chat!');
- });
- // Handle errors
- bot.on('error', (err) => {
- console.log('Error:', err);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement