From 3c90676e30eaf9ce6c99559fa000ed8a9de516de Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 21 Feb 2018 23:58:35 +0000 Subject: [PATCH] Month names in zodiac-sign, Number.parseInt/isNaN --- assets/json/months.json | 14 ++++++++++++++ commands/events/days-until.js | 4 ++-- commands/games/akinator.js | 6 +++--- commands/games/apples-to-apples.js | 6 +++--- commands/games/cards-against-humanity.js | 6 +++--- commands/games/roulette.js | 4 ++-- commands/games/tic-tac-toe.js | 2 +- commands/games/wizard-convention.js | 9 +++++---- commands/search/my-anime-list-manga.js | 2 +- commands/search/periodic-table.js | 8 ++++---- commands/search/xkcd.js | 11 +++++++++-- commands/search/zodiac-sign.js | 16 +++++++++++++--- commands/text-edit/binary.js | 2 +- package.json | 2 +- 14 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 assets/json/months.json diff --git a/assets/json/months.json b/assets/json/months.json new file mode 100644 index 00000000..77912012 --- /dev/null +++ b/assets/json/months.json @@ -0,0 +1,14 @@ +[ + "january", + "february", + "march", + "april", + "may", + "june", + "july", + "august", + "september", + "october", + "november", + "december" +] diff --git a/commands/events/days-until.js b/commands/events/days-until.js index 7f532d76..d94db079 100644 --- a/commands/events/days-until.js +++ b/commands/events/days-until.js @@ -21,8 +21,8 @@ module.exports = class DaysUntilCommand extends Command { } run(msg, { date }) { - const month = parseInt(date[0], 10); - const day = parseInt(date[1], 10); + const month = Number.parseInt(date[0], 10); + const day = Number.parseInt(date[1], 10); if (!month || !day) return msg.reply('Invalid date.'); const now = new Date(); let year = now.getMonth() + 1 <= month ? now.getFullYear() : now.getFullYear() + 1; diff --git a/commands/games/akinator.js b/commands/games/akinator.js index 793c84c4..6e3a8a75 100644 --- a/commands/games/akinator.js +++ b/commands/games/akinator.js @@ -75,7 +75,7 @@ module.exports = class AkinatorCommand extends Command { id: data.identification.session, signature: data.identification.signature, step: 0, - progress: parseInt(data.step_information.progression, 10) + progress: Number.parseInt(data.step_information.progression, 10) }); return data.step_information; } @@ -95,8 +95,8 @@ module.exports = class AkinatorCommand extends Command { this.sessions.set(channel.id, { id: session.id, signature: session.signature, - step: parseInt(data.step, 10), - progress: parseInt(data.progression, 10) + step: Number.parseInt(data.step, 10), + progress: Number.parseInt(data.progression, 10) }); return data; } diff --git a/commands/games/apples-to-apples.js b/commands/games/apples-to-apples.js index 34f438df..6a17f8f2 100644 --- a/commands/games/apples-to-apples.js +++ b/commands/games/apples-to-apples.js @@ -73,7 +73,7 @@ module.exports = class ApplesToApplesCommand extends Command { `); let chosen = null; const filter = res => { - const existing = hand[parseInt(res.content, 10) - 1]; + const existing = hand[Number.parseInt(res.content, 10) - 1]; if (!existing) return false; chosen = existing; return true; @@ -111,7 +111,7 @@ module.exports = class ApplesToApplesCommand extends Command { `); const filter = res => { if (res.author.id !== czar.user.id) return false; - if (!cards[parseInt(res.content, 10) - 1]) return false; + if (!cards[Number.parseInt(res.content, 10) - 1]) return false; return true; }; const chosen = await msg.channel.awaitMessages(filter, { @@ -122,7 +122,7 @@ module.exports = class ApplesToApplesCommand extends Command { await msg.say('Hmm... No one wins.'); continue; } - const player = players.get(cards[parseInt(chosen.first().content, 10) - 1].id); + const player = players.get(cards[Number.parseInt(chosen.first().content, 10) - 1].id); ++player.points; if (player.points >= maxPts) winner = player.user; else await msg.say(`Nice one, ${player.user}! You now have **${player.points}** points!`); diff --git a/commands/games/cards-against-humanity.js b/commands/games/cards-against-humanity.js index 087dbaa3..56074306 100644 --- a/commands/games/cards-against-humanity.js +++ b/commands/games/cards-against-humanity.js @@ -74,7 +74,7 @@ module.exports = class CardsAgainstHumanityCommand extends Command { `); const chosen = []; const filter = res => { - const existing = hand[parseInt(res.content, 10) - 1]; + const existing = hand[Number.parseInt(res.content, 10) - 1]; if (!existing) return false; if (chosen.includes(existing)) return false; chosen.push(existing); @@ -112,7 +112,7 @@ module.exports = class CardsAgainstHumanityCommand extends Command { `); const filter = res => { if (res.author.id !== czar.user.id) return false; - if (!cards[parseInt(res.content, 10) - 1]) return false; + if (!cards[Number.parseInt(res.content, 10) - 1]) return false; return true; }; const chosen = await msg.channel.awaitMessages(filter, { @@ -123,7 +123,7 @@ module.exports = class CardsAgainstHumanityCommand extends Command { await msg.say('Hmm... No one wins.'); continue; } - const player = players.get(cards[parseInt(chosen.first().content, 10) - 1].id); + const player = players.get(cards[Number.parseInt(chosen.first().content, 10) - 1].id); ++player.points; if (player.points >= maxPts) winner = player.user; else await msg.say(`Nice one, ${player.user}! You now have **${player.points}** points!`); diff --git a/commands/games/roulette.js b/commands/games/roulette.js index c4b0c3c8..5d9980d4 100644 --- a/commands/games/roulette.js +++ b/commands/games/roulette.js @@ -22,7 +22,7 @@ module.exports = class RouletteCommand extends Command { prompt: 'What space do you want to bet on?', type: 'string', validate: space => { - if (numbers.includes(parseInt(space, 10))) return true; + if (numbers.includes(Number.parseInt(space, 10))) return true; if (dozens.includes(space)) return true; if (halves.includes(space)) return true; if (columns.includes(space.toLowerCase())) return true; @@ -57,7 +57,7 @@ module.exports = class RouletteCommand extends Command { } if (parity.includes(choice)) return parity[result % 2] === choice; if (columns.includes(choice)) return columns[(result - 1) % 3] === choice; - const number = parseInt(choice, 10); + const number = Number.parseInt(choice, 10); if (numbers.includes(number)) return result === number; if (!result) return false; return false; diff --git a/commands/games/tic-tac-toe.js b/commands/games/tic-tac-toe.js index 77fa5e13..c3d5bdab 100644 --- a/commands/games/tic-tac-toe.js +++ b/commands/games/tic-tac-toe.js @@ -67,7 +67,7 @@ module.exports = class TicTacToeCommand extends Command { } else if (!sides.includes(choice)) { await msg.say('I don\'t think that is a valid spot...'); } else { - sides[parseInt(choice, 10)] = sign; + sides[Number.parseInt(choice, 10)] = sign; taken.push(choice); if ((sides[0] === sides[1] && sides[0] === sides[2]) || (sides[0] === sides[3] && sides[0] === sides[6]) diff --git a/commands/games/wizard-convention.js b/commands/games/wizard-convention.js index 8517fb10..a89d4d4d 100644 --- a/commands/games/wizard-convention.js +++ b/commands/games/wizard-convention.js @@ -42,7 +42,8 @@ module.exports = class WizardConventionCommand extends Command { ${questions[player.role]} Please type the number. ${valid.map((p, i) => `**${i + 1}.** ${p.user.tag}`).join('\n')} `); - const decision = await player.user.dmChannel.awaitMessages(res => valid[parseInt(res.content, 10) - 1], { + const filter = res => valid[Number.parseInt(res.content, 10) - 1]; + const decision = await player.user.dmChannel.awaitMessages(filter, { max: 1, time: 120000 }); @@ -50,7 +51,7 @@ module.exports = class WizardConventionCommand extends Command { await player.user.send('Sorry, time is up!'); continue; } - const choice = parseInt(decision.first().content, 10); + const choice = Number.parseInt(decision.first().content, 10); if (player.role === 'dragon') { const chosen = players.get(choice); eaten = chosen.id; @@ -99,7 +100,7 @@ module.exports = class WizardConventionCommand extends Command { const filter = res => { if (!players.exists(p => p.user.id === res.author.id)) return false; if (voted.includes(res.author.id)) return false; - if (!playersArr[parseInt(res.content, 10) - 1]) return false; + if (!playersArr[Number.parseInt(res.content, 10) - 1]) return false; voted.push(res.author.id); return true; }; @@ -146,7 +147,7 @@ module.exports = class WizardConventionCommand extends Command { getExpelled(votes, players, playersArr) { const counts = new Collection(); for (const vote of votes.values()) { - const player = players.get(playersArr[parseInt(vote.content, 10) - 1].id); + const player = players.get(playersArr[Number.parseInt(vote.content, 10) - 1].id); if (counts.has(player.id)) { ++counts.get(player.id).votes; } else { diff --git a/commands/search/my-anime-list-manga.js b/commands/search/my-anime-list-manga.js index ca62a558..7cb80fc8 100644 --- a/commands/search/my-anime-list-manga.js +++ b/commands/search/my-anime-list-manga.js @@ -43,7 +43,7 @@ module.exports = class MyAnimeListMangaCommand extends Command { .addField('❯ Type', `${data.type[0]} - ${data.status[0]}`, true) .addField('❯ Volumes / Chapters', - `${parseInt(data.volumes[0], 10) || '???'} / ${parseInt(data.chapters[0], 10) || '???'}`, true) + `${Number.parseInt(data.volumes[0], 10) || '???'} / ${Number.parseInt(data.chapters[0], 10) || '???'}`, true) .addField('❯ Start Date', data.start_date[0] !== '0000-00-00' ? data.start_date[0] : '???', true) .addField('❯ End Date', diff --git a/commands/search/periodic-table.js b/commands/search/periodic-table.js index 559de8c0..994e231e 100644 --- a/commands/search/periodic-table.js +++ b/commands/search/periodic-table.js @@ -23,15 +23,15 @@ module.exports = class PeriodicTableCommand extends Command { prompt: 'What element do you want to find? You can enter the name, symbol, or atomic number.', type: 'string', validate: element => { - const num = parseInt(element, 10); - if (!isNaN(num) && num >= 0 && num <= elements.length - 1) return true; + const num = Number.parseInt(element, 10); + if (!Number.isNaN(num) && num >= 0 && num <= elements.length - 1) return true; const search = element.toString().toLowerCase(); if (elements.find(e => e.name.toLowerCase() === search || e.symbol.toLowerCase() === search)) return true; return 'Invalid element, please enter a valid element symbol, name, or atomic number.'; }, parse: element => { - const num = parseInt(element, 10); - if (!isNaN(num)) return elements[num]; + const num = Number.parseInt(element, 10); + if (!Number.isNaN(num)) return elements[num]; const search = element.toLowerCase(); return elements.find(e => e.name.toLowerCase() === search || e.symbol.toLowerCase() === search); } diff --git a/commands/search/xkcd.js b/commands/search/xkcd.js index 28f20ca6..791dc2e6 100644 --- a/commands/search/xkcd.js +++ b/commands/search/xkcd.js @@ -1,6 +1,7 @@ const { Command } = require('discord.js-commando'); const { MessageEmbed } = require('discord.js'); const snekfetch = require('snekfetch'); +const types = ['random', 'today']; module.exports = class XKCDCommand extends Command { constructor(client) { @@ -17,6 +18,12 @@ module.exports = class XKCDCommand extends Command { prompt: 'Please enter either a specific comic number, today, or random.', type: 'string', default: 'today', + validate: query => { + if (types.includes(query.toLowerCase())) return true; + const num = Number.parseInt(query, 10); + if (!Number.isNaN(num) && num > 1) return true; + return `Invalid query, please enter either today, random, or a specific comic number.`; + }, parse: query => query.toLowerCase() } ] @@ -46,8 +53,8 @@ module.exports = class XKCDCommand extends Command { .setFooter(body.alt); return msg.embed(embed); } - const choice = parseInt(query, 10); - if (isNaN(choice) || current.body.num < choice || choice < 1) return msg.say('Could not find any results.'); + const choice = Number.parseInt(query, 10); + if (current.body.num < choice) return msg.say('Could not find any results.'); const { body } = await snekfetch.get(`https://xkcd.com/${choice}/info.0.json`); const embed = new MessageEmbed() .setTitle(`${body.num} - ${body.title}`) diff --git a/commands/search/zodiac-sign.js b/commands/search/zodiac-sign.js index 0e92a753..9e023568 100644 --- a/commands/search/zodiac-sign.js +++ b/commands/search/zodiac-sign.js @@ -1,4 +1,5 @@ const { Command } = require('discord.js-commando'); +const months = require('../../assets/json/months'); module.exports = class ZodiacSignCommand extends Command { constructor(client) { @@ -11,9 +12,18 @@ module.exports = class ZodiacSignCommand extends Command { { key: 'month', prompt: 'What month would you like to get the Zodiac Sign for?', - type: 'integer', - min: 1, - max: 12 + type: 'string', + validate: month => { + const num = Number.parseInt(month, 10); + if (num > 0 && num < 13) return true; + if (months.includes(month.toLowerCase())) return true; + return 'Please enter a valid month name or number.'; + }, + parse: month => { + const num = Number.parseInt(month, 10); + if (!Number.isNaN(num)) return num; + return months.indexOf(month.toLowerCase()) + 1; + } }, { key: 'day', diff --git a/commands/text-edit/binary.js b/commands/text-edit/binary.js index 6ba97084..9d8056e3 100644 --- a/commands/text-edit/binary.js +++ b/commands/text-edit/binary.js @@ -47,6 +47,6 @@ module.exports = class BinaryCommand extends Command { } unbinary(text) { - return text.split(' ').map(str => String.fromCharCode(parseInt(str, 2))).join(''); + return text.split(' ').map(str => String.fromCharCode(Number.parseInt(str, 2))).join(''); } }; diff --git a/package.json b/package.json index c2f0e3e9..3e4909b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "65.4.0", + "version": "65.4.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {