{"size": 1556, "ext": "js", "max_stars_repo_path": "src/commands/utility/mdn.js", "max_stars_repo_name": "Loliticos/mayfi", "max_stars_repo_stars_event_min_datetime": "2020-03-12T19:35:05.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-03T02:34:24.000Z", "max_issues_repo_path": "src/commands/utility/mdn.js", "max_issues_repo_name": "Loliticos/mayfi", "max_issues_repo_issues_event_min_datetime": "2020-03-04T18:10:53.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-22T10:54:40.000Z", "max_forks_repo_path": "src/commands/utility/mdn.js", "max_forks_repo_name": "Loliticos/mayfi", "max_forks_repo_forks_event_min_datetime": "2020-03-12T19:33:23.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-07T17:33:29.000Z"}
{"max_stars_count": 5, "max_issues_count": 4, "max_forks_count": 3, "avg_line_length": 31.7551020408, "max_line_length": 118, "alphanum_fraction": 0.6028277635}
const { Command, MayfiEmbed, Constants } = require('../../')
const qs = require("querystring")
const fetch = require("node-fetch")
const Turndown = require("turndown")

module.exports = class MDN extends Command {
  constructor (client) {
    super({
      name: 'mdn',
      aliases: ['mozilla-developer-network'],
      category: 'utility',
      parameters: [{
        type: 'string', full: true, required: true, missingError: "commands:mdn.invalidDocument"
      }]
    }, client)
  }

  async run ({ channel, author, t, message, language}, query) {
        
    const embed = new MayfiEmbed()

      const queryString = qs.stringify({ q: query })

      const res = await fetch(`https://mdn.pleb.xyz/search?${queryString}`).then(res => res.json())

      if (!res) {
        embed
          .setColor(Constants.ERROR_COLOR)
          .setTitle(t("commands:mdn.invalidDocument"))
        return channel.send(embed)
      }

      const summary = res.Summary.replace(/<code><strong>(.+)<\/strong><\/code>/g, '<strong><code>$1</code></strong>')

      const turndown = new Turndown()
        turndown.addRule('hyperlink', {
          filter: 'a',
          replacement: (text, node) => `[${text}](https://developer.mozilla.org${res.URL})`,
      })

      embed
        .setAuthor("MDN", "https://developer.mozilla.org/static/img/favicon144.png", 'https://developer.mozilla.org/')
        .setURL(`https://developer.mozilla.org${res.URL}`)
        .setTitle(res.Title)
        .setDescription(turndown.turndown(summary))
      channel.send(embed)
  } 
}
