From 7812715e86c1101049a48d3f0d2b89680a8705c4 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 15 Mar 2021 17:06:53 -0400 Subject: [PATCH] OCR Timeout --- commands/analyze/ocr.js | 11 ++++++++++- structures/cards-against-humanity/Game.js | 6 +++--- structures/cards-against-humanity/Player.js | 9 +++++---- structures/mafia/Game.js | 4 ++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/commands/analyze/ocr.js b/commands/analyze/ocr.js index 381ae72d..73e39364 100644 --- a/commands/analyze/ocr.js +++ b/commands/analyze/ocr.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { createWorker } = require('tesseract.js'); const { reactIfAble } = require('../../util/Util'); -const { LOADING_EMOJI_ID, SUCCESS_EMOJI_ID } = process.env; +const { LOADING_EMOJI_ID, SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env; module.exports = class OcrCommand extends Command { constructor(client) { @@ -32,7 +32,16 @@ module.exports = class OcrCommand extends Command { await worker.load(); await worker.loadLanguage('eng'); await worker.initialize('eng'); + let timedOut = false; + setTimeout(async () => { + timedOut = false; + await worker.terminate(); + }, 30000); const { data: { text } } = await worker.recognize(image); + if (timedOut) { + await reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌'); + return msg.reply('Scanning took longer than 30 seconds, so I\'ve given up.'); + } await worker.terminate(); await reactIfAble(msg, this.client.user, SUCCESS_EMOJI_ID, '✅'); if (!text) return msg.reply('There is no text in this image.'); diff --git a/structures/cards-against-humanity/Game.js b/structures/cards-against-humanity/Game.js index 7da127ca..e98fe5bc 100644 --- a/structures/cards-against-humanity/Game.js +++ b/structures/cards-against-humanity/Game.js @@ -1,7 +1,7 @@ const Collection = require('@discordjs/collection'); const Player = require('./Player'); const Deck = require('./Deck'); -const { removeFromArray, awaitPlayers } = require('../../util/Util'); +const { removeFromArray, awaitPlayers, reactIfAble } = require('../../util/Util'); const { SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env; module.exports = class Game { @@ -61,11 +61,11 @@ module.exports = class Game { if (this.players.has(res.author.id) && res.content.toLowerCase() !== 'leave game') return false; if (!this.players.has(res.author.id) && res.content.toLowerCase() !== 'join game') return false; if (this.czar.id === res.author.id || this.players.size >= 10) { - res.react(FAILURE_EMOJI_ID || '❌').catch(() => null); + reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌'); return false; } if (!['join game', 'leave game'].includes(res.content.toLowerCase())) return false; - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); + reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); return true; }); collector.on('collect', msg => { diff --git a/structures/cards-against-humanity/Player.js b/structures/cards-against-humanity/Player.js index c2cb1023..f16fe283 100644 --- a/structures/cards-against-humanity/Player.js +++ b/structures/cards-against-humanity/Player.js @@ -1,5 +1,6 @@ const { stripIndents } = require('common-tags'); const { escapeMarkdown } = require('discord.js'); +const { reactIfAble } = require('../../util/Util'); const { SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env; module.exports = class Player { @@ -57,7 +58,7 @@ module.exports = class Player { if (res.content.toLowerCase() === 'gamble' && this.points > 0 && !gambled) return true; const existing = hand[Number.parseInt(res.content, 10) - 1]; if (!existing || chosen.includes(existing)) { - res.react(FAILURE_EMOJI_ID || '❌').catch(() => null); + reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌'); return false; } return true; @@ -65,7 +66,7 @@ module.exports = class Player { collector.on('collect', async msg => { const existing = hand[Number.parseInt(msg.content, 10) - 1]; if (msg.content.toLowerCase() === 'swap') { - await msg.react(SUCCESS_EMOJI_ID || '✅'); + await reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); for (const card of this.hand) this.hand.delete(card); this.dealHand(); hand = Array.from(this.hand); @@ -74,12 +75,12 @@ module.exports = class Player { await this.sendHand(hand, black); return; } else if (msg.content.toLowerCase() === 'gamble') { - await msg.react(SUCCESS_EMOJI_ID || '✅'); + await reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); this.points--; gambled = true; return; } else if (existing) { - await msg.react(SUCCESS_EMOJI_ID || '✅'); + await reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); chosen.push(existing); } if (chosen.length >= black.pick * (gambled ? 2 : 1)) collector.stop(); diff --git a/structures/mafia/Game.js b/structures/mafia/Game.js index b3c7d124..dcfb11fa 100644 --- a/structures/mafia/Game.js +++ b/structures/mafia/Game.js @@ -2,7 +2,7 @@ const Collection = require('@discordjs/collection'); const path = require('path'); const { stripIndents } = require('common-tags'); const Player = require('./Player'); -const { shuffle } = require('../../util/Util'); +const { shuffle, reactIfAble } = require('../../util/Util'); const { SUCCESS_EMOJI_ID } = process.env; module.exports = class Game { @@ -67,7 +67,7 @@ module.exports = class Game { if (voted.includes(res.author.id)) return false; if (!playersArr[Number.parseInt(res.content, 10) - 1]) return false; voted.push(res.author.id); - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); + reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); return true; }; const votes = await this.channel.awaitMessages(filter, {