Choose from movies and tv shows when more than 1 result

This commit is contained in:
Dragon Fire
2020-10-30 12:01:21 -04:00
parent 7739a6218f
commit 2409d97e83
4 changed files with 33 additions and 9 deletions
+7 -4
View File
@@ -1,7 +1,7 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const request = require('node-superfetch'); const request = require('node-superfetch');
const { shorten } = require('../../util/Util'); const { shorten, pickWhenMany } = require('../../util/Util');
const { TMDB_KEY } = process.env; const { TMDB_KEY } = process.env;
module.exports = class MovieCommand extends Command { module.exports = class MovieCommand extends Command {
@@ -41,9 +41,12 @@ module.exports = class MovieCommand extends Command {
query query
}); });
if (!search.body.results.length) return msg.say('Could not find any results.'); if (!search.body.results.length) return msg.say('Could not find any results.');
const find = search.body.results.find( let find = search.body.results.find(m => m.title.toLowerCase() === query.toLowerCase())
m => m.title.toLowerCase() === query.toLowerCase() || search.body.results[0];
) || 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 const { body } = await request
.get(`https://api.themoviedb.org/3/movie/${find.id}`) .get(`https://api.themoviedb.org/3/movie/${find.id}`)
.query({ api_key: TMDB_KEY }); .query({ api_key: TMDB_KEY });
+7 -4
View File
@@ -1,7 +1,7 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const request = require('node-superfetch'); const request = require('node-superfetch');
const { shorten, formatNumber } = require('../../util/Util'); const { shorten, formatNumber, pickWhenMany } = require('../../util/Util');
const { TMDB_KEY } = process.env; const { TMDB_KEY } = process.env;
module.exports = class TvShowCommand extends Command { module.exports = class TvShowCommand extends Command {
@@ -41,9 +41,12 @@ module.exports = class TvShowCommand extends Command {
query query
}); });
if (!search.body.results.length) return msg.say('Could not find any results.'); if (!search.body.results.length) return msg.say('Could not find any results.');
const find = search.body.results.find( let find = search.body.results.find(m => m.name.toLowerCase() === query.toLowerCase())
m => m.name.toLowerCase() === query.toLowerCase() || search.body.results[0];
) || 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 const { body } = await request
.get(`https://api.themoviedb.org/3/tv/${find.id}`) .get(`https://api.themoviedb.org/3/tv/${find.id}`)
.query({ api_key: TMDB_KEY }); .query({ api_key: TMDB_KEY });
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "119.34.7", "version": "119.34.8",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {
+18
View File
@@ -1,6 +1,7 @@
const crypto = require('crypto'); const crypto = require('crypto');
const Entities = require('html-entities').AllHtmlEntities; const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities(); const entities = new Entities();
const { stripIndents } = require('common-tags');
const { SUCCESS_EMOJI_ID } = process.env; const { SUCCESS_EMOJI_ID } = process.env;
const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea', 'ya', 'hai', 'si', 'sí', 'oui', 'はい', 'correct']; 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']; const no = ['no', 'n', 'nah', 'nope', 'nop', 'iie', 'いいえ', 'non', 'fuck off'];
@@ -210,6 +211,23 @@ module.exports = class Util {
return false; 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) { static async awaitPlayers(msg, max, min = 1) {
if (max === 1) return [msg.author.id]; if (max === 1) return [msg.author.id];
const addS = min - 1 === 1 ? '' : 's'; const addS = min - 1 === 1 ? '' : 's';