mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 14:19:56 +02:00
Begin migrating away from stupid "Oh no, an error occurred!"
This commit is contained in:
@@ -41,21 +41,17 @@ module.exports = class AnimeAiringCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg) {
|
||||
try {
|
||||
const anime = await this.getList();
|
||||
if (!anime) return msg.say('No anime air today...');
|
||||
const mapped = anime.sort((a, b) => a.airingAt - b.airingAt).map(ani => {
|
||||
const title = ani.media.title.english || ani.media.title.romaji;
|
||||
const airingAt = moment(ani.airingAt * 1000).tz('Asia/Tokyo').format('h:mm A');
|
||||
return `• ${title} (@${airingAt} JST)`;
|
||||
});
|
||||
return msg.say(stripIndents`
|
||||
**Anime Airing on ${moment().tz('Asia/Tokyo').format('dddd, MMMM Do, YYYY')}**
|
||||
${mapped.join('\n')}
|
||||
`);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
const anime = await this.getList();
|
||||
if (!anime) return msg.say('No anime air today...');
|
||||
const mapped = anime.sort((a, b) => a.airingAt - b.airingAt).map(ani => {
|
||||
const title = ani.media.title.english || ani.media.title.romaji;
|
||||
const airingAt = moment(ani.airingAt * 1000).tz('Asia/Tokyo').format('h:mm A');
|
||||
return `• ${title} (@${airingAt} JST)`;
|
||||
});
|
||||
return msg.say(stripIndents`
|
||||
**Anime Airing on ${moment().tz('Asia/Tokyo').format('dddd, MMMM Do, YYYY')}**
|
||||
${mapped.join('\n')}
|
||||
`);
|
||||
}
|
||||
|
||||
async getList() {
|
||||
|
||||
+17
-21
@@ -25,26 +25,22 @@ module.exports = class ApodCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg) {
|
||||
try {
|
||||
const { body } = await request
|
||||
.get('https://api.nasa.gov/planetary/apod')
|
||||
.query({ api_key: GOV_KEY });
|
||||
const embed = new MessageEmbed()
|
||||
.setTitle(body.title)
|
||||
.setDescription(shorten(body.explanation))
|
||||
.setColor(0x2E528E)
|
||||
.setAuthor(
|
||||
'Astronomy Picture of the Day',
|
||||
'https://i.imgur.com/Wh8jY9c.png',
|
||||
'https://apod.nasa.gov/apod/astropix.html'
|
||||
)
|
||||
.setImage(body.media_type === 'image' ? body.url : null)
|
||||
.setURL(body.url)
|
||||
.setFooter(`Image Credits: ${body.copyright ? body.copyright.replaceAll('\n', '/') : 'Public Domain'}`)
|
||||
.setTimestamp();
|
||||
return msg.embed(embed);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
const { body } = await request
|
||||
.get('https://api.nasa.gov/planetary/apod')
|
||||
.query({ api_key: GOV_KEY });
|
||||
const embed = new MessageEmbed()
|
||||
.setTitle(body.title)
|
||||
.setDescription(shorten(body.explanation))
|
||||
.setColor(0x2E528E)
|
||||
.setAuthor(
|
||||
'Astronomy Picture of the Day',
|
||||
'https://i.imgur.com/Wh8jY9c.png',
|
||||
'https://apod.nasa.gov/apod/astropix.html'
|
||||
)
|
||||
.setImage(body.media_type === 'image' ? body.url : null)
|
||||
.setURL(body.url)
|
||||
.setFooter(`Image Credits: ${body.copyright ? body.copyright.replaceAll('\n', '/') : 'Public Domain'}`)
|
||||
.setTimestamp();
|
||||
return msg.embed(embed);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -40,14 +40,10 @@ module.exports = class GoogleDoodleCommand extends Command {
|
||||
const now = new Date();
|
||||
if (latest) month = now.getMonth() + 1;
|
||||
if (!year) year = now.getFullYear();
|
||||
try {
|
||||
const { body } = await request.get(`https://www.google.com/doodles/json/${year}/${month}`);
|
||||
if (!body.length) return msg.say('Could not find any results.');
|
||||
const data = body[latest ? 0 : Math.floor(Math.random() * body.length)];
|
||||
const runDate = moment.utc(data.run_date_array.join('-')).format('MMMM Do, YYYY');
|
||||
return msg.say(`${runDate}: ${data.share_text}`, { files: [`https:${data.url}`] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
const { body } = await request.get(`https://www.google.com/doodles/json/${year}/${month}`);
|
||||
if (!body.length) return msg.say('Could not find any results.');
|
||||
const data = body[latest ? 0 : Math.floor(Math.random() * body.length)];
|
||||
const runDate = moment.utc(data.run_date_array.join('-')).format('MMMM Do, YYYY');
|
||||
return msg.say(`${runDate}: ${data.share_text}`, { files: [`https:${data.url}`] });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -35,20 +35,16 @@ module.exports = class HoroscopeCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg, { sign }) {
|
||||
try {
|
||||
const horoscope = await this.fetchHoroscope(sign);
|
||||
const embed = new MessageEmbed()
|
||||
.setColor(0x9797FF)
|
||||
.setTitle(`Horoscope for ${firstUpperCase(sign)}...`)
|
||||
.setURL(`https://astrology.tv/horoscope/signs/${sign}/`)
|
||||
.setFooter('© Kelli Fox, The Astrologer')
|
||||
.setThumbnail(this.getImageURL(sign))
|
||||
.setTimestamp()
|
||||
.setDescription(horoscope);
|
||||
return msg.embed(embed);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
const horoscope = await this.fetchHoroscope(sign);
|
||||
const embed = new MessageEmbed()
|
||||
.setColor(0x9797FF)
|
||||
.setTitle(`Horoscope for ${firstUpperCase(sign)}...`)
|
||||
.setURL(`https://astrology.tv/horoscope/signs/${sign}/`)
|
||||
.setFooter('© Kelli Fox, The Astrologer')
|
||||
.setThumbnail(this.getImageURL(sign))
|
||||
.setTimestamp()
|
||||
.setDescription(horoscope);
|
||||
return msg.embed(embed);
|
||||
}
|
||||
|
||||
async fetchHoroscope(sign) {
|
||||
|
||||
@@ -43,17 +43,13 @@ module.exports = class NekoAtsumePasswordCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg, { locale }) {
|
||||
try {
|
||||
const data = await this.fetchPassword(locale);
|
||||
return msg.say(stripIndents`
|
||||
The current Neko Atsume password is **${data.password}**.
|
||||
It will expire in **${moment.duration(data.expires - data.date).format('hh:mm:ss', { trim: false })}**.
|
||||
const data = await this.fetchPassword(locale);
|
||||
return msg.say(stripIndents`
|
||||
The current Neko Atsume password is **${data.password}**.
|
||||
It will expire in **${moment.duration(data.expires - data.date).format('hh:mm:ss', { trim: false })}**.
|
||||
|
||||
${data.gold} ${this.goldFishEmoji} ${data.silver} ${this.silverFishEmoji}
|
||||
`);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
${data.gold} ${this.goldFishEmoji} ${data.silver} ${this.silverFishEmoji}
|
||||
`);
|
||||
}
|
||||
|
||||
async fetchPassword(locale) {
|
||||
|
||||
@@ -56,7 +56,7 @@ module.exports = class TodayInHistoryCommand extends Command {
|
||||
return msg.embed(embed);
|
||||
} catch (err) {
|
||||
if (err.status === 404 || err.status === 500) return msg.say('Invalid date.');
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -27,20 +27,16 @@ module.exports = class UsElectionCommand extends Command {
|
||||
if (year !== currentYear) {
|
||||
return msg.reply(`This command has not been updated to reflect the ${currentYear} election season.`);
|
||||
}
|
||||
try {
|
||||
const canidates = await this.getList();
|
||||
const list = canidates.map(
|
||||
canidate => `**${canidate.name}:** ${canidate.score} (${canidate.percentChange} in last day)`
|
||||
);
|
||||
return msg.say(stripIndents`
|
||||
__**Chances of Winning the ${year} US Election:**__
|
||||
${list.join('\n')}
|
||||
const canidates = await this.getList();
|
||||
const list = canidates.map(
|
||||
canidate => `**${canidate.name}:** ${canidate.score} (${canidate.percentChange} in last day)`
|
||||
);
|
||||
return msg.say(stripIndents`
|
||||
__**Chances of Winning the ${year} US Election:**__
|
||||
${list.join('\n')}
|
||||
|
||||
_More detailed information is available at <https://electionbettingodds.com/>._
|
||||
`);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
_More detailed information is available at <https://electionbettingodds.com/>._
|
||||
`);
|
||||
}
|
||||
|
||||
async getList() {
|
||||
|
||||
@@ -26,25 +26,21 @@ module.exports = class WordOfTheDayCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg) {
|
||||
try {
|
||||
const word = await this.fetchWordOfTheDay();
|
||||
let data;
|
||||
if (this.cache?.word === word) {
|
||||
data = this.cache.data;
|
||||
} else {
|
||||
const { body } = await request
|
||||
.get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`)
|
||||
.query({ key: WEBSTER_KEY });
|
||||
data = body[0];
|
||||
this.cache = { word, data };
|
||||
}
|
||||
return msg.say(stripIndents`
|
||||
**${data.meta.stems[0]}** (${data.fl})
|
||||
${data.shortdef.map((definition, i) => `(${i + 1}) ${definition}`).join('\n')}
|
||||
`);
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
const word = await this.fetchWordOfTheDay();
|
||||
let data;
|
||||
if (this.cache?.word === word) {
|
||||
data = this.cache.data;
|
||||
} else {
|
||||
const { body } = await request
|
||||
.get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`)
|
||||
.query({ key: WEBSTER_KEY });
|
||||
data = body[0];
|
||||
this.cache = { word, data };
|
||||
}
|
||||
return msg.say(stripIndents`
|
||||
**${data.meta.stems[0]}** (${data.fl})
|
||||
${data.shortdef.map((definition, i) => `(${i + 1}) ${definition}`).join('\n')}
|
||||
`);
|
||||
}
|
||||
|
||||
async fetchWordOfTheDay() {
|
||||
|
||||
Reference in New Issue
Block a user