import type { EmbedField , Message , TextChannel } from "eris" ; import { Command } from "../../classes/Command" ; import { dateFormat } from "../../utils/format" ; import axios from "axios" ; export class npmCommand extends Command { description = "Searches for a package on npm." ; args = "<package:string>" ; aliases = [ "yarn" ] ; cooldown = 3000 ; allowdms = true ; async run ( msg , _pargs , args ) { const query = encodeURIComponent ( args . join ( " " ) . toLowerCase ( ) ) ; const body = await axios . get ( `https://registry.npmjs.com/${ query }` ) . catch ( ( ) => { } ) ; const isYarn = msg . content . toLowerCase ( ) . includes ( "yarn" ) ; if ( ! body || ! body . data || body . data ?. error || ! body . data ?. [ "dist-tags" ] ) { return msg . createEmbed ( msg . string ( "global.ERROR" ) , msg . string ( "utility.PACKAGE_NOTFOUND" ) , "error" ) ; } const pkg = body . data . versions [ body . data [ "dist-tags" ] . latest ] ; const fields = [ ] ; if ( pkg . keywords ?. length ) fields . push ( { name : msg . string ( "utility.KEYWORDS" ) , value : `${ pkg . keywords . map ( ( k ) => `\`${ k }\`` ) . join ( ", " ) }` , inline : false , } ) ; fields . push ( { name : msg . string ( "global.URL" ) , value : `[https://${ isYarn ? "yarnpkg" : "npmjs" }.com/package/${ args . join ( " " ) }](https://www.npmjs.com/package/${ args . join ( " " ) })` , inline : false , } ) ; fields . push ( { name : msg . string ( "utility.LATEST_VERSION" ) , value : body . data [ "dist-tags" ] . latest , inline : true , } ) ; if ( ! body . data . license ?. type ) { fields . push ( { name : msg . string ( "utility.LICENSE" ) , value : `${ body . data . license }` , inline : true , } ) ; } else if ( body . data . license ?. type ) { fields . push ( { name : msg . string ( "utility.LICENSE" ) , value : `${ body . data . license . type }` , inline : true , } ) ; } if ( pkg . maintainers ?. length ) fields . push ( { name : msg . string ( "utility.MAINTAINERS" ) , value : pkg . maintainers . map ( ( m ) => `\`${ m . name }\`` ) . join ( ", " ) , inline : false , } ) ; if ( body . data . time ?. created ) fields . push ( { name : msg . string ( "global.CREATED_AT" ) , value : `${ dateFormat ( body . data . time . created , msg . string ) }` , inline : true , } ) ; if ( body . data . time ?. modified ) fields . push ( { name : msg . string ( "global.UPDATED_AT" ) , value : `${ dateFormat ( body . data . time . modified , msg . string ) }` , inline : true , } ) ; msg . channel . createMessage ( { embed : { title : `${ isYarn ? "🧶" : "📦" } ${ pkg . name }` , description : pkg . description , color : isYarn ? 0x2c8ebb : 0xcc3534 , fields : fields , footer : { text : `${ msg . string ( "global.RAN_BY" , { author : msg . tagUser ( msg . author ) } ) }` , icon_url : msg . author . dynamicAvatarURL ( ) , } , } , } ) ; } }