Skip to content
This repository has been archived by the owner on Jan 19, 2025. It is now read-only.

added mdn documentation command #1196

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/apis/MDN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { APIWrapper } = require('../')
const axios = require('axios')

const API_URL = 'https://mdn.almeidx.me/api'

module.exports = class MDN extends APIWrapper {
constructor () {
super({
name: 'mdn'
})
}

async search (name) {
const res = await axios.get(`${API_URL}/search`, {
params: {
q: encodeURIComponent(name)
}
})
return res.data
}

async getInfo (link) {
const res = await axios.get(`${API_URL}/info`, {
params: {
l: link
}
})
return res.data
}
}
43 changes: 43 additions & 0 deletions src/commands/utility/mdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { SearchCommand, SwitchbladeEmbed, Constants } = require('../..')
const moment = require('moment')

const BASE_URL = 'https://developer.mozilla.org'

module.exports = class MDN extends SearchCommand {
constructor (client) {
super({
name: 'mdn',
aliases: ['mdndoc', 'mdndocumentation', 'mozilladevelopernetwork'],
requirements: { apis: ['mdn'] },
embedColor: Constants.MDN_COLOR,
embedLogoURL: 'https://i.imgur.com/6IzokAO.jpeg'
}, client)
}

search (_, query) {
return this.client.apis.mdn.search(query)
}

searchResultFormatter (i) {
return `[${i.name}](${BASE_URL}${i.url})`
}

async handleResult ({ t, author, channel, language }, info) {
moment.locale(language)
channel.startTyping()

const { description, name, url, parameters, returns, syntax } = await this.client.apis.mdn.getInfo(info.url)

const embed = new SwitchbladeEmbed(author)
.setColor(this.embedColor)
.setAuthor(t('commands:mdn.mdn'), this.embedLogoURL, BASE_URL)
.setTitle(name)
.setURL(url)
.setDescription(description)
.addField(t('commands:mdn.syntax'), `\`\`\`js\n${syntax}\n\`\`\`\n${parameters}`)

if (returns) embed.addField(t('commands:mdn.returns'), returns)

channel.send(embed).then(() => channel.stopTyping(true))
}
}
7 changes: 7 additions & 0 deletions src/locales/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -1756,5 +1756,12 @@
"by": "by {{authors}}",
"installs": "{{count}} installs",
"license": "License"
},
"mdn": {
"commandDescription": "Seaches for JavaScript documentation on the Mozilla Developer Network",
"commandUsage": "<query>",
"syntax": "Syntax",
"returns": "Returns",
"mdn": "Mozilla Developer Network"
}
}
1 change: 1 addition & 0 deletions src/utils/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
CHOCOLATEY_COLOR: '#7eb5e2',
SNAPCRAFT_COLOR: '82BEA0',
JETBRAINS_PLUGIN: '#d44386',
MDN_COLOR: '#88d1f1',

// Emojis

Expand Down