discord_bot/commands/info-search/npm.js

52 lines
2.3 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 { MessageEmbed } = require("discord.js");
const fetch = require("node-fetch");
const moment = require("moment");
module.exports = {
name: "npm",
description: "Suchen Sie nach Paketen auf npm!",
category: "search",
run: async (client, message, args) => {
let query = args.join(' ');
if (!query) query = await awaitMessages(message);
if (!query) return;
const res = await fetch(`https://registry.npmjs.com/${encodeURIComponent(query)}`).catch(err => console.log(err));
if (res.status === 404) return message.channel.send('Keine Suchergebnisse gefunden, versuchen Sie vielleicht, nach etwas zu suchen, das existiert.');
const body = await res.json();
const embed = new MessageEmbed()
.setColor(0xde2c2c)
.setTitle(body.name)
.setURL(`https://www.npmjs.com/package/${body.name}`)
.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 : '???', true)
.addField(' Creation Date', moment.utc(body.time.created).format('YYYY/MM/DD hh:mm:ss'), true)
.addField(' Modification Date', body.time.modified ? moment.utc(body.time.modified).format('YYYY/MM/DD hh:mm:ss') : 'None', true)
.addField(' Repository', body.repository ? `[View Here](${body.repository.url.split('+')[1]})` : 'None', true)
.addField(' Maintainers', body.maintainers.map(user => user.name).join(', '))
message.channel.send(embed);
async function awaitMessages(message) {
let responce;
const filter = (user) => {
return user.author.id === message.author.id;
};
message.channel.send('**Wonach möchten Sie suchen?** \nGeben Sie „cancel“ ein, um den Befehl abzubrechen.');
await message.channel.awaitMessages(filter, { max: 1, time: 120000, errors: ['time'] })
.then((msg) => {
const firstMsg = msg.first();
if (firstMsg.content.toLowerCase() === 'cancel') return firstMsg.react('👍');
responce = firstMsg.content;
})
.catch(() => {
message.channel.send('Nun, Sie haben zu lange gebraucht, um den Befehl abzubrechen.');
});
return responce;
}
},
};