From 5ffc3a5eb7e39ddc3c6cca858b3a5396dfa4e52e Mon Sep 17 00:00:00 2001 From: Matthew R Date: Thu, 30 Apr 2020 21:57:02 -0400 Subject: [PATCH] fixes to npm command --- src/commands/npm.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/commands/npm.ts b/src/commands/npm.ts index 5ef7e3c..98a489f 100644 --- a/src/commands/npm.ts +++ b/src/commands/npm.ts @@ -2,7 +2,7 @@ import { Message } from 'eris'; import axios from 'axios'; import { Client, Command, RichEmbed } from '../class'; -export default class Npm extends Command { +export default class NPM extends Command { constructor(client: Client) { super(client); this.name = 'npm'; @@ -19,16 +19,24 @@ export default class Npm extends Command { const res = await axios.get(`https://registry.npmjs.com/${args[0]}`, { validateStatus: (_) => true }); - if (res.status == 404) this.error(message.channel, 'Could not find the library, try something else.'); + if (res.status === 404) this.error(message.channel, 'Could not find the library, try something else.'); const { data } = res; - const bugs: string = data.bugs.url || ''; + const bugs: string = data.bugs?.url || ''; const description: string = data.description || 'None'; - const version: string = data['dist-tags'].latest || 'Unknown'; + const version: string = data['dist-tags']?.latest || 'Unknown'; const homepage: string = data.homepage || ''; - const license: string = data.license || 'None'; - const dependencies: string = Object.keys(data.versions[version].dependencies).join(', ') || 'None'; + 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'; + if (version !== 'Unknown' && data.versions[version].dependencies) { + dependencies = Object.keys(data.versions[version].dependencies).join(', '); + } 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'; @@ -39,7 +47,7 @@ export default class Npm extends Command { embed.setTimestamp(); embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setAuthor('NPM', 'https://i.imgur.com/ErKf5Y0.png', 'https://www.npmjs.com/'); - embed.setDescription(`[NPM](https://www.npmjs.com/package/${args[0]}) | [Homepage](${homepage}) | [Repository](${repository}) | [Bugs](${bugs})`); + embed.setDescription(`[NPM](https://www.npmjs.com/package/${args[0]}) | [Homepage](${homepage}) | [Repository](${repository}) | [Bugs](${bugs})`); embed.addField('Name', name, true); embed.addField('Latest version', version, true); embed.addField('License', license, true);