From af99a7b6d9d4be9e58de367bbd3f3f3b80dafb17 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 5 May 2020 22:05:44 -0400 Subject: [PATCH] Jeopardy Command --- README.md | 7 +- commands/edit-image/jeopardy-question.js | 2 +- commands/games-sp/jeopardy.js | 83 ++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 commands/games-sp/jeopardy.js diff --git a/README.md b/README.md index 8d9cff12..0886c51f 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 413 +Total: 414 ### Utility: @@ -383,6 +383,7 @@ Total: 413 * **hangman:** Prevent a man from being hanged by guessing a word as fast as you can. * **horse-race:** Bet on the fastest horse in a 6-horse race. * **hunger-games:** Simulate a Hunger Games match with up to 24 tributes. +* **jeopardy:** Answer a Jeopardy question. * **lottery:** Attempt to win the lottery with 6 numbers. * **mad-libs:** Choose words that fill in the blanks to create a crazy story! * **math-quiz:** See how fast you can answer a math problem in a given time limit. @@ -901,6 +902,7 @@ here. - [JellyNeo Item Database](https://items.jellyneo.net/) * neopets-item (Item Data) - [Jeopardy](https://www.jeopardy.com/) + * jeopardy (Original Show) * jeopardy-question (Original Show) - [Jessica Knable](https://picsart.com/u/jessicaknable) * hearts ([Image](https://picsart.com/i/sticker-hearts-heart-borders-frames-round-frame-border-love-263412201018212)) @@ -910,6 +912,8 @@ here. * to-be-continued (Original Anime) - [Jon Bernhardt](http://web.mit.edu/jonb/www/) * bart-chalkboard ([Akbar Font](https://www.wobblymusic.com/groening/akbar.html)) +- [jService](http://jservice.io/) + * jeopardy (API) - [Kickstarter](https://www.kickstarter.com/) * kickstarter (API) - [KINMOZA!](http://www.kinmosa.com/) @@ -1029,6 +1033,7 @@ here. - [OpenWeatherMap](https://openweathermap.org/) * weather ([API](https://openweathermap.org/api)) - [OPTIFONT](http://opti.netii.net/) + * jeopardy ([Korinna Agency Font](https://fontmeme.com/fonts/korinna-agency-font/)) * jeopardy-question ([Korinna Agency Font](https://fontmeme.com/fonts/korinna-agency-font/)) - [osu!](https://osu.ppy.sh/home) * osu ([API](https://github.com/ppy/osu-api/wiki)) diff --git a/commands/edit-image/jeopardy-question.js b/commands/edit-image/jeopardy-question.js index 972f511e..5205c999 100644 --- a/commands/edit-image/jeopardy-question.js +++ b/commands/edit-image/jeopardy-question.js @@ -8,7 +8,7 @@ module.exports = class JeopardyQuestionCommand extends Command { constructor(client) { super(client, { name: 'jeopardy-question', - aliases: ['jeopardy', 'clue-card', 'jeopardy-clue-card', 'jeopardy-clue'], + aliases: ['clue-card', 'jeopardy-clue-card', 'jeopardy-clue'], group: 'edit-image', memberName: 'jeopardy-question', description: 'Sends a Jeopardy Question with the text of your choice.', diff --git a/commands/games-sp/jeopardy.js b/commands/games-sp/jeopardy.js new file mode 100644 index 00000000..66c3abee --- /dev/null +++ b/commands/games-sp/jeopardy.js @@ -0,0 +1,83 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const { createCanvas, registerFont } = require('canvas'); +const path = require('path'); +const { wrapText } = require('../../util/Canvas'); +registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'OPTIKorinna-Agency.otf'), { family: 'Korinna' }); + +module.exports = class JeopardyCommand extends Command { + constructor(client) { + super(client, { + name: 'jeopardy', + group: 'games-sp', + memberName: 'jeopardy', + description: 'Answer a Jeopardy question.', + credit: [ + { + name: 'jService', + url: 'http://jservice.io/', + reason: 'API' + }, + { + name: 'Jeopardy', + url: 'https://www.jeopardy.com/', + reason: 'Original Show' + }, + { + name: 'OPTIFONT', + url: 'http://opti.netii.net/', + reason: 'Korinna Agency Font', + reasonURL: 'https://fontmeme.com/fonts/korinna-agency-font/' + } + ] + }); + } + + async run(msg) { + try { + const question = await this.fetchQuestion(); + const clueCard = await this.generateClueCard(question.question); + await msg.reply(`The category is: **${question.category.title.toUpperCase()}**. 30 seconds, good luck.`, { + files: [{ attachment: clueCard, name: 'clue-card.png' }] + }); + const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { + max: 1, + time: 30000 + }); + if (!msgs.size) return msg.reply(`Time's up, the answer was **${question.answer}**.`); + const win = msgs.first().content.toLowerCase() === question.answer.toLowerCase(); + if (!win) return msg.reply(`The answer was **${question.answer}**.`); + return msg.reply(`The answer was **${question.answer}**. Good job!`); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async fetchQuestion() { + const { body } = await request + .get('http://jservice.io/api/random') + .query({ count: 1 }); + return body[0]; + } + + async generateClueCard(question) { + const canvas = createCanvas(1280, 720); + const ctx = canvas.getContext('2d'); + ctx.fillStyle = '#030e78'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + ctx.textAlign = 'center'; + ctx.textBaseline = 'top'; + ctx.fillStyle = 'white'; + ctx.font = '62px Korinna'; + const lines = await wrapText(ctx, question.toUpperCase(), 813); + const topMost = (canvas.height / 2) - (((52 * lines.length) / 2) + ((20 * (lines.length - 1)) / 2)); + for (let i = 0; i < lines.length; i++) { + const height = topMost + ((52 + 20) * i); + ctx.fillStyle = 'black'; + ctx.fillText(lines[i], (canvas.width / 2) + 6, height + 6); + ctx.fillStyle = 'white'; + ctx.fillText(lines[i], canvas.width / 2, height); + } + return canvas.toBuffer(); + } +}; diff --git a/package.json b/package.json index 3ea532ac..55845206 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "114.5.8", + "version": "114.6.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {