Advertisement
Guest User

mineflayer_openai.js

a guest
Sep 18th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mineflayer = require('mineflayer');
  2. const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder');
  3. const axios = require('axios');
  4. const { Vec3 } = require('vec3'); // Correct way to import Vec3
  5.  
  6.  
  7. const bot = mineflayer.createBot({
  8.   host: 'localhost', // Replace with your server address
  9.   port: 25565, // Replace with your server port
  10.   username: 'BashBot' // Replace with your bot's name
  11. });
  12.  
  13. bot.loadPlugin(pathfinder);
  14.  
  15.  
  16. // Function to get response from OpenAI API
  17. async function getOpenAIResponse(message) {
  18.   try {
  19.     const response = await axios.post('http://localhost:9090/v1/chat/completions', {
  20.       model: 'gemma-2-2b-it-q8_0', // Use the appropriate model
  21.       messages: [
  22.         { role: 'system', content: 'You are a helpful Minecraft bot. Keep responses down to one to two lines. Your username is BashBot.' },
  23.         { role: 'user', content: message }
  24.       ]
  25.     }, {
  26.       headers: {
  27.         'Authorization': `Bearer lm-studio`, // Replace with your OpenAI API key
  28.         'Content-Type': 'application/json'
  29.       }
  30.     });
  31.     return response.data.choices[0].message.content;
  32.   } catch (error) {
  33.     console.error('Error fetching response from OpenAI:', error);
  34.     return "Sorry, I couldn't process that.";
  35.   }
  36. }
  37.  
  38.  
  39. // Function to clear grass blocks
  40. async function clearGrassBlocks() {
  41.   const grassBlocks = bot.findBlocks({
  42.     matching: bot.registry.blocksByName.grass.id,
  43.     maxDistance: 64,
  44.     count: 100
  45.   });
  46.  
  47.   const defaultMove = new Movements(bot, bot.registry);
  48.  
  49.   for (const blockId of grassBlocks) {
  50.     const block = bot.blockAt(blockId);
  51.     if (block) {
  52.       // Move to the block position
  53.       bot.pathfinder.setMovements(defaultMove);
  54.       await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
  55.  
  56.       try {
  57.         await bot.dig(block);
  58.         console.log(`Cleared grass block at ${block.position}`);
  59.       } catch (err) {
  60.         console.error(`Failed to clear block at ${block.position}:`, err);
  61.       }
  62.     }
  63.   }
  64. }
  65.  
  66. // Function to clear wood blocks
  67. async function clearWoodBlocks() {
  68.   const woodBlocks = bot.findBlocks({
  69.     matching: bot.registry.blocksByName.oak_log.id, // Use the correct wood block name
  70.     maxDistance: 64,
  71.     count: 100
  72.   });
  73.  
  74.   const defaultMove = new Movements(bot, bot.registry);
  75.  
  76.   for (const blockId of woodBlocks) {
  77.     const block = bot.blockAt(blockId);
  78.     if (block) {
  79.       // Move to the block position
  80.       bot.pathfinder.setMovements(defaultMove);
  81.       await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
  82.  
  83.       try {
  84.         await bot.dig(block);
  85.         console.log(`Chopped wood block at ${block.position}`);
  86.       } catch (err) {
  87.         console.error(`Failed to chop block at ${block.position}:`, err);
  88.       }
  89.     }
  90.   }
  91. }
  92.  
  93.  
  94. // Function to clear dirt blocks
  95. async function clearDirtBlocks() {
  96.   const dirtBlocks = bot.findBlocks({
  97.     matching: bot.registry.blocksByName.dirt.id, // Use the correct wood block name
  98.     maxDistance: 64,
  99.     count: 64
  100.   });
  101.  
  102.   const defaultMove = new Movements(bot, bot.registry);
  103.  
  104.   for (const blockId of dirtBlocks) {
  105.     const block = bot.blockAt(blockId);
  106.     if (block) {
  107.       // Move to the block position
  108.       bot.pathfinder.setMovements(defaultMove);
  109.       await bot.pathfinder.goto(new GoalNear(block.position.x, block.position.y, block.position.z, 1));
  110.  
  111.       try {
  112.         await bot.dig(block);
  113.         console.log(`Chopped dirt block at ${block.position}`);
  114.       } catch (err) {
  115.         console.error(`Failed to chop block at ${block.position}:`, err);
  116.       }
  117.     }
  118.   }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. // Function to return to the player
  125. async function returnToPlayer(username) {
  126.   const player = bot.players[username];
  127.   if (player) {
  128.     const playerPosition = player.entity.position;
  129.  
  130.     const defaultMove = new Movements(bot, bot.registry);
  131.     bot.pathfinder.setMovements(defaultMove);
  132.     await bot.pathfinder.goto(new GoalNear(playerPosition.x, playerPosition.y, playerPosition.z, 1));
  133.     bot.chat('I have returned to you!');
  134.   } else {
  135.     bot.chat("I can't find you!");
  136.   }
  137. }
  138.  
  139.  
  140. // Listen for chat messages
  141. bot.on('chat', async (username, message) => {
  142.   if (username === bot.username) return; // Ignore messages from the bot itself
  143.  
  144.   if (message.toLowerCase() === 'clear grass') {
  145.     await clearGrassBlocks();
  146.     bot.chat('All grass blocks have been cleared!');
  147.   } else if (message.toLowerCase() === 'dig dirt') {
  148.     await clearDirtBlocks();
  149.     bot.chat('I have a fill dirt blocks inventory!');
  150.   } else if (message.toLowerCase() === 'chop wood') {
  151.     await clearWoodBlocks();
  152.     bot.chat('All wood blocks have been chopped!');
  153.   } else if (message.toLowerCase() === 'return') {
  154.     await returnToPlayer(username);
  155.   } else {
  156.     const response = await getOpenAIResponse(message);
  157.     bot.chat(response);
  158.   }
  159. });
  160. // Handle bot spawn event
  161. bot.on('spawn', () => {
  162.   console.log('Bot has spawned and is ready to chat!');
  163. });
  164.  
  165. // Handle errors
  166. bot.on('error', (err) => {
  167.   console.log('Error:', err);
  168. });
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement