def calculate(self, *, equation : str):
'''Calculator'''
#_equation = re.sub("[^[0-9]+-/*^%\.]", "", equation).replace('^', "**") #words
replacements = {"pi" : "math.pi", 'e' : "math.e", "sin" : "math.sin", "cos" : "math.cos", "tan" : "math.tan", '^' : "**"}
allowed = set("0123456789.+-*/^%()")
for key, value in replacements.items():
equation = equation.replace(key, value)
equation = "".join(character for character in equation if character in allowed)
print("Calculated " + equation)
with multiprocessing.Pool(1) as pool:
async_result = pool.apply_async(eval, (equation,))
future = self.bot.loop.run_in_executor(None, async_result.get, 10.0)
try:
result = await asyncio.wait_for(future, 10.0, loop = self.bot.loop)
await self.bot.embed_reply("{} = {}".format(equation, result))
except discord.errors.HTTPException:
await self.bot.embed_reply(":no_entry: Output too long")
except SyntaxError:
await self.bot.embed_reply(":no_entry: Syntax error")
except ZeroDivisionError:
await self.bot.embed_reply(":no_entry: Error: Division by zero")
except (concurrent.futures.TimeoutError, multiprocessing.context.TimeoutError):
await self.bot.embed_reply(":no_entry: Execution exceeded time limit")
评论列表
文章目录