From 1d8d231b47ceca1d9ecb9971668414e822c0543b Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 8 Apr 2020 18:47:44 -0400 Subject: [PATCH] Add definitions to hangman --- README.md | 1 + commands/games-sp/hangman.js | 43 ++++++++++++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 48909a6e..7732980b 100644 --- a/README.md +++ b/README.md @@ -853,6 +853,7 @@ here. * mdn (API) - [Merriam-Webster's Collegiate® Dictionary](https://www.merriam-webster.com/) * define ([API](https://dictionaryapi.com/products/api-collegiate-dictionary)) + * hangman ([API](https://dictionaryapi.com/products/api-collegiate-dictionary)) - [mikewesthad](https://github.com/mikewesthad) * pirate ([English-to-Pirate Dictionary Data](https://github.com/mikewesthad/pirate-speak/blob/master/lib/pirate-speak.js#L1-L155)) - [Minecraft Achievement Generator](https://www.minecraftskinstealer.com/achievement/) diff --git a/commands/games-sp/hangman.js b/commands/games-sp/hangman.js index dd1aeaa5..42a6b89d 100644 --- a/commands/games-sp/hangman.js +++ b/commands/games-sp/hangman.js @@ -1,6 +1,8 @@ const Command = require('../../structures/Command'); +const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); const words = require('../../assets/json/word-list'); +const { WEBSTER_KEY } = process.env; module.exports = class HangmanCommand extends Command { constructor(client) { @@ -15,6 +17,12 @@ module.exports = class HangmanCommand extends Command { url: 'https://en.wikipedia.org/wiki/Grady_Ward', reason: 'Moby Word Lists', reasonURL: 'http://www.gutenberg.org/ebooks/3201' + }, + { + name: 'Merriam-Webster\'s Collegiate® Dictionary', + url: 'https://www.merriam-webster.com/', + reason: 'API', + reasonURL: 'https://dictionaryapi.com/products/api-collegiate-dictionary' } ] }); @@ -77,11 +85,42 @@ module.exports = class HangmanCommand extends Command { } } this.client.games.delete(msg.channel.id); - if (word.length === confirmation.length || guessed) return msg.say(`You won, it was ${word}!`); - return msg.say(`Too bad... It was ${word}...`); + const defined = await this.defineWord(word); + if (word.length === confirmation.length || guessed) { + return msg.say(stripIndents` + You won, it was ${word}! + + **${defined.name}** (${defined.partOfSpeech}) + ${defined.definiton} + `); + } + return msg.say(stripIndents` + Too bad... It was ${word}... + + **${defined.name}** (${defined.partOfSpeech}) + ${defined.definiton} + `); } catch (err) { this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } + + async defineWord(word) { + try { + const { body } = await request + .get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`) + .query({ key: WEBSTER_KEY }); + if (!body.length) return null; + const data = body[0]; + if (typeof data === 'string') return null; + return { + name: data.meta.stems[0], + partOfSpeech: data.fl, + definiton: data.shortdef.map((definition, i) => `(${i + 1}) ${definition}`).join('\n') + }; + } catch (err) { + return null; + } + } }; diff --git a/package.json b/package.json index 0ac87eb4..ec28910c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "112.19.9", + "version": "112.19.10", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {