diff --git a/commands/search/movie.js b/commands/search/movie.js index f4aee8ed..69eb5f13 100644 --- a/commands/search/movie.js +++ b/commands/search/movie.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); -const { shorten } = require('../../util/Util'); +const { shorten, pickWhenMany } = require('../../util/Util'); const { TMDB_KEY } = process.env; module.exports = class MovieCommand extends Command { @@ -41,9 +41,12 @@ module.exports = class MovieCommand extends Command { query }); if (!search.body.results.length) return msg.say('Could not find any results.'); - const find = search.body.results.find( - m => m.title.toLowerCase() === query.toLowerCase() - ) || search.body.results[0]; + let find = search.body.results.find(m => m.title.toLowerCase() === query.toLowerCase()) + || search.body.results[0]; + if (search.body.results > 1) { + const resultListFunc = (movie, i) => `**${i + 1}.** ${movie.title} (${movie.release_date || 'TBA'})`; + find = await pickWhenMany(msg, search.body.results, find, resultListFunc); + } const { body } = await request .get(`https://api.themoviedb.org/3/movie/${find.id}`) .query({ api_key: TMDB_KEY }); diff --git a/commands/search/tv-show.js b/commands/search/tv-show.js index 69a7caf0..f5823439 100644 --- a/commands/search/tv-show.js +++ b/commands/search/tv-show.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { MessageEmbed } = require('discord.js'); const request = require('node-superfetch'); -const { shorten, formatNumber } = require('../../util/Util'); +const { shorten, formatNumber, pickWhenMany } = require('../../util/Util'); const { TMDB_KEY } = process.env; module.exports = class TvShowCommand extends Command { @@ -41,9 +41,12 @@ module.exports = class TvShowCommand extends Command { query }); if (!search.body.results.length) return msg.say('Could not find any results.'); - const find = search.body.results.find( - m => m.name.toLowerCase() === query.toLowerCase() - ) || search.body.results[0]; + let find = search.body.results.find(m => m.name.toLowerCase() === query.toLowerCase()) + || search.body.results[0]; + if (search.body.results > 1) { + const resultListFunc = (show, i) => `**${i + 1}.** ${show.name} (${show.first_air_date || 'TBA'})`; + find = await pickWhenMany(msg, search.body.results, find, resultListFunc); + } const { body } = await request .get(`https://api.themoviedb.org/3/tv/${find.id}`) .query({ api_key: TMDB_KEY }); diff --git a/package.json b/package.json index 0b30bb74..dd18cd37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.34.7", + "version": "119.34.8", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index aa16f434..c515b18f 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,6 +1,7 @@ const crypto = require('crypto'); const Entities = require('html-entities').AllHtmlEntities; const entities = new Entities(); +const { stripIndents } = require('common-tags'); const { SUCCESS_EMOJI_ID } = process.env; const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea', 'ya', 'hai', 'si', 'sí', 'oui', 'はい', 'correct']; const no = ['no', 'n', 'nah', 'nope', 'nop', 'iie', 'いいえ', 'non', 'fuck off']; @@ -210,6 +211,23 @@ module.exports = class Util { return false; } + static async pickWhenMany(msg, arr, defalt, arrListFunc, { time = 30000 }) { + const resultsList = arr.map(arrListFunc); + await msg.reply(stripIndents` + __**Found ${arr.length} results, which would you like to view?**__ + ${resultsList.join('\n')} + `); + const filter = res => { + if (res.author.id !== msg.author.id) return false; + const num = Number.parseInt(res.content, 10); + if (!num) return false; + return num > 0 && num <= arr.length; + }; + const msgs = await msg.channel.awaitMessages(filter, { max: 1, time }); + if (!msgs.size) return defalt; + return arr[Number.parseInt(msgs.first().content, 10) - 1]; + } + static async awaitPlayers(msg, max, min = 1) { if (max === 1) return [msg.author.id]; const addS = min - 1 === 1 ? '' : 's';