Files
xiao/commands/search/npm.js
T
Daniel Odendahl Jr ffa05f01ff structures -> util
2017-10-13 21:46:29 +00:00

58 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { shorten } = require('../../util/Util');
module.exports = class NPMCommand extends Command {
constructor(client) {
super(client, {
name: 'npm',
aliases: ['npm-package'],
group: 'search',
memberName: 'npm',
description: 'Searches NPM for info on an NPM package.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'query',
prompt: 'What package would you like to search for?',
type: 'string',
parse: query => encodeURIComponent(query)
}
]
});
}
async run(msg, { query }) {
try {
const { body } = await snekfetch.get(`https://registry.npmjs.com/${query}`);
const embed = new MessageEmbed()
.setColor(0xCB0000)
.setAuthor('NPM', 'https://i.imgur.com/ErKf5Y0.png')
.setTitle(body.name)
.setURL(`https://www.npmjs.com/package/${query}`)
.setDescription(body.description || 'No Description.')
.addField(' Version',
body['dist-tags'].latest, true)
.addField(' License',
body.license || 'None', true)
.addField(' Author',
body.author ? body.author.name : 'Unknown', true)
.addField(' Created',
new Date(body.time.created).toDateString(), true)
.addField(' Modified',
new Date(body.time.modified).toDateString(), true)
.addField(' Main File',
body.versions[body['dist-tags'].latest].main, true)
.addField(' Keywords',
body.keywords && body.keywords.length ? shorten(body.keywords.join(', '), 1000) : 'None')
.addField(' Maintainers',
body.maintainers.map(user => user.name).join(', '));
return msg.embed(embed);
} catch (err) {
if (err.status === 404) return msg.say('Could not find any results.');
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};