mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
const Command = require('../../structures/Command');
|
|
const request = require('node-superfetch');
|
|
const cheerio = require('cheerio');
|
|
const { stripIndents } = require('common-tags');
|
|
const { WEBSTER_KEY } = process.env;
|
|
|
|
module.exports = class WordOfTheDayCommand extends Command {
|
|
constructor(client) {
|
|
super(client, {
|
|
name: 'word-of-the-day',
|
|
aliases: ['daily-word', 'wotd', 'word-of-day'],
|
|
group: 'search',
|
|
memberName: 'word-of-the-day',
|
|
description: 'Responds with today\'s word of the day.',
|
|
credit: [
|
|
{
|
|
name: 'Merriam-Webster\'s Collegiate® Dictionary',
|
|
url: 'https://www.merriam-webster.com/',
|
|
reason: 'API',
|
|
reasonURL: 'https://dictionaryapi.com/products/api-collegiate-dictionary'
|
|
}
|
|
]
|
|
});
|
|
|
|
this.cache = null;
|
|
}
|
|
|
|
async run(msg) {
|
|
try {
|
|
const word = await this.fetchWordOfTheDay();
|
|
let data;
|
|
if (this.cache.word === word) {
|
|
data = this.cache.data;
|
|
} else {
|
|
const { body } = await request
|
|
.get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`)
|
|
.query({ key: WEBSTER_KEY });
|
|
data = body[0];
|
|
this.cache = { word, data };
|
|
}
|
|
return msg.say(stripIndents`
|
|
**${data.meta.stems[0]}** (${data.fl})
|
|
${data.shortdef.map((definition, i) => `(${i + 1}) ${definition}`).join('\n')}
|
|
`);
|
|
} catch (err) {
|
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
}
|
|
}
|
|
|
|
async fetchWordOfTheDay() {
|
|
const { text } = await request.get('https://www.merriam-webster.com/word-of-the-day');
|
|
const $ = cheerio.load(text);
|
|
return $('div[class="word-and-pronunciation"]').first().children().first().text();
|
|
}
|
|
};
|