Skip to content

Commit

Permalink
handle when there are no results
Browse files Browse the repository at this point in the history
  • Loading branch information
M4ss1ck committed Oct 7, 2023
1 parent dc77668 commit 82d1294
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/middleware/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/middleware/anime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = `<b>${media.title.romaji ?? 'Title'}</b> (${media.id})\n<i>${escape(media.title.english ?? '')}</i>\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') + ' <i>(in ' + convertMsToRelativeTime(media.nextAiringEpisode.airingAt * 1000 - Date.now()) + ')</i> ' : '<i>no airing info available</i>'}\n\n<i>${media.description ? escape(media.description) : 'description n/a'}`
Expand All @@ -105,7 +108,7 @@ anime.action(/getAnime/, async (ctx) => {
: ctx.editMessageText(`${caption.slice(0, 4090)}</i>`, { 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)
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = `<a href="${character.siteUrl}">${character.name.full ?? 'Nombre'}</a> (${character.id})\nAge: ${character.age ?? 'n/a'} Gender: ${character.gender ?? 'n/a'}\n\n<i>${character.description ? escape(character.description) : 'description n/a'}`
Expand Down
1 change: 1 addition & 0 deletions src/middleware/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

Expand Down

0 comments on commit 82d1294

Please sign in to comment.