Skip to content

Commit 59af6aa

Browse files
committed
refactor(weebism.manga): DRY out this cog as well
1 parent 3e71cfa commit 59af6aa

File tree

1 file changed

+44
-109
lines changed

1 file changed

+44
-109
lines changed

takina/cogs/weebism/manga.py

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

31-
@commands.command(
32-
name="manga",
33-
help="Fetch manga information from MyAnimeList. \nUsage: `manga Lycoris Recoil` or `anime 135455`.",
34-
)
35-
async def base_manga(self, ctx: commands.Context, *, manga_name: str):
31+
async def build_manga_embed(self, manga_name):
3632
url = f"https://api.jikan.moe/v4/manga?q={manga_name}&limit=1"
33+
embed = nextcord.Embed(color=EMBED_COLOR)
3734
try:
3835
manga = await self.fetch_manga(manga_name)
3936
if manga:
@@ -66,113 +63,81 @@ async def base_manga(self, ctx: commands.Context, *, manga_name: str):
6663
embed.description += f"\n> **Authors**: {authors}"
6764
embed.set_thumbnail(url=cover_image)
6865
embed.set_footer(text=str(mal_id))
66+
return embed
6967

7068
else:
71-
embed = nextcord.Embed(
72-
description=":x: Manga not found.",
73-
color=ERROR_COLOR,
74-
)
69+
embed.description = ":x: Manga not found."
70+
embed.color = ERROR_COLOR
7571

7672
except Exception as e:
7773
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
78-
await ctx.reply(embed=embed, mention_author=False)
74+
return embed
7975

80-
@nextcord.slash_command(
81-
name="manga", description="MyAnimeList manga information commands."
82-
)
83-
async def manga(
84-
self,
85-
interaction: nextcord.Interaction,
86-
):
87-
pass
88-
89-
@manga.subcommand(
90-
name="info", description="Fetch manga information from MyAnimeList."
91-
)
92-
async def slash_manga_info(
93-
self,
94-
interaction: Interaction,
95-
manga_name: str = SlashOption(description="Name of the manga"),
96-
):
97-
await interaction.response.defer()
76+
async def build_mangasyn_embed(self, manga_name):
9877
url = f"https://api.jikan.moe/v4/manga?q={manga_name}&limit=1"
78+
embed = nextcord.Embed(color=EMBED_COLOR)
9979
try:
10080
manga = await self.fetch_manga(manga_name)
10181
if manga:
10282
title = manga.get("title")
103-
chapters = manga.get("chapters")
104-
volumes = manga.get("volumes")
105-
score = manga.get("score")
106-
published = manga.get("published", {}).get("string")
10783
english_title = manga.get("title_english")
108-
status = manga.get("status")
10984
cover_image = manga["images"]["jpg"]["image_url"]
11085
url = manga.get("url")
11186
mal_id = manga.get("mal_id")
112-
genres = ", ".join([genre["name"] for genre in manga.get("genres", [])])
113-
authors = " & ".join(
114-
[author["name"] for author in manga.get("authors", [])]
115-
)
87+
synopsis = manga.get("synopsis")
88+
if len(synopsis) > 700:
89+
synopsis = synopsis[:700] + "..."
11690

11791
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
11892
embed.description = ""
11993
if english_title and english_title != title:
12094
embed.description += f"-# {english_title}\n"
121-
if status != "Publishing":
122-
embed.description += f"\n> **Chapters**: {chapters}"
123-
embed.description += f"\n> **Volumes**: {volumes}"
124-
embed.description += f"\n> **Score**: {score}"
125-
embed.description += f"\n> **Status**: {status}"
126-
embed.description += f"\n> **Genres**: {genres}"
127-
embed.description += f"\n> **Published**: {published}"
128-
embed.description += f"\n> **Authors**: {authors}"
95+
embed.description += f"\n{synopsis}"
12996
embed.set_thumbnail(url=cover_image)
13097
embed.set_footer(text=str(mal_id))
98+
return embed
13199

132100
else:
133-
embed = nextcord.Embed(
134-
description=":x: Manga not found.",
135-
color=ERROR_COLOR,
136-
)
101+
embed.description = ":x: Manga not found."
102+
embed.color = ERROR_COLOR
103+
return embed
137104

138105
except Exception as e:
139106
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
107+
return embed
108+
109+
@commands.command(
110+
name="manga",
111+
help="Fetch manga information from MyAnimeList. \nUsage: `manga Lycoris Recoil` or `anime 135455`.",
112+
)
113+
async def base_manga(self, ctx: commands.Context, *, manga_name: str):
114+
embed = await self.build_manga_embed(manga_name)
115+
await ctx.reply(embed=embed, mention_author=False)
116+
117+
@nextcord.slash_command(
118+
name="manga", description="MyAnimeList manga information commands."
119+
)
120+
async def manga(self, interaction: nextcord.Interaction):
121+
pass
122+
123+
@manga.subcommand(
124+
name="info", description="Fetch manga information from MyAnimeList."
125+
)
126+
async def slash_manga_info(
127+
self,
128+
interaction: Interaction,
129+
manga_name: str = SlashOption(description="Name of the manga"),
130+
):
131+
await interaction.response.defer()
132+
embed = await self.build_manga_embed(manga_name)
140133
await interaction.send(embed=embed)
141134

142135
@commands.command(
143136
aliases=["mangaplot", "mangasyn"],
144137
help="Fetch a manga's summary from MyAnimeList. \nUsage: `mangasyn Lycoris Recoil` or `mangasyn 50709`.",
145138
)
146139
async def manga_synopsis(self, ctx: commands.Context, *, manga_name: str):
147-
url = f"https://api.jikan.moe/v4/manga?q={manga_name}&limit=1"
148-
try:
149-
manga = await self.fetch_manga(manga_name)
150-
if manga:
151-
title = manga.get("title")
152-
english_title = manga.get("title_english")
153-
cover_image = manga["images"]["jpg"]["image_url"]
154-
url = manga.get("url")
155-
mal_id = manga.get("mal_id")
156-
synopsis = manga.get("synopsis")
157-
if len(synopsis) > 700:
158-
synopsis = synopsis[:700] + "..."
159-
160-
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
161-
embed.description = ""
162-
if english_title and english_title != title:
163-
embed.description += f"-# {english_title}\n"
164-
embed.description += f"\n{synopsis}"
165-
embed.set_thumbnail(url=cover_image)
166-
embed.set_footer(text=str(mal_id))
167-
168-
else:
169-
embed = nextcord.Embed(
170-
description=":x: Manga not found.",
171-
color=ERROR_COLOR,
172-
)
173-
174-
except Exception as e:
175-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
140+
embed = await self.build_mangasyn_embed(manga_name)
176141
await ctx.reply(embed=embed, mention_author=False)
177142

178143
@manga.subcommand(
@@ -184,37 +149,7 @@ async def slash_manga_synopsis(
184149
manga_name: str = SlashOption(description="Name of the manga"),
185150
):
186151
await interaction.response.defer()
187-
url = f"https://api.jikan.moe/v4/manga?q={manga_name}&limit=1"
188-
try:
189-
manga = await self.fetch_manga(manga_name)
190-
if manga:
191-
title = manga.get("title")
192-
english_title = manga.get("title_english")
193-
cover_image = manga["images"]["jpg"]["image_url"]
194-
url = manga.get("url")
195-
mal_id = manga.get("mal_id")
196-
synopsis = manga.get("synopsis")
197-
if len(synopsis) > 700:
198-
synopsis = synopsis[:700] + "..."
199-
200-
embed = nextcord.Embed(title=title, url=url, color=EMBED_COLOR)
201-
embed.description = ""
202-
if english_title and english_title != title:
203-
embed.description += f"-# {english_title}\n"
204-
embed.description += f"\n{synopsis}"
205-
embed.set_thumbnail(url=cover_image)
206-
embed.set_footer(text=str(mal_id))
207-
208-
else:
209-
embed = nextcord.Embed(
210-
description=":x: Manga not found.",
211-
color=ERROR_COLOR,
212-
)
213-
214-
except Exception as e:
215-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
216-
await interaction.send(embed=embed, ephemeral=True)
217-
return
152+
embed = await self.build_mangasyn_embed(manga_name)
218153
await interaction.send(embed=embed)
219154

220155

0 commit comments

Comments
 (0)