Okay, so I have a simple issue, I have a calculator command, which uses eval, how can I make my bot not crash when someone does 9^9^9^9 or maybe something bigger? I don't want a limit on it where they can only have this amount of exponents, but I want it to return a custom error if the eval failed, but I don't want my bot to go offline either, how can I prevent this?
@discord.ui.button(label='=', style=discord.ButtonStyle.green, custom_id="=", row=4)
async def equal(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id != self.ctx.author.id:
await interaction.response.send_message("This button isn't for you. Just run the command yourself goober.", ephemeral=True)
return
if interaction.user.id == self.ctx.author.id and not self.disabled:
expression = self.value.replace('x', '*').replace('รท', '/').replace('^', '**')
try:
try:
result = str(eval(expression))
self.value = f"{self.value} = {result}"
self.clear_on_next_input = True
except Exception:
self.value = "The provided equation is invalid!"
self.clear_on_next_input = True
except ZeroDivisionError:
self.value = 'Infinity'
self.clear_on_next_input = True
except Exception as e:
self.value = "The provided equation is invalid!"
self.clear_on_next_input = True
embed = await self.update_embed(interaction)
await interaction.response.edit_message(embed=embed, view=self)```