2020-04-30 02:18:15 -04:00
|
|
|
import { Message } from 'eris';
|
|
|
|
import axios from 'axios';
|
|
|
|
import { Client, Command, RichEmbed } from '../class';
|
|
|
|
|
2020-04-30 21:57:02 -04:00
|
|
|
export default class NPM extends Command {
|
2020-04-30 02:18:15 -04:00
|
|
|
constructor(client: Client) {
|
|
|
|
super(client);
|
|
|
|
this.name = 'npm';
|
|
|
|
this.description = 'Get information about npm modules.';
|
|
|
|
this.usage = 'npm <module name>';
|
|
|
|
this.permissions = 0;
|
|
|
|
this.guildOnly = false;
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async run(message: Message, args: string[]) {
|
|
|
|
try {
|
2020-05-02 21:03:34 -04:00
|
|
|
if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
|
2020-04-30 02:18:15 -04:00
|
|
|
|
|
|
|
const res = await axios.get(`https://registry.npmjs.com/${args[0]}`, { validateStatus: (_) => true });
|
|
|
|
|
2020-05-01 01:50:57 -04:00
|
|
|
if (res.status === 404) return this.error(message.channel, 'Could not find the library, try something else.');
|
2020-04-30 02:18:15 -04:00
|
|
|
|
|
|
|
const { data } = res;
|
|
|
|
|
2020-04-30 21:57:02 -04:00
|
|
|
const bugs: string = data.bugs?.url || '';
|
2020-04-30 02:18:15 -04:00
|
|
|
const description: string = data.description || 'None';
|
2020-04-30 21:57:02 -04:00
|
|
|
const version: string = data['dist-tags']?.latest || 'Unknown';
|
2020-04-30 02:18:15 -04:00
|
|
|
const homepage: string = data.homepage || '';
|
2020-04-30 21:57:02 -04:00
|
|
|
let license: string = 'None';
|
|
|
|
if (typeof data.license === 'object') {
|
|
|
|
license = data.license.type;
|
|
|
|
} else if (typeof data.license === 'string') {
|
|
|
|
license = data.license;
|
|
|
|
}
|
|
|
|
let dependencies: string = 'None';
|
2020-05-16 15:03:21 -04:00
|
|
|
if (version !== 'Unknown' && data.versions[version].dependencies !== undefined && Object.keys(data.versions[version].dependencies).length > 0) {
|
2020-04-30 21:57:02 -04:00
|
|
|
dependencies = Object.keys(data.versions[version].dependencies).join(', ');
|
|
|
|
}
|
2020-04-30 02:18:15 -04:00
|
|
|
const name: string = data.name || 'None';
|
|
|
|
const repository: string = bugs.replace('/issues', '') || '';
|
|
|
|
const creation: string = data.time.created ? new Date(data.time.created).toLocaleString('en') : 'None';
|
|
|
|
const modification: string = data.time.modified ? new Date(data.time.modified).toLocaleString('en') : 'None';
|
|
|
|
|
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setColor(0xCC3534);
|
2020-04-30 02:34:15 -04:00
|
|
|
embed.setTimestamp();
|
|
|
|
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
|
2020-04-30 02:18:15 -04:00
|
|
|
embed.setAuthor('NPM', 'https://i.imgur.com/ErKf5Y0.png', 'https://www.npmjs.com/');
|
2020-04-30 21:57:02 -04:00
|
|
|
embed.setDescription(`[NPM](https://www.npmjs.com/package/${args[0]}) | [Homepage](${homepage}) | [Repository](${repository}) | [Bugs](${bugs})`);
|
2020-04-30 02:18:15 -04:00
|
|
|
embed.addField('Name', name, true);
|
|
|
|
embed.addField('Latest version', version, true);
|
|
|
|
embed.addField('License', license, true);
|
|
|
|
embed.addField('Description', description, false);
|
|
|
|
embed.addField('Dependencies', dependencies, false);
|
|
|
|
embed.addField('Creation Date', creation, true);
|
|
|
|
embed.addField('Modification Date', modification, true);
|
|
|
|
|
|
|
|
return message.channel.createMessage({ embed });
|
|
|
|
} catch (err) {
|
|
|
|
return this.client.util.handleError(err, message, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|