All files memeit.js

57.14% Statements 12/21
100% Branches 0/0
44.44% Functions 4/9
57.14% Lines 12/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78          1x 1x   1x                         2x   2x           2x 2x     2x 2x 2x             2x 2x                                                                    
/**
 * Have butlerbot get a meme and apply the previous text to it
 * @module memeit
 * @return {Object} - image (as a message to discord text channel)
 */
const axios = require('axios');
const queryString = require('query-string');
 
module.exports = {
  name: 'memeit',
  description: 'Overlay previous message onto a randomly selected meme',
  /**
   * @method execute
   * @param {string} message - command, used to determine which channel to return results
   * @return {string} link to custom meme created
   */  
  execute(message) { 
    /** 
     * @const {string} memeApi
     * @summary Get a random meme from imglfip API
     */
    const memeApi = 'https://api.imgflip.com/get_memes';     
    // grab the full list of memes and randomly select one from the list
    let memePromise = axios.get(memeApi)
    .then(response => {
      /** 
       * @var {string} memeList
       * @summary list of JSON objects including meme and ID
       */      
      var memeList = response.data.data.memes.filter(function (meme) {
        return meme.box_count <= 2;
      });
      // pick a random meme from the list and return the id, needed for the meme generation
      const rand = Math.floor(Math.random() * memeList.length);
      response = memeList[rand];
      return response.id;
    })        
    .catch(error => {
      console.log(error);
      return message.channel.send(`I'm unable to meme-ify the message, sorry master ${message.author}`);
    });
    // form the request header to generate the meme
    memePromise.then(memeId => {
      message.channel.messages.fetch({ limit: 2 }).then(messages => {
        /** 
         * @var {string} body
         * @summary Body of request for meme
         */        
        let body = queryString.stringify({
          'username': process.env.IMGFLIP_USERNAME,
          'password': process.env.IMGFLIP_PASSWORD,
          'template_id': memeId,
          'text0': messages.last().content,
          'text1': ''
        });
        return body;
      })
      // sent fully formed request to api and return the link to the image
      .then((captionBody) => {
        /** 
         * @const {string} captionApi
         * @summary caption image endpoint used to generate custom meme
         */
        const captionApi = 'https://api.imgflip.com/caption_image';     
        // post request with our custom text and random image, then send to channel
        axios.post(captionApi + '?' + captionBody)
        .then(response => {
          message.channel.send(response.data.data.url);
        })        
        .catch(error => {
          console.log(error);
          return message.channel.send(`I'm unable to caption the message, sorry master ${message.author}`);
        });
      });  
    });
  }
};