Skip to content

Commit e656ef5

Browse files
committed
refactor(weebism.anime): DRY out anime info and anime synopsis commands
1 parent c746b57 commit e656ef5

File tree

1 file changed

+61
-112
lines changed

1 file changed

+61
-112
lines changed

takina/cogs/weebism/anime.py

Lines changed: 61 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ async def fetch_anime(self, anime_name: str):
2828
except Exception as e:
2929
raise e
3030

31-
@commands.command(
32-
name="anime",
33-
aliases=["ani"],
34-
help="Fetch anime information from MyAnimeList. \nUsage: `anime Lycoris Recoil` or `anime 50709`.",
35-
)
36-
async def base_anime(self, ctx: commands.Context, *, anime_name: str):
31+
async def build_anime_embed(self, anime_name):
32+
embed = nextcord.Embed(color=EMBED_COLOR)
33+
is_error_embed = False
3734
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
3835
try:
3936
anime = await self.fetch_anime(anime_name)
@@ -69,115 +66,94 @@ async def base_anime(self, ctx: commands.Context, *, anime_name: str):
6966
embed.description += f"\n> **Rating**: {rating}"
7067
embed.set_thumbnail(url=cover_image)
7168
embed.set_footer(text=str(mal_id))
69+
return embed, is_error_embed
7270

7371
else:
74-
embed = nextcord.Embed(
75-
description=":x: Anime not found.",
76-
color=ERROR_COLOR,
77-
)
72+
embed.description = ":x: Anime not found."
73+
embed.color = ERROR_COLOR
74+
is_error_embed = True
75+
return embed, is_error_embed
7876

7977
except Exception as e:
8078
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
81-
await ctx.reply(embed=embed, mention_author=False)
82-
83-
@nextcord.slash_command(
84-
name="anime", description="MyAnimeList anime information commands."
85-
)
86-
async def anime(
87-
self,
88-
interaction: nextcord.Interaction,
89-
):
90-
pass
79+
is_error_embed = True
80+
return embed, is_error_embed
9181

92-
@anime.subcommand(
93-
name="info", description="Fetch anime information from MyAnimeList."
94-
)
95-
async def slash_anime_info(
96-
self,
97-
interaction: Interaction,
98-
anime_name: str = SlashOption(description="Name of the anime"),
99-
):
100-
await interaction.response.defer()
82+
async def build_anisyn_embed(self, anime_name):
83+
embed = nextcord.Embed(color=EMBED_COLOR)
84+
is_error_embed = False
10185
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
10286
try:
10387
anime = await self.fetch_anime(anime_name)
10488
if anime:
10589
title = anime.get("title")
106-
episodes = anime.get("episodes")
107-
score = anime.get("score")
10890
synopsis = anime.get("synopsis")
109-
source = anime.get("source")
91+
if len(synopsis) > 700:
92+
synopsis = synopsis[:700] + "..."
11093
english_title = anime.get("title_english")
111-
aired = anime.get("aired", {}).get("string")
112-
type = anime.get("type")
11394
cover_image = anime["images"]["jpg"]["image_url"]
11495
url = anime.get("url")
115-
rating = anime.get("rating")
11696
mal_id = anime.get("mal_id")
117-
genres = ", ".join([genre["name"] for genre in anime.get("genres", [])])
118-
studios = ", ".join(
119-
[studio["name"] for studio in anime.get("studios", [])]
120-
)
12197

12298
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
12399
embed.description = ""
124100
if english_title and english_title != title:
125101
embed.description += f"-# {english_title}\n"
126-
embed.description += f"\n> **Type**: {type}"
127-
embed.description += f"\n> **Episodes**: {episodes}"
128-
embed.description += f"\n> **Score**: {score}"
129-
embed.description += f"\n> **Source**: {source}"
130-
embed.description += f"\n> **Aired**: {aired}"
131-
embed.description += f"\n> **Genres**: {genres}"
132-
embed.description += f"\n> **Studios**: {studios}"
133-
embed.description += f"\n> **Rating**: {rating}"
102+
embed.description += f"\n{synopsis}"
134103
embed.set_thumbnail(url=cover_image)
135104
embed.set_footer(text=str(mal_id))
105+
return embed, is_error_embed
136106

137107
else:
138-
embed = nextcord.Embed(
139-
description=":x: Anime not found.",
140-
color=ERROR_COLOR,
141-
)
108+
embed.description = ":x: Anime not found."
109+
embed.color = ERROR_COLOR
110+
is_error_embed = True
111+
return embed, is_error_embed
142112

143113
except Exception as e:
114+
is_error_embed = True
144115
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
145-
await interaction.send(embed=embed)
116+
return embed, is_error_embed
146117

147118
@commands.command(
148-
aliases=["animeplot", "anisyn", "animesyn"],
149-
help="Fetch a anime's summary from MyAnimeList. \nUsage: `anisyn Lycoris Recoil` or `anisyn 50709`.",
119+
name="anime",
120+
aliases=["ani"],
121+
help="Fetch anime information from MyAnimeList. \nUsage: `anime Lycoris Recoil` or `anime 50709`.",
150122
)
151-
async def anime_synopsis(self, ctx: commands.Context, *, anime_name: str):
152-
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
153-
try:
154-
anime = await self.fetch_anime(anime_name)
155-
if anime:
156-
title = anime.get("title")
157-
synopsis = anime.get("synopsis")
158-
if len(synopsis) > 700:
159-
synopsis = synopsis[:700] + "..."
160-
english_title = anime.get("title_english")
161-
cover_image = anime["images"]["jpg"]["image_url"]
162-
url = anime.get("url")
163-
mal_id = anime.get("mal_id")
123+
async def base_anime(self, ctx: commands.Context, *, anime_name: str):
124+
embed, is_error_embed = await self.build_anime_embed(anime_name)
125+
await ctx.reply(embed=embed, mention_author=False)
164126

165-
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
166-
embed.description = ""
167-
if english_title and english_title != title:
168-
embed.description += f"-# {english_title}\n"
169-
embed.description += f"\n{synopsis}"
170-
embed.set_thumbnail(url=cover_image)
171-
embed.set_footer(text=str(mal_id))
127+
@nextcord.slash_command(
128+
name="anime", description="MyAnimeList anime information commands."
129+
)
130+
async def anime(
131+
self,
132+
interaction: nextcord.Interaction,
133+
):
134+
pass
172135

173-
else:
174-
embed = nextcord.Embed(
175-
description=":x: Anime not found.",
176-
color=ERROR_COLOR,
177-
)
136+
@anime.subcommand(
137+
name="info", description="Fetch anime information from MyAnimeList."
138+
)
139+
async def slash_anime_info(
140+
self,
141+
interaction: Interaction,
142+
anime_name: str = SlashOption(description="Name of the anime"),
143+
):
144+
await interaction.response.defer()
145+
embed, is_error_embed = await self.build_anime_embed(anime_name)
146+
if is_error_embed:
147+
await interaction.send(embed=embed, ephemeral=True)
148+
else:
149+
await interaction.send(embed=embed)
178150

179-
except Exception as e:
180-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
151+
@commands.command(
152+
aliases=["animeplot", "anisyn", "animesyn"],
153+
help="Fetch a anime's summary from MyAnimeList. \nUsage: `anisyn Lycoris Recoil` or `anisyn 50709`.",
154+
)
155+
async def anime_synopsis(self, ctx: commands.Context, *, anime_name: str):
156+
embed, is_error_embed = await self.build_anisyn_embed(anime_name)
181157
await ctx.reply(embed=embed, mention_author=False)
182158

183159
@anime.subcommand(
@@ -189,38 +165,11 @@ async def slash_anime_synopsis(
189165
anime_name: str = SlashOption(description="Name of the anime"),
190166
):
191167
await interaction.response.defer()
192-
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
193-
try:
194-
anime = await self.fetch_anime(anime_name)
195-
if anime:
196-
title = anime.get("title")
197-
synopsis = anime.get("synopsis")
198-
if len(synopsis) > 700:
199-
synopsis = synopsis[:700] + "..."
200-
english_title = anime.get("title_english")
201-
cover_image = anime["images"]["jpg"]["image_url"]
202-
url = anime.get("url")
203-
mal_id = anime.get("mal_id")
204-
205-
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
206-
embed.description = ""
207-
if english_title and english_title != title:
208-
embed.description += f"-# {english_title}\n"
209-
embed.description += f"\n{synopsis}"
210-
embed.set_thumbnail(url=cover_image)
211-
embed.set_footer(text=str(mal_id))
212-
213-
else:
214-
embed = nextcord.Embed(
215-
description=":x: Anime not found.",
216-
color=ERROR_COLOR,
217-
)
218-
219-
except Exception as e:
220-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
168+
embed, is_error_embed = await self.build_anisyn_embed(anime_name)
169+
if is_error_embed:
221170
await interaction.send(embed=embed, ephemeral=True)
222-
return
223-
await interaction.send(embed=embed)
171+
else:
172+
await interaction.send(embed=embed)
224173

225174

226175
def setup(bot):

0 commit comments

Comments
 (0)