Exemplo n.º 1
0
    async def bark(self, ctx,*, script):
        async with ctx.typing():
            script = script.replace('```', '')
            try:
                # Set Up Whitelist using string of Wolfram code, only allowed functions specified here are able to execute (see WOLFRAM_WHITELIST.txt for formatted text for the following string)
                code = 'WHITELIST = {List, CompoundExpression, Set, SetDelayed, Blank, Pattern, Real, Complex, Hold, Sin, Cos, Plus, Times, Power, Log, Symbol, Integer, N, NIntegrate, Integrate, D, ReplaceAll, Rule, Style, FontFamily, Background, Plot, Graphics}; SetAttributes[whit, HoldFirst]; whit[REPLACE_] := Block[{i, k}, If[Length[Flatten[List[DeleteMissing[DeleteDuplicates[Evaluate[Map[Head, Level[Hold[REPLACE], {0, Infinity}]]/.Table[WHITELIST[[i]] -> Missing[], {i, Length[WHITELIST]}]] /. Missing[][u__] -> {u}]]]]] > 0, "Grrr, banned function(s)! " <> ToString[DeleteMissing[DeleteDuplicates[Evaluate[Map[Head, Level[Hold[REPLACE], {0, Infinity}]]] /.Table[WHITELIST[[k]] -> Missing[], {k, Length[WHITELIST]}] /. Missing[][u__] -> {u}]]], REPLACE]];'
                await session.evaluate(wlexpr(code))
                export = wrap_wolf(script)

                # Evaluate given expression, exporting result as png
                eval = await asyncio.wait_for(session.evaluate_wrap(wlexpr(export)), 40)

                # Check for errors before sending result
                log = str(eval.messages)

                # Remove any (' and ') from error messages
                if  '(\'' in log and ('\')' in log or '\',)' in log):
                    log = log.replace('(\'', '')
                    log = log.replace('\')', '')

                # Determine output when there's a wolfram error
                if log != 'None':
                    if(len(log) > 256):
                        await ctx.send(embed = embeds.general_error)
                    elif (log).startswith('(\'Invalid syntax'):
                        await ctx.send(embed = embeds.syntax_error)
                    elif log.startswith('(\'Not enough memory available to rasterize Notebook expression.\',)'):
                        await ctx.send(embed = embeds.memory_error)
                        await ctx.send(f'```{await session.evaluate_wrap(wlexpr(script), timeout = 5)}```')
                    else:
                        log = embeds.createEmbed(log)
                        ##################enlarge()
                        if 'Animate' not in script:
                            await ctx.send(file=discord.File(img_path))
                        else:
                            await ctx.send(file=discord.File(f'{file}/output/output.gif'))
                        await ctx.send(embed = log) 
                else:
                    # No errors, continue
                    # Send image from Wolfram calculation results
                    if 'Animate' not in script:
                        await ctx.send(file=discord.File(img_path))
                    else:
                        await ctx.send(file=discord.File(f'{file}/output/output.gif'))

            except exceptions.WhiteListError as error:
                await ctx.send(error.message)
            except exceptions.BlackListError as error:
                await ctx.send(error.message)
            except asyncio.TimeoutError:
                await ctx.send(embed = embeds.time_error)

            await session.evaluate(wlexpr('ClearAll["Global`*"]'))
            embeds.tail_message.description = f'Requested by\n{ctx.message.author.mention}'
            await ctx.send(embed = embeds.tail_message)
Exemplo n.º 2
0
    async def bark(self, ctx,*, script):
        async with ctx.typing():
            script = script.replace('```', '')
            try:
                export = wrap_wolf(script)

                # Evaluate given expression, exporting result as png
                eval = await asyncio.wait_for(session.evaluate_wrap(wlexpr(export)), 40)

                # Check for errors before sending result
                log = str(eval.messages)

                # Remove any (' and ') from error messages
                if  '(\'' in log and ('\')' in log or '\',)' in log):
                    log = log.replace('(\'', '')
                    log = log.replace('\')', '')

                # Determine output when there's a wolfram error
                if log != 'None':
                    if(len(log) > 256):
                        await ctx.send(embed = embeds.general_error)
                    elif (log).startswith('(\'Invalid syntax'):
                        await ctx.send(embed = embeds.syntax_error)
                    elif log.startswith('(\'Not enough memory available to rasterize Notebook expression.\',)'):
                        await ctx.send(embed = embeds.memory_error)
                        await ctx.send(f'```{await session.evaluate_wrap(wlexpr(script), timeout = 5)}```')
                    else:
                        log = embeds.createEmbed(log)
                        ##################enlarge()
                        if 'Animate' not in script:
                            await ctx.send(file=discord.File(img_path))
                        else:
                            await ctx.send(file=discord.File(f'{file}/output/output.gif'))
                        await ctx.send(embed = log) 
                else:
                    # No errors, continue
                    # Send image from Wolfram calculation results
                    if 'Animate' not in script:
                        await ctx.send(file=discord.File(img_path))
                    else:
                        await ctx.send(file=discord.File(f'{file}/output/output.gif'))

            except exceptions.WhiteListError as error:
                await ctx.send(error.message)
            except exceptions.BlackListError as error:
                await ctx.send(error.message)
            except asyncio.TimeoutError:
                await ctx.send(embed = embeds.time_error)

            await session.evaluate(wlexpr('ClearAll["Global`*"]'))
            embeds.tail_message.description = f'Requested by\n{ctx.message.author.mention}'
            await ctx.send(embed = embeds.tail_message)
Exemplo n.º 3
0
    async def bark(self, ctx, *, script):
        # Prepares the user input to be passed into Wolfram functions that export the output image, and limit the time of the computation
        async with ctx.typing():
            export = f'Export["{img_path}", Style[{script}, Large], Background -> None, ImageResolution -> 100]'
            try:
                # Evaluate given expression, exporting result as png
                eval = await asyncio.wait_for(
                    session.evaluate_wrap(wlexpr(export)), 40)

                # Check for errors before sending result
                log = str(eval.messages)

                # Remove any (' and ') from error messages
                if '(\'' in log and ('\')' in log or '\',)' in log):
                    log.replace('(\'', '')
                    log.replace('\')', '')

                # Determine output when there's a wolfram error
                if log != 'None':
                    if (len(log) > 256):
                        await ctx.send(embed=embeds.general_error)
                    elif (log).startswith('(\'Invalid syntax'):
                        await ctx.send(embed=embeds.syntax_error)
                    elif log.startswith(
                            '(\'Not enough memory available to rasterize Notebook expression.\',)'
                    ):
                        await ctx.send(embed=embeds.memory_error)
                        await ctx.send(
                            f'```{await session.evaluate_wrap(wlexpr(script), timeout = 5)}```'
                        )
                    else:
                        log = embeds.createEmbed(log)
                        ##################enlarge()
                        await ctx.send(file=discord.File(img_path))
                        await ctx.send(embed=log)
                else:
                    # No errors, continue
                    ######################enlarge()
                    # Send image from Wolfram calculation results
                    await ctx.send(file=discord.File(img_path))
            except Exception:
                await ctx.send(embed=embeds.time_error)
            await session.evaluate(wlexpr('ClearAll["Global`*"]'))
            embeds.tail_message.description = f'Requested by\n{ctx.message.author.mention}'
            await ctx.send(embed=embeds.tail_message)