snowcatman

example 2 chat and whisper

Jan 15th, 2021
649
130 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mineflayer = require('mineflayer');
  2. console.log("begin mineflayer js file")
  3. const bot = mineflayer.createBot()
  4. console.log("defualt login info used -- bot loging in")
  5.  
  6. bot.on('login', () => {
  7.   console.log('Bot connected');
  8. })
  9.  
  10. bot.on('spawn', () => {
  11.   bot.chat("Hello, all!!");
  12.   console.log('bot spawned');
  13. })
  14.  
  15. bot.on("chat", (username, message) => {
  16.   console.log(`chat ${message}`)
  17.   if (username === bot.username) return
  18. /*  if (message = `{Hi|hi|helo|hello|Hello}`) { */
  19.   if (message.match = (/hi |Hi |hello |Hello |hey |Hey |Hay |hay /g)) {
  20.   console.log("chat hi used")
  21.     bot.chat(`Hello ${username} I am an example bot`)
  22.     bot.chat('you can tell me to leave the server')
  23.     bot.chat(' but you have to whisper it the chat bar to me')
  24.     bot.chat(`"/tell ${bot.username} leave"`)
  25.     bot.chat("but the example is when you tell me hi in whisper")
  26.     bot.chat(`"/tell ${bot.username} hi"`)
  27.     bot.chat(`"I think I echo, stutter or something."`)
  28.   }
  29. })
  30.  
  31. bot.on("whisper", (username, message,) => {
  32.   if (username === bot.username) return
  33.     console.log(`whisper ${message}`)
  34.   if (message === "hi") {
  35.     console.log("wisper hi used")
  36.     bot.whisper(username, (`Hello ${username}`))
  37.     bot.whisper(username, ("as you can see I am going to respond in open chat like a parrot"))
  38.     bot.whisper(username, ("I should not do this.... if chat and whisper are seperated"))
  39.     bot.whisper(username, ("thank you"))
  40.   }
  41.   if (message === "leave") {
  42.     quit();
  43.   }
  44. });
  45.  
  46. function lookAtNearestPlayer() {
  47.   const playerFilter = (entity) => entity.type === 'player'
  48.   const playerEntity = bot.nearestEntity(playerFilter)
  49.   if (!playerEntity) return
  50.   const pos = playerEntity.position.offset(0, playerEntity.height, 0)
  51.   bot.lookAt(pos)
  52. }
  53.  
  54. function quit() {
  55.   bot.chat("I got to go now. See you later")
  56.   bot.whisper('username', `Quit command used, I will leave the server now!! ---> count to 5 slowly`);
  57.   console.log('quit command used, and proccess ending');
  58.   setTimeout(() => {
  59.     bot.quit('quitting')
  60.     console.log("ending mineflayer js file and exiting proccess instance")
  61.   }, 5000) /* 1000 = 1 secound */
  62.   setTimeout(() => {
  63.     process.exit(0)
  64.   }, 5000) /* 1000 = 1 secound */
  65. }
  66. bot.on('physicTick', lookAtNearestPlayer);
RAW Paste Data