Skip to content

Commit

Permalink
wiierror: Add a basic cache for Wiimmfi responses
Browse files Browse the repository at this point in the history
This cache will only be alive during runtime; it is not using the db.

It (hopefully) saves us from too many API calls.
  • Loading branch information
lifehackerhansol authored and FrozenChen committed Sep 5, 2024
1 parent e189338 commit 99d749b
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions cogs/assistancewii.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class AssistanceWii(commands.GroupCog):
def __init__(self, bot: Kurisu):
self.bot: Kurisu = bot

# Cached error codes from Wiimmfi
saved_errors = []

@commands.dynamic_cooldown(KurisuCooldown(1, 30.0), commands.BucketType.channel)
@commands.command(aliases=["wfcerror"])
async def wiierror(self, ctx: KurisuContext, error: int):
Expand All @@ -38,19 +41,33 @@ async def wiierror(self, ctx: KurisuContext, error: int):
if error > 999999:
return await ctx.send(f"{ctx.author.mention} This is an invalid error code. Please try again.")
apicall = f"https://wiimmfi.de/error?m=json&e={error}"
async with ctx.typing():
async with self.bot.session.get(apicall) as r:
if r.status == 200:
response = await r.json()
if response[0]["found"] == 0:
return await ctx.send(f"{ctx.author.mention} This error code does not exist.")

errorinfo = {}

# Check if we have this error cached
for i in self.saved_errors:
if i["error"] == error:
errorinfo = i

# If our error wasn't cached, go ask Wiimmfi
if not errorinfo:
async with ctx.typing():
async with self.bot.session.get(apicall) as r:
if r.status == 200:
response = await r.json()
errorinfo = response[0]
# Cache the error
self.saved_errors.append(response[0])
else:
embed = discord.Embed(title=f"Error {error}", colour=ConsoleColor.wii())
for i in response[0]["infolist"]:
embed.add_field(name=i["type"], value=f'**{i["name"]}**: {markdownify(i["info"])}', inline=False)
return await ctx.send(embed=embed)
else:
return await ctx.send(f'{ctx.author.mention} API returned error {r.status}. Please check your values and try again.')
return await ctx.send(f'{ctx.author.mention} API returned error {r.status}. Please check your values and try again.')

if errorinfo["found"] == 0:
return await ctx.send(f"{ctx.author.mention} This error code does not exist.")
else:
embed = discord.Embed(title=f"Error {error}", colour=ConsoleColor.wii())
for i in errorinfo["infolist"]:
embed.add_field(name=i["type"], value=f'**{i["name"]}**: {markdownify(i["info"])}', inline=False)
return await ctx.send(embed=embed)


add_md_files_as_commands(AssistanceWii, console_cmd="wii")
Expand Down

0 comments on commit 99d749b

Please sign in to comment.