text: const { Client, MessageEmbed } = require('discord.js');
  const mineflayer = require('mineflayer');
  const needle = require('needle');
  const data = require(`./config.js`);
  const client = new Client();
  
  const bot = mineflayer.createBot({
    host: 'hypixel.net',
    username: data.email,
    password: data.password,
    version: data.version,        
  });
  
  //Announce the minecraft message in discord
  function announce(name, message){
      needle.get(`https://api.mojang.com/users/profiles/minecraft/${name}`, {json: true}, (err, res) => {
  	    if (err) { 
  	          return -1; 
  	      }
  	    let todo = res.body;
  	    const id = todo.id;
  	    const face = `https://crafatar.com/avatars/${id}`;
  	    const _name = todo.name;
  	    const em = new MessageEmbed()
  	    	.setAuthor(_name, face)
  	    	.setDescription(message)
  	    	.setTimestamp()
    		const _channel = client.channels.cache.get(data.announcementChannel);
   		 _channel.send(em)
   	});  
  };
  
  //Get minecraft name and message
  function extractName(string){
  	let text = string.replace('Guild > ','')
  	let namePart = text.split(':')[0]
  	let message = text.replace(namePart+':','').trim()
  	if (namePart.startsWith('[')){
  		namePart = namePart.split(' ')[1]
  	};
  	let name = namePart.split(' ')[0]
  	return [name, message]
  
  }
  
  //Announce the discord message in minecraft
  function announceInGC(name, message){
  	const allowedLength = 100-`/gchat [D] ${name}: `.length;
  	const messageWL = message.substring(0,allowedLength-3);
  	if(messageWL.length === 97){
  		messageWL+='...';
  	};
  	bot.chat(`/gchat [D] ${name}: ${messageWL}`);
  };
  
  
  client.on('message', message => {
    if (message.author.bot) return;
    if (message.channel.id === data.announcementChannel){
      announceInGC(message.author.username, message.content)
      };
  });
  
  bot.on('messagestr', (msg) => {
  	if (!msg.includes(':')) return;
  	if (msg.startsWith('Guild > ')){
  		const mcData = extractName(msg);
  		if (mcData[0]===bot.username) return;
  		announce(mcData[0], mcData[1])
  	}
  });
  
  client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
  });
  
  bot.on('kicked', console.log)
  bot.on('error', console.log)
  
  client.login(data.token);
  