Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheat.sh Command - Python cheatsheet #572

Merged
merged 14 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ class Channels(NamedTuple):
devlog = int(environ.get("CHANNEL_DEVLOG", 622895325144940554))
dev_contrib = 635950537262759947
dev_branding = 753252897059373066
help_0 = 303906576991780866
help_1 = 303906556754395136
help_2 = 303906514266226689
help_3 = 439702951246692352
help_4 = 451312046647148554
help_5 = 454941769734422538
helpers = 385474242440986624
message_log = 467752170159079424
mod_alerts = 473092532147060736
Expand All @@ -133,6 +127,7 @@ class Channels(NamedTuple):


class Categories(NamedTuple):
help_in_use = 696958401460043776
development = 411199786025484308
devprojects = 787641585624940544
media = 799054581991997460
Expand Down Expand Up @@ -248,7 +243,6 @@ class Roles(NamedTuple):
announcements = 463658397560995840
champion = 430492892331769857
contributor = 295488872404484098
developer = 352427296948486144
devops = 409416496733880320
jammer = 423054537079783434
moderator = 267629731250176001
Expand All @@ -259,6 +253,7 @@ class Roles(NamedTuple):
rockstars = 458226413825294336
core_developers = 587606783669829632
events_lead = 778361735739998228
everyone_role = 267624335836053506


class Tokens(NamedTuple):
Expand Down
108 changes: 108 additions & 0 deletions bot/exts/evergreen/cheatsheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import random
import re
import typing as t
from urllib.parse import quote_plus

from discord import Embed
from discord.ext import commands
from discord.ext.commands import BucketType, Context

from bot import constants
from bot.constants import Categories, Channels, Colours, ERROR_REPLIES, Roles, WHITELISTED_CHANNELS
from bot.utils.decorators import with_role

ERROR_MESSAGE = f"""
Unknown cheat sheet. Please try to reformulate your query.

**Examples**:
```md
{constants.Client.prefix}cht read json
{constants.Client.prefix}cht hello world
{constants.Client.prefix}cht lambda
```
If the problem persists send a message in <#{Channels.dev_contrib}>
"""

URL = 'https://cheat.sh/python/{search}'
ESCAPE_TT = str.maketrans({"`": "\\`"})
ANSI_RE = re.compile(r"\x1b\[.*?m")


class CheatSheet(commands.Cog):
"""Commands that sends a result of a cht.sh search in code blocks."""

def __init__(self, bot: commands.Bot):
self.bot = bot

@staticmethod
def fmt_error_embed() -> Embed:
Shivansh-007 marked this conversation as resolved.
Show resolved Hide resolved
"""
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,
colour=Colours.soft_red
)
return embed

def result_fmt(self, url: str, body_text: str) -> t.Tuple[bool, t.Union[str, Embed]]:
"""Format Result."""
if body_text.startswith("# 404 NOT FOUND"):
embed = self.fmt_error_embed()
return True, embed

body_space = min(1986 - len(url), 1000)

if len(body_text) > body_space:
description = f"**Result Of cht.sh**\n" \
ChrisLovering marked this conversation as resolved.
Show resolved Hide resolved
f"```python\n{body_text[:body_space]}\n" \
f"... (truncated - too many lines)```\n" \
f"Full results: {url} "
Shivansh-007 marked this conversation as resolved.
Show resolved Hide resolved
else:
description = f"**Result Of cht.sh**\n" \
f"```python\n{body_text}```\n" \
f"{url}"
return False, description

@commands.command(
name="cheat",
aliases=("cht.sh", "cheatsheet", "cheat-sheet", "cht"),
)
@commands.cooldown(1, 10, BucketType.user)
@with_role(Roles.everyone_role)
Shivansh-007 marked this conversation as resolved.
Show resolved Hide resolved
async def cheat_sheet(self, ctx: Context, *search_terms: str) -> None:
"""
Search cheat.sh.

Gets a post from https://cheat.sh/python/ by default.
Usage:
--> .cht read json
"""
if not (
ctx.channel.category.id == Categories.help_in_use
or ctx.channel.id in WHITELISTED_CHANNELS
):
return

async with self.bot.http_session.get(
URL.format(search=quote_plus(" ".join(search_terms)))
) as response:
result = ANSI_RE.sub("", await response.text()).translate(ESCAPE_TT)
Shivansh-007 marked this conversation as resolved.
Show resolved Hide resolved

is_embed, description = self.result_fmt(
URL.format(search=quote_plus(" ".join(search_terms))),
Shivansh-007 marked this conversation as resolved.
Show resolved Hide resolved
result
)
if is_embed:
await ctx.send(embed=description)
else:
await ctx.send(content=description)


def setup(bot: commands.Bot) -> None:
"""Load the CheatSheet cog."""
bot.add_cog(CheatSheet(bot))