Files
xiao/commands/search/define.js
T
2024-03-30 11:59:42 -04:00

45 lines
1.3 KiB
JavaScript

const Command = require('../../framework/Command');
const request = require('node-superfetch');
const { stripIndents } = require('common-tags');
const { WEBSTER_KEY } = process.env;
module.exports = class DefineCommand extends Command {
constructor(client) {
super(client, {
name: 'define',
aliases: ['dictionary', 'webster'],
group: 'search',
memberName: 'define',
description: 'Defines a word.',
credit: [
{
name: 'Merriam-Webster\'s Collegiate® Dictionary',
url: 'https://www.merriam-webster.com/',
reason: 'API',
reasonURL: 'https://dictionaryapi.com/products/api-collegiate-dictionary'
}
],
args: [
{
key: 'word',
type: 'string',
parse: word => encodeURIComponent(word)
}
]
});
}
async run(msg, { word }) {
const { body } = await request
.get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`)
.query({ key: WEBSTER_KEY });
if (!body.length) return msg.say('Could not find any results.');
const data = body[0];
if (typeof data === 'string') return msg.say(`Could not find any results. Did you mean **${data}**?`);
return msg.say(stripIndents`
**${data.meta.stems[0]}** (${data.fl})
${data.shortdef.map((definition, i) => `(${i + 1}) ${definition}`).join('\n')}
`);
}
};