Skip to content

Commit 3e71cfa

Browse files
committed
refactor(weebism.mal_profiles): DRY out the code
1 parent e656ef5 commit 3e71cfa

File tree

3 files changed

+35
-102
lines changed

3 files changed

+35
-102
lines changed

takina/cogs/weebism/anime.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async def fetch_anime(self, anime_name: str):
3030

3131
async def build_anime_embed(self, anime_name):
3232
embed = nextcord.Embed(color=EMBED_COLOR)
33-
is_error_embed = False
3433
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
3534
try:
3635
anime = await self.fetch_anime(anime_name)
@@ -66,22 +65,19 @@ async def build_anime_embed(self, anime_name):
6665
embed.description += f"\n> **Rating**: {rating}"
6766
embed.set_thumbnail(url=cover_image)
6867
embed.set_footer(text=str(mal_id))
69-
return embed, is_error_embed
68+
return embed
7069

7170
else:
7271
embed.description = ":x: Anime not found."
7372
embed.color = ERROR_COLOR
74-
is_error_embed = True
75-
return embed, is_error_embed
73+
return embed
7674

7775
except Exception as e:
7876
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
79-
is_error_embed = True
80-
return embed, is_error_embed
77+
return embed
8178

8279
async def build_anisyn_embed(self, anime_name):
8380
embed = nextcord.Embed(color=EMBED_COLOR)
84-
is_error_embed = False
8581
url = f"https://api.jikan.moe/v4/anime?q={anime_name}&limit=1"
8682
try:
8783
anime = await self.fetch_anime(anime_name)
@@ -102,26 +98,24 @@ async def build_anisyn_embed(self, anime_name):
10298
embed.description += f"\n{synopsis}"
10399
embed.set_thumbnail(url=cover_image)
104100
embed.set_footer(text=str(mal_id))
105-
return embed, is_error_embed
101+
return embed
106102

107103
else:
108104
embed.description = ":x: Anime not found."
109105
embed.color = ERROR_COLOR
110-
is_error_embed = True
111-
return embed, is_error_embed
106+
return embed
112107

113108
except Exception as e:
114-
is_error_embed = True
115109
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
116-
return embed, is_error_embed
110+
return embed
117111

118112
@commands.command(
119113
name="anime",
120114
aliases=["ani"],
121115
help="Fetch anime information from MyAnimeList. \nUsage: `anime Lycoris Recoil` or `anime 50709`.",
122116
)
123117
async def base_anime(self, ctx: commands.Context, *, anime_name: str):
124-
embed, is_error_embed = await self.build_anime_embed(anime_name)
118+
embed = await self.build_anime_embed(anime_name)
125119
await ctx.reply(embed=embed, mention_author=False)
126120

127121
@nextcord.slash_command(
@@ -142,18 +136,15 @@ async def slash_anime_info(
142136
anime_name: str = SlashOption(description="Name of the anime"),
143137
):
144138
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)
139+
embed = await self.build_anime_embed(anime_name)
140+
await interaction.send(embed=embed)
150141

151142
@commands.command(
152143
aliases=["animeplot", "anisyn", "animesyn"],
153144
help="Fetch a anime's summary from MyAnimeList. \nUsage: `anisyn Lycoris Recoil` or `anisyn 50709`.",
154145
)
155146
async def anime_synopsis(self, ctx: commands.Context, *, anime_name: str):
156-
embed, is_error_embed = await self.build_anisyn_embed(anime_name)
147+
embed = await self.build_anisyn_embed(anime_name)
157148
await ctx.reply(embed=embed, mention_author=False)
158149

159150
@anime.subcommand(
@@ -165,11 +156,8 @@ async def slash_anime_synopsis(
165156
anime_name: str = SlashOption(description="Name of the anime"),
166157
):
167158
await interaction.response.defer()
168-
embed, is_error_embed = await self.build_anisyn_embed(anime_name)
169-
if is_error_embed:
170-
await interaction.send(embed=embed, ephemeral=True)
171-
else:
172-
await interaction.send(embed=embed)
159+
embed = await self.build_anisyn_embed(anime_name)
160+
await interaction.send(embed=embed)
173161

174162

175163
def setup(bot):

takina/cogs/weebism/character.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ async def fetch_character(
1818
url1 = f"https://api.jikan.moe/v4/characters?q={character}&limit=1"
1919
url2 = f"https://api.jikan.moe/v4/characters/{character}"
2020
character_data = None
21-
is_error_embed = False
2221

2322
try:
2423
data = await request(url2)
@@ -32,14 +31,12 @@ async def fetch_character(
3231
except Exception as e:
3332
embed.description = f":x: {e}"
3433
embed.color = ERROR_COLOR
35-
is_error_embed = True
36-
return embed, is_error_embed
34+
return embed
3735

3836
if not character_data:
3937
embed.description = ":x: Character not found."
4038
embed.color = ERROR_COLOR
41-
is_error_embed = True
42-
return embed, is_error_embed
39+
return embed
4340

4441
name = character_data.get("name", "Unknown")
4542
cover_image = (
@@ -66,15 +63,15 @@ async def fetch_character(
6663
embed.set_thumbnail(url=cover_image)
6764
embed.set_footer(text=str(mal_id))
6865

69-
return embed, is_error_embed
66+
return embed
7067

7168
@commands.command(
7269
aliases=["waifu", "chr"],
7370
help="Fetch character information from MyAnimeList. \nUsage: `chr Takina Inoue` or `chr 204620`.",
7471
)
7572
async def character(self, ctx: commands.Context, *, character: str):
7673
embed = nextcord.Embed(color=EMBED_COLOR)
77-
embed, is_error_embed = await self.fetch_character(character, embed)
74+
embed = await self.fetch_character(character, embed)
7875
await ctx.reply(embed=embed, mention_author=False)
7976

8077
@nextcord.slash_command(
@@ -87,11 +84,8 @@ async def slash_character(
8784
):
8885
await interaction.response.defer()
8986
embed = nextcord.Embed(color=EMBED_COLOR)
90-
embed, is_error_embed = await self.fetch_character(character, embed)
91-
if is_error_embed:
92-
await interaction.send(embed=embed, ephemeral=True)
93-
else:
94-
await interaction.send(embed=embed)
87+
embed = await self.fetch_character(character, embed)
88+
await interaction.send(embed=embed)
9589

9690

9791
def setup(bot):

takina/cogs/weebism/mal_profiles.py

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ class MAL_Profiles(commands.Cog):
2222
def __init__(self, bot):
2323
self.bot = bot
2424

25-
@commands.command(
26-
help="Fetch information about a MyAnimeList user. \nUsage: `mal <username>`.",
27-
)
28-
async def mal(self, ctx: commands.Context, *, username: str):
25+
async def build_embed(self, username):
26+
embed = nextcord.Embed(color=EMBED_COLOR)
2927
try:
3028
profile_data = await request(f"https://api.jikan.moe/v4/users/{username}")
3129

3230
if not profile_data or not profile_data.get("data"):
33-
embed = nextcord.Embed(title="User not found.", color=EMBED_COLOR)
34-
await ctx.send(embed=embed, ephemeral=True)
35-
return
31+
embed.description = ":x: User not found."
32+
embed.color = ERROR_COLOR
33+
return embed
3634

3735
user = profile_data["data"]
3836

@@ -78,81 +76,34 @@ async def mal(self, ctx: commands.Context, *, username: str):
7876

7977
if profile_pic:
8078
embed.set_thumbnail(url=profile_pic)
79+
return embed
8180

8281
except Exception as e:
83-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
82+
embed.description = str(e)
83+
embed.color = ERROR_COLOR
84+
return embed
8485

86+
@commands.command(
87+
help="Fetch information about a MyAnimeList user. \nUsage: `mal <username>`.",
88+
)
89+
async def mal(self, ctx: commands.Context, *, username: str):
90+
embed = await self.build_embed(username)
8591
await ctx.reply(embed=embed, mention_author=False)
8692

8793
@nextcord.slash_command(
8894
name="mal", description="Fetch information about a MyAnimeList user."
8995
)
9096
async def mal_slash(
9197
self,
92-
ctx: nextcord.Interaction,
98+
interaction: nextcord.Interaction,
9399
*,
94100
username: str = nextcord.SlashOption(
95101
description="Username of the user to fetch"
96102
),
97103
):
98104
await interaction.response.defer()
99-
try:
100-
profile_data = await request(f"https://api.jikan.moe/v4/users/{username}")
101-
102-
if not profile_data or not profile_data.get("data"):
103-
embed = nextcord.Embed(title="User not found.", color=EMBED_COLOR)
104-
await ctx.send(embed=embed, ephemeral=True)
105-
return
106-
107-
user = profile_data["data"]
108-
109-
mal_id = user.get("mal_id")
110-
profile_url = user.get("url")
111-
profile_pic = user.get("images", {}).get("jpg", {}).get("image_url", "")
112-
gender = user.get("gender") or "Not Specified"
113-
last_online = format_date_long(user.get("last_online"))
114-
joined = format_date(user.get("joined"))
115-
location = user.get("location") or "Not Specified"
116-
anime_list_url = f"https://myanimelist.net/animelist/{username}"
117-
manga_list_url = f"https://myanimelist.net/mangalist/{username}"
118-
119-
# stats
120-
profile_stats = await request(
121-
f"https://api.jikan.moe/v4/users/{username}/statistics"
122-
)
123-
124-
anime_stats = profile_stats["data"].get("anime")
125-
days_watched = f"**{str(anime_stats.get("days_watched"))}**"
126-
anime_mean = f"**{str(anime_stats.get("mean_score"))}**"
127-
128-
manga_stats = profile_stats["data"].get("manga")
129-
days_read = f"**{str(manga_stats.get("days_read"))}**"
130-
manga_mean = f"**{str(manga_stats.get("mean_score"))}**"
131-
132-
embed = nextcord.Embed(
133-
title=f"{username}'s Profile",
134-
url=profile_url,
135-
color=EMBED_COLOR,
136-
)
137-
138-
embed.description = (
139-
f"-# [Anime List]({anime_list_url}) • [Manga List]({manga_list_url})\n"
140-
)
141-
embed.description += f"\n> **Gender**: {gender}"
142-
embed.description += f"\n> **Last Seen**: {last_online}"
143-
embed.description += f"\n> **Joined**: {joined}"
144-
embed.description += f"\n> **Location**: {location}"
145-
embed.description += f"\n> **Anime:** {days_watched} days watched with a mean score of {anime_mean}."
146-
embed.description += f"\n> **Manga:** {days_read} days read with a mean score of {manga_mean}."
147-
embed.set_footer(text=str(mal_id))
148-
149-
if profile_pic:
150-
embed.set_thumbnail(url=profile_pic)
151-
152-
except Exception as e:
153-
embed = nextcord.Embed(description=str(e), color=ERROR_COLOR)
154-
155-
await ctx.send(embed=embed, ephemeral=True)
105+
embed = await self.build_embed(username)
106+
await interaction.send(embed=embed)
156107

157108

158109
def setup(bot):

0 commit comments

Comments
 (0)