aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/evergreen/cheatsheet.py
diff options
context:
space:
mode:
authorGravatar Shivansh-007 <[email protected]>2021-02-01 05:18:28 +0530
committerGravatar Shivansh-007 <[email protected]>2021-02-01 05:18:28 +0530
commita602361492cc653f8fae82a3b30f36b232ba5024 (patch)
treebaa7e9f283471eb771de9c94a375ea0035274eba /bot/exts/evergreen/cheatsheet.py
parentFix import order (diff)
Rename cog CheatSheet ; Change Embed code to a single constructor ; remove headers as chubin/cheat.sh#280 got merged ; improve error embed docstrings
Diffstat (limited to 'bot/exts/evergreen/cheatsheet.py')
-rw-r--r--bot/exts/evergreen/cheatsheet.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py
index 3a689005..211b3246 100644
--- a/bot/exts/evergreen/cheatsheet.py
+++ b/bot/exts/evergreen/cheatsheet.py
@@ -22,7 +22,7 @@ If the problem persists send a message in <#{channel}>
"""
-class ChtSh(commands.Cog):
+class CheatSheet(commands.Cog):
"""Commands that sends a result of a cht.sh search in code blocks."""
def __init__(self, bot: commands.Bot):
@@ -30,10 +30,17 @@ class ChtSh(commands.Cog):
@staticmethod
def fmt_error_embed() -> Embed:
- """If the cht.sh search returned 404, overwrite it to send a custom error embed."""
- embed = Embed(colour=Colours.soft_red)
- embed.title = random.choice(ERROR_REPLIES)
- embed.description = ERROR_MESSAGE.format(channel=Channels.dev_contrib)
+ """
+ Format the Error Embed.
+
+ If the cht.sh search returned 404, overwrite it to send a custom error embed.
+ link -> https://github.com/chubin/cheat.sh/issues/198
+ """
+ embed = Embed(
+ title=random.choice(ERROR_REPLIES),
+ description=ERROR_MESSAGE.format(channel=Channels.dev_contrib),
+ colour=Colours.soft_red
+ )
return embed
def result_fmt(self, url: str, body_text: str) -> t.Tuple[bool, t.Union[str, Embed]]:
@@ -71,23 +78,20 @@ class ChtSh(commands.Cog):
--> .cht read json
"""
url = f'https://cheat.sh/python/{quote_plus(" ".join(search_terms))}'
- headers = {
- 'User-Agent': 'curl/7.68.0'
- }
escape_tt = str.maketrans({"`": "\\`"})
ansi_re = re.compile(r"\x1b\[.*?m")
- async with self.bot.http_session.get(url, headers=headers) as response:
+ async with self.bot.http_session.get(url) as response:
result = ansi_re.sub("", await response.text()).translate(escape_tt)
- is_embed, desciprtion = self.result_fmt(url, result)
+ is_embed, description = self.result_fmt(url, result)
if is_embed:
- await ctx.send(embed=desciprtion)
+ await ctx.send(embed=description)
else:
- await ctx.send(content=desciprtion)
+ await ctx.send(content=description)
def setup(bot: commands.Bot) -> None:
- """Load the ChtSh cog."""
- bot.add_cog(ChtSh(bot))
+ """Load the CheatSheet cog."""
+ bot.add_cog(CheatSheet(bot))