From 82d12942abd7bcffde39b97fa340b39d8e335ccb Mon Sep 17 00:00:00 2001
From: M4ss1ck <34517932+M4ss1ck@users.noreply.github.com>
Date: Fri, 6 Oct 2023 22:28:38 -0400
Subject: [PATCH] handle when there are no results
---
src/middleware/actions.ts | 1 +
src/middleware/anime.ts | 9 ++++++++-
src/middleware/scheduler.ts | 1 +
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/middleware/actions.ts b/src/middleware/actions.ts
index 8f25bd1..c45f1b8 100644
--- a/src/middleware/actions.ts
+++ b/src/middleware/actions.ts
@@ -373,6 +373,7 @@ actions.action(/afm_\d+_\d+_\d+_\d+/i, async ctx => {
}
const results = await getAnime(parseInt(animeId))
+ if (!results) await ctx.answerCbQuery('Anime not found').catch(e => logger.error(e))
const anime = results.Media
const englishTitle = anime.title.english ?? 'English title not found!'
await prisma.anime
diff --git a/src/middleware/anime.ts b/src/middleware/anime.ts
index ce06765..5ff03d9 100644
--- a/src/middleware/anime.ts
+++ b/src/middleware/anime.ts
@@ -13,6 +13,7 @@ anime.command('anime', async (ctx) => {
// buscar en AniList
try {
const results = await getAnimes(search)
+ if (!results) await ctx.replyWithHTML('Error. No anime found.')
const media = results.Page?.media
const total = results.Page?.pageInfo?.total as number ?? 1
const perPage = results.Page?.pageInfo?.perPage as number ?? 5
@@ -48,6 +49,7 @@ anime.action(/AnimPage\d+-/i, async (ctx) => {
// buscar en AniList
try {
const results = await getAnimes(search, page)
+ if (!results) logger.error('No results for ' + search + ' in page ' + page)
const media = results.Page?.media
const total = results.Page?.pageInfo?.total as number ?? 1
const perPage = results.Page?.pageInfo?.perPage as number ?? 5
@@ -82,6 +84,7 @@ anime.action(/getAnime/, async (ctx) => {
// buscar en AniList
try {
const results = await getAnime(animeId)
+ if (!results) await ctx.replyWithHTML('Error. No anime found.').catch(logger.error)
const media = results.Media
if (media) {
const caption = `${media.title.romaji ?? 'Title'} (${media.id})\n${escape(media.title.english ?? '')}\nGenres: ${media.genres ? media.genres.join(', ') : 'n/a'}\nHashtag: ${media.hashtag ?? 'n/a'}\nYear: ${media.seasonYear ?? 'n/a'} Episodes: ${media.episodes ?? 'n/a'}\n${media.nextAiringEpisode ? 'Next airing episode: ' + new Date(Math.floor(media.nextAiringEpisode.airingAt * 1000)).toLocaleString('en-US') + ' (in ' + convertMsToRelativeTime(media.nextAiringEpisode.airingAt * 1000 - Date.now()) + ') ' : 'no airing info available'}\n\n${media.description ? escape(media.description) : 'description n/a'}`
@@ -105,7 +108,7 @@ anime.action(/getAnime/, async (ctx) => {
: ctx.editMessageText(`${caption.slice(0, 4090)}`, { parse_mode: "HTML" }).catch(() => ctx.reply('Parsing error. Contact bot owner.'))
}
else {
- ctx.replyWithHTML('Error. No anime found.')
+ ctx.replyWithHTML('Error. No anime found.').catch(logger.error)
}
} catch (error) {
logger.error(error)
@@ -116,6 +119,7 @@ anime.action(/getAnime/, async (ctx) => {
anime.command('animebd', async (ctx) => {
try {
const results = await getIsBirthdayCharacters()
+ if (!results) await ctx.replyWithHTML('Error. No character found.')
const characters = results.Page?.characters
if (characters && characters.length > 0) {
@@ -138,6 +142,7 @@ anime.command('character', async (ctx) => {
if (search.length > 2) {
try {
const results = await getCharacters(search)
+ if (!results) await ctx.replyWithHTML('Error. No character found.')
const characters = results.Page?.characters
const total = results.Page?.pageInfo?.total as number ?? 1
const perPage = results.Page?.pageInfo?.perPage as number ?? 5
@@ -169,6 +174,7 @@ anime.action(/CharPage\d+-/i, async (ctx) => {
if (search && search.length > 2) {
try {
const results = await getCharacters(search, page)
+ if (!results) logger.error('Error in CharPage for ' + search)
const characters = results.Page?.characters
const total = results.Page?.pageInfo?.total as number ?? 1
const perPage = results.Page?.pageInfo?.perPage as number ?? 5
@@ -203,6 +209,7 @@ anime.action(/getCharacter/, async (ctx) => {
// buscar en AniList
try {
const results = await getCharacter(characterId)
+ if (!results) await ctx.replyWithHTML('Error. No character found.').catch(logger.error)
const character = results.Character
if (character) {
const caption = `${character.name.full ?? 'Nombre'} (${character.id})\nAge: ${character.age ?? 'n/a'} Gender: ${character.gender ?? 'n/a'}\n\n${character.description ? escape(character.description) : 'description n/a'}`
diff --git a/src/middleware/scheduler.ts b/src/middleware/scheduler.ts
index 7b70bbe..d84f79a 100644
--- a/src/middleware/scheduler.ts
+++ b/src/middleware/scheduler.ts
@@ -95,6 +95,7 @@ scheduler.action(/a_scheduler:/i, async ctx => {
await ctx.answerCbQuery().catch(e => logger.error(e))
const anime = await getAnime(Number(animeId))
+ if (!anime) await ctx.answerCbQuery('No anime found!').catch(logger.error)
const jobId = `${animeId}:${date}:${userId}`