tux.cogs.utility.run
¶
Classes:
Name | Description |
---|---|
Run | |
Classes¶
Run(bot: Tux)
¶
Bases: Cog
Methods:
Name | Description |
---|---|
remove_ansi | Converts ANSI encoded text into non-ANSI. |
remove_backticks | Removes backticks from the provided string. |
generalized_code_executor | A generalized version of the code executor. |
generalized_code_constructor | A generalized version of the assembly generation function used previously. |
send_embedded_reply | A generalized version of an embed. |
run | Run code in various languages. Code should be enclosed in triple backticks with syntax highlighting. |
run_error | A generalized error handler for the run command. |
languages | Lists all the supported languages. |
Source code in tux/cogs/utility/run.py
Functions¶
remove_ansi(ansi: str) -> str
staticmethod
¶
remove_backticks(st: str) -> str
staticmethod
¶
generalized_code_executor(ctx: commands.Context[Tux], compiler_map: dict[str, str], code: str, options: str | None = None) -> tuple[str, str, str]
async
¶
A generalized version of the code executor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
compiler_map | dict[str, str] | A dictionary containing mappings from a language to its compiler. | required |
code | str | A string consisting of the code. | required |
options | str | None | Optional arguments to be passed to the compiler. | None |
Returns:
Type | Description |
---|---|
tuple[str, str, str] | A tuple containing the filtered output, the first few lines of the output, and the normalized language. |
Source code in tux/cogs/utility/run.py
async def generalized_code_executor(
self,
ctx: commands.Context[Tux],
compiler_map: dict[str, str],
code: str,
options: str | None = None,
) -> tuple[str, str, str]:
"""
A generalized version of the code executor.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
compiler_map : dict[str, str]
A dictionary containing mappings from a language to its compiler.
code : str
A string consisting of the code.
options : str | None
Optional arguments to be passed to the compiler.
Returns
-------
tuple[str, str, str]
A tuple containing the filtered output, the first few lines of the output, and the normalized language.
"""
cleaned_code = self.remove_backticks(code)
normalized_lang = cleaned_code.splitlines().pop(0)
cleaned_code = "\n".join(cleaned_code.splitlines()[1:])
if normalized_lang not in compiler_map:
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Fatal exception occurred!",
description="Bad Formatting",
)
await ctx.send(embed=embed)
return ("", "", "")
compiler_id = compiler_map[normalized_lang]
output = godbolt.getoutput(cleaned_code, compiler_id, options)
if output is None:
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Fatal exception occurred!",
description="failed to get output from the compiler",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
return ("", "", "")
lines = output.split("\n")
gen_one = lines[0]
filtered_output = "\n".join(lines[1:])
return (filtered_output, gen_one, normalized_lang)
generalized_code_constructor(ctx: commands.Context[Tux], compiler_map: dict[str, str], code: str, options: str | None = None) -> tuple[str, str, str]
async
¶
A generalized version of the assembly generation function used previously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
compiler_map | dict[str, str] | A dictionary containing mappings from a language to its compiler. | required |
code | str | A string consisting of the code. | required |
options | str | None | Optional arguments to be passed to the compiler. | None |
Returns:
Type | Description |
---|---|
tuple[str, str, str] | None | A tuple containing the filtered output, the first few lines of the output, and the normalized language. |
Source code in tux/cogs/utility/run.py
async def generalized_code_constructor(
self,
ctx: commands.Context[Tux],
compiler_map: dict[str, str],
code: str,
options: str | None = None,
) -> tuple[str, str, str]:
"""
A generalized version of the assembly generation function used previously.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
compiler_map : dict[str, str]
A dictionary containing mappings from a language to its compiler.
code : str
A string consisting of the code.
options : str | None
Optional arguments to be passed to the compiler.
Returns
-------
tuple[str, str, str] | None
A tuple containing the filtered output, the first few lines of the output, and the normalized language.
"""
cleaned_code = self.remove_backticks(code)
normalized_lang = cleaned_code.splitlines().pop(0)
cleaned_code = "\n".join(cleaned_code.splitlines()[1:])
if normalized_lang not in compiler_map:
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Fatal exception occurred!",
description="Bad Formatting",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
return ("", "", "")
compiler_id = compiler_map[normalized_lang]
output = godbolt.generateasm(cleaned_code, compiler_id, options)
if output is None:
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Fatal exception occurred!",
description="failed to get output from the compiler",
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
return ("", "", "")
lines = output.split("\n")
gen_one = lines[0]
filtered_output = "\n".join(lines[1:])
if len(filtered_output) > 3500:
return (
"The assembly is too big to fit! Please do it on the GodBolt website instead.",
gen_one,
normalized_lang,
)
return (filtered_output, gen_one, normalized_lang)
send_embedded_reply(ctx: commands.Context[Tux], gen_one: str, output: str, lang: str) -> None
async
¶
A generalized version of an embed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
gen_one | str | The first few lines of the output. | required |
output | str | The output. | required |
lang | str | The language of the code. | required |
Source code in tux/cogs/utility/run.py
async def send_embedded_reply(
self,
ctx: commands.Context[Tux],
gen_one: str,
output: str,
lang: str,
) -> None:
"""
A generalized version of an embed.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
gen_one : str
The first few lines of the output.
output : str
The output.
lang : str
The language of the code.
"""
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.INFO,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Compilation provided by https://godbolt.org/",
description=f"```{lang}\n{output}\n```",
)
await ctx.send(embed=embed)
run(ctx: commands.Context[Tux], *, code: str)
async
¶
Run code in various languages. Code should be enclosed in triple backticks with syntax highlighting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
code | str | The code to be evaluated. | required |
Source code in tux/cogs/utility/run.py
@commands.command(
name="run",
aliases=["compile", "exec"],
)
async def run(
self,
ctx: commands.Context[Tux],
*,
code: str,
):
"""
Run code in various languages. Code should be enclosed in triple backticks with syntax highlighting.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
code : str
The code to be evaluated.
"""
msg = await ctx.send("<a:typing:1236671731859722270>")
(filtered_output, gen_one, normalized_lang) = await self.generalized_code_executor(
ctx,
compiler_map,
code,
)
await msg.delete()
if not filtered_output and not gen_one and not normalized_lang:
return
await self.send_embedded_reply(
ctx,
gen_one,
self.remove_ansi(filtered_output),
normalized_lang,
)
run_error(ctx: commands.Context[Tux], error: Exception)
async
¶
A generalized error handler for the run command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
error | Exception | The error that occurred. | required |
Source code in tux/cogs/utility/run.py
@run.error
async def run_error(
self,
ctx: commands.Context[Tux],
error: Exception,
):
"""
A generalized error handler for the run command.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
error : Exception
The error that occurred.
"""
desc = ""
if isinstance(error, commands.CommandInvokeError):
desc = error.original
if isinstance(error, commands.MissingRequiredArgument):
desc = f"Missing required argument: `{error.param.name}`"
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Fatal exception occurred!",
description=str(desc),
)
await ctx.send(embed=embed, ephemeral=True, delete_after=30)
languages(ctx: commands.Context[Tux]) -> None
async
¶
Lists all the supported languages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context in which the command is invoked. | required |
Source code in tux/cogs/utility/run.py
@commands.command(
name="languages",
aliases=["langs"],
)
async def languages(self, ctx: commands.Context[Tux]) -> None:
"""
Lists all the supported languages.
Parameters
----------
ctx : commands.Context[Tux]
The context in which the command is invoked.
"""
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.INFO,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title="Supported Languages",
description=f"```{', '.join(compiler_map.keys())}```",
)
await ctx.send(embed=embed)