From 661ab2cc966c96d6f05dfc0444f7569b2a8fd727 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Thu, 28 Jan 2021 04:44:36 +0530 Subject: Add http status dog and add http_cat to group http_status --- bot/exts/evergreen/status_cats.py | 33 -------------------- bot/exts/evergreen/status_codes.py | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 33 deletions(-) delete mode 100644 bot/exts/evergreen/status_cats.py create mode 100644 bot/exts/evergreen/status_codes.py (limited to 'bot') diff --git a/bot/exts/evergreen/status_cats.py b/bot/exts/evergreen/status_cats.py deleted file mode 100644 index 586b8378..00000000 --- a/bot/exts/evergreen/status_cats.py +++ /dev/null @@ -1,33 +0,0 @@ -from http import HTTPStatus - -import discord -from discord.ext import commands - - -class StatusCats(commands.Cog): - """Commands that give HTTP statuses described and visualized by cats.""" - - def __init__(self, bot: commands.Bot): - self.bot = bot - - @commands.command(aliases=['statuscat']) - async def http_cat(self, ctx: commands.Context, code: int) -> None: - """Sends an embed with an image of a cat, potraying the status code.""" - embed = discord.Embed(title=f'**Status: {code}**') - - try: - HTTPStatus(code) - - except ValueError: - embed.set_footer(text='Inputted status code does not exist.') - - else: - embed.set_image(url=f'https://http.cat/{code}.jpg') - - finally: - await ctx.send(embed=embed) - - -def setup(bot: commands.Bot) -> None: - """Load the StatusCats cog.""" - bot.add_cog(StatusCats(bot)) diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py new file mode 100644 index 00000000..eebc23af --- /dev/null +++ b/bot/exts/evergreen/status_codes.py @@ -0,0 +1,63 @@ +import re +from http import HTTPStatus + +import discord +from discord import HTTPException +from discord.ext import commands + + +class StatusCats(commands.Cog): + """Commands that give HTTP statuses described and visualized by cats and dogs.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.group(name="http_status", aliases=("status", "httpstatus")) + async def http_status_group(self, ctx: commands.Context) -> None: + """Group containing dog and cat http status code commands.""" + if not ctx.invoked_subcommand: + await ctx.send_help(ctx.command) + + @http_status_group.command(aliases=['cat']) + async def http_cat(self, ctx: commands.Context, code: int) -> None: + """Sends an embed with an image of a cat, portraying the status code.""" + embed = discord.Embed(title=f'**Status: {code}**') + + try: + HTTPStatus(code) + + except ValueError: + embed.set_footer(text='Inputted status code does not exist.') + + else: + embed.set_image(url=f'https://http.cat/{code}.jpg') + + finally: + await ctx.send(embed=embed) + + @http_status_group.command(aliases=['dog']) + async def http_dog(self, ctx: commands.Context, code: int) -> None: + """Sends an embed with an image of a dog, portraying the status code.""" + embed = discord.Embed(title=f'**Status: {code}**') + + try: + HTTPStatus(code) + async with self.bot.http_session.get( + f'https://httpstatusdogs.com/img/{code}.jpg', + allow_redirects=False + ) as response: + if response.status != 302: + embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') + else: + raise ValueError + + except ValueError: + embed.set_footer(text='Inputted status code does not exist.') + + finally: + await ctx.send(embed=embed) + + +def setup(bot: commands.Bot) -> None: + """Load the StatusCats cog.""" + bot.add_cog(StatusCats(bot)) -- cgit v1.2.3 From f5eae5bec548bc1e450af7f07387531b80869f84 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sun, 31 Jan 2021 05:26:23 +0530 Subject: Remove unused imports --- bot/exts/evergreen/status_codes.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index eebc23af..473e778c 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -1,8 +1,6 @@ -import re from http import HTTPStatus import discord -from discord import HTTPException from discord.ext import commands -- cgit v1.2.3 From c60c8471b4e0cb9b54bee80fd43c75c4a2e75b76 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Sun, 31 Jan 2021 15:13:58 +0530 Subject: Fix import order --- bot/exts/evergreen/cheatsheet.py | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 bot/exts/evergreen/cheatsheet.py (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py new file mode 100644 index 00000000..3a689005 --- /dev/null +++ b/bot/exts/evergreen/cheatsheet.py @@ -0,0 +1,93 @@ +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.constants import Channels, Colours, ERROR_REPLIES + +ERROR_MESSAGE = """ +Unknown cheat sheet. Please try to reformulate your query. + +**Examples**: +```md +.cht read json +.cht hello world +.cht lambda +``` +If the problem persists send a message in <#{channel}> +""" + + +class ChtSh(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: + """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) + 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" \ + f"```python\n{body_text[:body_space]}\n" \ + f"... (truncated - too many lines)```\n" \ + f"Full results: {url} " + 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) + 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 + """ + 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: + result = ansi_re.sub("", await response.text()).translate(escape_tt) + + is_embed, desciprtion = self.result_fmt(url, result) + if is_embed: + await ctx.send(embed=desciprtion) + else: + await ctx.send(content=desciprtion) + + +def setup(bot: commands.Bot) -> None: + """Load the ChtSh cog.""" + bot.add_cog(ChtSh(bot)) -- cgit v1.2.3 From a602361492cc653f8fae82a3b30f36b232ba5024 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:18:28 +0530 Subject: Rename cog CheatSheet ; Change Embed code to a single constructor ; remove headers as chubin/cheat.sh#280 got merged ; improve error embed docstrings --- bot/exts/evergreen/cheatsheet.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'bot') 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)) -- cgit v1.2.3 From c87f6e0561bbeb6510292a0b09a829aa6fa290a7 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:21:32 +0530 Subject: rename the cog to HTTPStatusCodes --- bot/exts/evergreen/status_codes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 473e778c..19375657 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -4,7 +4,7 @@ import discord from discord.ext import commands -class StatusCats(commands.Cog): +class HTTPStatusCodes(commands.Cog): """Commands that give HTTP statuses described and visualized by cats and dogs.""" def __init__(self, bot: commands.Bot): @@ -57,5 +57,5 @@ class StatusCats(commands.Cog): def setup(bot: commands.Bot) -> None: - """Load the StatusCats cog.""" - bot.add_cog(StatusCats(bot)) + """Load the HTTPStatusCodes cog.""" + bot.add_cog(HTTPStatusCodes(bot)) -- cgit v1.2.3 From e32182ee7000cea8a49d41605bb01563c0d68800 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 1 Feb 2021 05:28:35 +0530 Subject: Add similar repsonse.status check to too, and instead of ValueError raise NotImplemented error if the status is not implemented yet but is a valid status code --- bot/exts/evergreen/status_codes.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index 19375657..bfc4bb1a 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -23,13 +23,18 @@ class HTTPStatusCodes(commands.Cog): try: HTTPStatus(code) + async with self.bot.http_session.get( + f'https://http.cat/{code}.jpg', + allow_redirects=False + ) as response: + if response.status != 404: + embed.set_image(url=f'https://http.cat/{code}.jpg') + else: + raise NotImplementedError except ValueError: embed.set_footer(text='Inputted status code does not exist.') - else: - embed.set_image(url=f'https://http.cat/{code}.jpg') - finally: await ctx.send(embed=embed) @@ -47,11 +52,14 @@ class HTTPStatusCodes(commands.Cog): if response.status != 302: embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') else: - raise ValueError + raise NotImplementedError except ValueError: embed.set_footer(text='Inputted status code does not exist.') + except NotImplementedError: + embed.set_footer(text='Inputted status code is not implemented by httpstatusdogs.com yet.') + finally: await ctx.send(embed=embed) -- cgit v1.2.3 From 5c4cbbeeb5fd9e7f7c651b24b2b1d3bdbcc55110 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 05:09:29 +0530 Subject: Make use of constants --- bot/exts/evergreen/cheatsheet.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index 211b3246..4191a83f 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -7,20 +7,25 @@ from discord import Embed from discord.ext import commands from discord.ext.commands import BucketType, Context +from bot import constants from bot.constants import Channels, Colours, ERROR_REPLIES -ERROR_MESSAGE = """ +ERROR_MESSAGE = f""" Unknown cheat sheet. Please try to reformulate your query. **Examples**: ```md -.cht read json -.cht hello world -.cht lambda +{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 <#{channel}> +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.""" @@ -38,7 +43,7 @@ class CheatSheet(commands.Cog): """ embed = Embed( title=random.choice(ERROR_REPLIES), - description=ERROR_MESSAGE.format(channel=Channels.dev_contrib), + description=ERROR_MESSAGE, colour=Colours.soft_red ) return embed @@ -67,9 +72,7 @@ class CheatSheet(commands.Cog): aliases=("cht.sh", "cheatsheet", "cheat-sheet", "cht"), ) @commands.cooldown(1, 10, BucketType.user) - async def cheat_sheet( - self, ctx: Context, *search_terms: str - ) -> None: + async def cheat_sheet(self, ctx: Context, *search_terms: str) -> None: """ Search cheat.sh. @@ -77,15 +80,15 @@ class CheatSheet(commands.Cog): Usage: --> .cht read json """ - url = f'https://cheat.sh/python/{quote_plus(" ".join(search_terms))}' - - escape_tt = str.maketrans({"`": "\\`"}) - ansi_re = re.compile(r"\x1b\[.*?m") - - async with self.bot.http_session.get(url) as response: - result = ansi_re.sub("", await response.text()).translate(escape_tt) - - is_embed, description = self.result_fmt(url, result) + 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) + + is_embed, description = self.result_fmt( + URL.format(search=quote_plus(" ".join(search_terms))), + result + ) if is_embed: await ctx.send(embed=description) else: -- cgit v1.2.3 From 2631004f7d1467bc81090ac481de78cbfe7978e5 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 05:14:34 +0530 Subject: Add override_in_channel deco --- bot/exts/evergreen/cheatsheet.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index 4191a83f..c83af839 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -8,7 +8,8 @@ from discord.ext import commands from discord.ext.commands import BucketType, Context from bot import constants -from bot.constants import Channels, Colours, ERROR_REPLIES +from bot.constants import Channels, Colours, ERROR_REPLIES, WHITELISTED_CHANNELS +from bot.utils.decorators import override_in_channel ERROR_MESSAGE = f""" Unknown cheat sheet. Please try to reformulate your query. @@ -71,6 +72,7 @@ class CheatSheet(commands.Cog): name="cheat", aliases=("cht.sh", "cheatsheet", "cheat-sheet", "cht"), ) + @override_in_channel(WHITELISTED_CHANNELS) @commands.cooldown(1, 10, BucketType.user) async def cheat_sheet(self, ctx: Context, *search_terms: str) -> None: """ -- cgit v1.2.3 From aee64f82f5a97a4bdd32c6d6d66f6f0eb8ef51f8 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 05:23:34 +0530 Subject: Add not Implemented error handler for .status cat command --- bot/exts/evergreen/status_codes.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bot') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index bfc4bb1a..ff2ac9e1 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -35,6 +35,9 @@ class HTTPStatusCodes(commands.Cog): except ValueError: embed.set_footer(text='Inputted status code does not exist.') + except NotImplementedError: + embed.set_footer(text='Inputted status code is not implemented by http.cat yet.') + finally: await ctx.send(embed=embed) -- cgit v1.2.3 From 3e884eaa43b78b433a470aaff39b03a0f6c2b3c2 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 06:05:46 +0530 Subject: Allow cht.sh command to be used in occupied help channels --- bot/constants.py | 11 +++++------ bot/exts/evergreen/cheatsheet.py | 13 +++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 1d41a53e..23865272 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -104,12 +104,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 @@ -128,6 +122,10 @@ class Channels(NamedTuple): voice_chat_1 = 799647045886541885 +class Categories(NamedTuple): + help_in_use = 696958401460043776 + + class Client(NamedTuple): name = "Sir Lancebot" guild = int(environ.get("BOT_GUILD", 267624335836053506)) @@ -249,6 +247,7 @@ class Roles(NamedTuple): rockstars = 458226413825294336 core_developers = 587606783669829632 events_lead = 778361735739998228 + everyone = 267624335836053506 class Tokens(NamedTuple): diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index c83af839..83098c76 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -3,13 +3,13 @@ import re import typing as t from urllib.parse import quote_plus -from discord import Embed +from discord import Embed, utils from discord.ext import commands from discord.ext.commands import BucketType, Context from bot import constants -from bot.constants import Channels, Colours, ERROR_REPLIES, WHITELISTED_CHANNELS -from bot.utils.decorators import override_in_channel +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. @@ -72,8 +72,8 @@ class CheatSheet(commands.Cog): name="cheat", aliases=("cht.sh", "cheatsheet", "cheat-sheet", "cht"), ) - @override_in_channel(WHITELISTED_CHANNELS) @commands.cooldown(1, 10, BucketType.user) + @with_role(Roles.everyone) async def cheat_sheet(self, ctx: Context, *search_terms: str) -> None: """ Search cheat.sh. @@ -82,6 +82,11 @@ class CheatSheet(commands.Cog): Usage: --> .cht read json """ + category = utils.get(ctx.message.guild.categories, id=Categories.help_in_use) + all_allowed_channels = WHITELISTED_CHANNELS + tuple(category.channels) + if ctx.channel not in all_allowed_channels: + return + async with self.bot.http_session.get( URL.format(search=quote_plus(" ".join(search_terms))) ) as response: -- cgit v1.2.3 From b10b2524a1284f7462f3b0f039827e6d115e7931 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 06:16:38 +0530 Subject: Improve category check code --- bot/exts/evergreen/cheatsheet.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index 83098c76..abc5306b 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -3,7 +3,7 @@ import re import typing as t from urllib.parse import quote_plus -from discord import Embed, utils +from discord import Embed from discord.ext import commands from discord.ext.commands import BucketType, Context @@ -82,9 +82,10 @@ class CheatSheet(commands.Cog): Usage: --> .cht read json """ - category = utils.get(ctx.message.guild.categories, id=Categories.help_in_use) - all_allowed_channels = WHITELISTED_CHANNELS + tuple(category.channels) - if ctx.channel not in all_allowed_channels: + 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( -- cgit v1.2.3 From 5190f5b8576267807dd6a4920964d684f0d572a4 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Tue, 2 Feb 2021 06:17:58 +0530 Subject: everyone -> everyone_role and remove developers role --- bot/constants.py | 3 +-- bot/exts/evergreen/cheatsheet.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'bot') diff --git a/bot/constants.py b/bot/constants.py index 23865272..8e210ad3 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -236,7 +236,6 @@ class Roles(NamedTuple): announcements = 463658397560995840 champion = 430492892331769857 contributor = 295488872404484098 - developer = 352427296948486144 devops = 409416496733880320 jammer = 423054537079783434 moderator = 267629731250176001 @@ -247,7 +246,7 @@ class Roles(NamedTuple): rockstars = 458226413825294336 core_developers = 587606783669829632 events_lead = 778361735739998228 - everyone = 267624335836053506 + everyone_role = 267624335836053506 class Tokens(NamedTuple): diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index abc5306b..f650e3b0 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -73,7 +73,7 @@ class CheatSheet(commands.Cog): aliases=("cht.sh", "cheatsheet", "cheat-sheet", "cht"), ) @commands.cooldown(1, 10, BucketType.user) - @with_role(Roles.everyone) + @with_role(Roles.everyone_role) async def cheat_sheet(self, ctx: Context, *search_terms: str) -> None: """ Search cheat.sh. -- cgit v1.2.3 From 5e55ea1dbfd652c10999c88d1c32aebd1be768f0 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Thu, 4 Feb 2021 05:23:54 +0530 Subject: Make use of constants in the url --- bot/exts/evergreen/status_codes.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/status_codes.py b/bot/exts/evergreen/status_codes.py index ff2ac9e1..874c87eb 100644 --- a/bot/exts/evergreen/status_codes.py +++ b/bot/exts/evergreen/status_codes.py @@ -3,6 +3,9 @@ from http import HTTPStatus import discord from discord.ext import commands +HTTP_DOG_URL = "https://httpstatusdogs.com/img/{code}.jpg" +HTTP_CAT_URL = "https://http.cat/{code}.jpg" + class HTTPStatusCodes(commands.Cog): """Commands that give HTTP statuses described and visualized by cats and dogs.""" @@ -16,19 +19,17 @@ class HTTPStatusCodes(commands.Cog): if not ctx.invoked_subcommand: await ctx.send_help(ctx.command) - @http_status_group.command(aliases=['cat']) + @http_status_group.command(name='cat') async def http_cat(self, ctx: commands.Context, code: int) -> None: """Sends an embed with an image of a cat, portraying the status code.""" embed = discord.Embed(title=f'**Status: {code}**') + url = HTTP_CAT_URL.format(code=code) try: HTTPStatus(code) - async with self.bot.http_session.get( - f'https://http.cat/{code}.jpg', - allow_redirects=False - ) as response: + async with self.bot.http_session.get(url, allow_redirects=False) as response: if response.status != 404: - embed.set_image(url=f'https://http.cat/{code}.jpg') + embed.set_image(url=url) else: raise NotImplementedError @@ -41,19 +42,17 @@ class HTTPStatusCodes(commands.Cog): finally: await ctx.send(embed=embed) - @http_status_group.command(aliases=['dog']) + @http_status_group.command(name='dog') async def http_dog(self, ctx: commands.Context, code: int) -> None: """Sends an embed with an image of a dog, portraying the status code.""" embed = discord.Embed(title=f'**Status: {code}**') + url = HTTP_DOG_URL.format(code=code) try: HTTPStatus(code) - async with self.bot.http_session.get( - f'https://httpstatusdogs.com/img/{code}.jpg', - allow_redirects=False - ) as response: + async with self.bot.http_session.get(url, allow_redirects=False) as response: if response.status != 302: - embed.set_image(url=f'https://httpstatusdogs.com/img/{code}.jpg') + embed.set_image(url=url) else: raise NotImplementedError -- cgit v1.2.3 From 3d30a1ff9103fc59bd7fcc0ec8b1e7304beb08d4 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Fri, 5 Feb 2021 09:27:25 +0530 Subject: Abrupt response, so make the bot type --- bot/exts/evergreen/cheatsheet.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index f650e3b0..b3ee0b94 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -88,19 +88,20 @@ class CheatSheet(commands.Cog): ): 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) - - is_embed, description = self.result_fmt( - URL.format(search=quote_plus(" ".join(search_terms))), - result - ) - if is_embed: - await ctx.send(embed=description) - else: - await ctx.send(content=description) + async with ctx.typing(): + 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) + + is_embed, description = self.result_fmt( + URL.format(search=quote_plus(" ".join(search_terms))), + result + ) + if is_embed: + await ctx.send(embed=description) + else: + await ctx.send(content=description) def setup(bot: commands.Bot) -> None: -- cgit v1.2.3 From f6cccfe73321173d24667321b41f2e2b05f29aac Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Fri, 5 Feb 2021 09:28:28 +0530 Subject: Move repeated code to a single variable --- bot/exts/evergreen/cheatsheet.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index b3ee0b94..acd77e59 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -89,13 +89,15 @@ class CheatSheet(commands.Cog): return async with ctx.typing(): + search_string = quote_plus(" ".join(search_terms)) + async with self.bot.http_session.get( - URL.format(search=quote_plus(" ".join(search_terms))) + URL.format(search=search_string) ) as response: result = ANSI_RE.sub("", await response.text()).translate(ESCAPE_TT) is_embed, description = self.result_fmt( - URL.format(search=quote_plus(" ".join(search_terms))), + URL.format(search=search_string), result ) if is_embed: -- cgit v1.2.3 From 95950b93f5e01819921f9bc32a1613bdf1ddd13b Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Fri, 5 Feb 2021 09:30:10 +0530 Subject: Use brackets instead of / for multi line strings --- bot/exts/evergreen/cheatsheet.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index acd77e59..dd44c68e 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -58,14 +58,14 @@ class CheatSheet(commands.Cog): body_space = min(1986 - len(url), 1000) if len(body_text) > body_space: - description = f"**Result Of cht.sh**\n" \ - f"```python\n{body_text[:body_space]}\n" \ - f"... (truncated - too many lines)```\n" \ - f"Full results: {url} " + description = (f"**Result Of cht.sh**\n" + f"```python\n{body_text[:body_space]}\n" + f"... (truncated - too many lines)```\n" + f"Full results: {url} ") else: - description = f"**Result Of cht.sh**\n" \ - f"```python\n{body_text}```\n" \ - f"{url}" + description = (f"**Result Of cht.sh**\n" + f"```python\n{body_text}```\n" + f"{url}") return False, description @commands.command( -- cgit v1.2.3 From 23c0b2f9464b993c8f2fd33dc2941bef8b0a9851 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Fri, 5 Feb 2021 09:31:09 +0530 Subject: Fix indentation --- bot/exts/evergreen/cheatsheet.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index dd44c68e..97485365 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -59,13 +59,13 @@ class CheatSheet(commands.Cog): if len(body_text) > body_space: description = (f"**Result Of cht.sh**\n" - f"```python\n{body_text[:body_space]}\n" - f"... (truncated - too many lines)```\n" - f"Full results: {url} ") + f"```python\n{body_text[:body_space]}\n" + f"... (truncated - too many lines)```\n" + f"Full results: {url} ") else: description = (f"**Result Of cht.sh**\n" - f"```python\n{body_text}```\n" - f"{url}") + f"```python\n{body_text}```\n" + f"{url}") return False, description @commands.command( @@ -83,8 +83,8 @@ class CheatSheet(commands.Cog): --> .cht read json """ if not ( - ctx.channel.category.id == Categories.help_in_use - or ctx.channel.id in WHITELISTED_CHANNELS + ctx.channel.category.id == Categories.help_in_use + or ctx.channel.id in WHITELISTED_CHANNELS ): return -- cgit v1.2.3 From 3242cc6c1dba0a0ecf3b5c0cdf5b90a4784dbfd1 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 8 Feb 2021 05:51:14 +0530 Subject: Add curl User Agent Headers --- bot/exts/evergreen/cheatsheet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index 97485365..f6dce995 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -26,6 +26,7 @@ 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") +HEADERS = {'User-Agent': 'curl/7.68.0'} class CheatSheet(commands.Cog): @@ -92,7 +93,7 @@ class CheatSheet(commands.Cog): search_string = quote_plus(" ".join(search_terms)) async with self.bot.http_session.get( - URL.format(search=search_string) + URL.format(search=search_string), headers=HEADERS ) as response: result = ANSI_RE.sub("", await response.text()).translate(ESCAPE_TT) -- cgit v1.2.3 From 671be9942521aaa4915a358d0c1d4af13db94e3c Mon Sep 17 00:00:00 2001 From: Vivaan Parashar Date: Sun, 7 Feb 2021 13:57:28 +0530 Subject: Revert "Fixes Issue Matching Regex" This reverts commit 4239b1d07cd0bba543aca8e7c77cee1d8f437b14. --- bot/exts/evergreen/issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py index 73ebe547..72ca6de4 100644 --- a/bot/exts/evergreen/issues.py +++ b/bot/exts/evergreen/issues.py @@ -184,7 +184,7 @@ class Issues(commands.Cog): ): return - message_repo_issue_map = re.findall(fr"({self.repo_regex})#(\d+)", message.content) + message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content) links = [] if message_repo_issue_map: -- cgit v1.2.3 From af1d11067daf1304c3c166202c13d8373da9e9b7 Mon Sep 17 00:00:00 2001 From: Shivansh-007 Date: Mon, 8 Feb 2021 14:08:54 +0530 Subject: Add comment explaining why we need to pass curl as user agent --- bot/exts/evergreen/cheatsheet.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot') diff --git a/bot/exts/evergreen/cheatsheet.py b/bot/exts/evergreen/cheatsheet.py index f6dce995..a64ddd69 100644 --- a/bot/exts/evergreen/cheatsheet.py +++ b/bot/exts/evergreen/cheatsheet.py @@ -26,6 +26,7 @@ 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") +# We need to pass headers as curl otherwise it would default to aiohttp which would return raw html. HEADERS = {'User-Agent': 'curl/7.68.0'} -- cgit v1.2.3 From 2f032a71beadd6c9613b89d525004c98a4cf6ab9 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 9 Feb 2021 11:41:28 +0100 Subject: Add location mocks to the wolfram cog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the server IP address could be leaked using some query such as “what is the weather here?” or simply “what’s my ip?”. This aims to fix it by using mock locations. --- bot/exts/evergreen/wolfram.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/wolfram.py b/bot/exts/evergreen/wolfram.py index 898e8d2a..d3b4df5c 100644 --- a/bot/exts/evergreen/wolfram.py +++ b/bot/exts/evergreen/wolfram.py @@ -108,7 +108,10 @@ async def get_pod_pages(ctx: Context, bot: commands.Bot, query: str) -> Optional "input": query, "appid": APPID, "output": DEFAULT_OUTPUT_FORMAT, - "format": "image,plaintext" + "format": "image,plaintext", + "location": "the moon", + "latlong": "0.0,0.0", + "ip": "1.1.1.1" }) request_url = QUERY.format(request="query", data=url_str) -- cgit v1.2.3 From 87c4066eb016dace5d48d87fba83ce6424408878 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 9 Feb 2021 12:22:26 +0100 Subject: Apply mock locations to every subcommands --- bot/exts/evergreen/wolfram.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'bot') diff --git a/bot/exts/evergreen/wolfram.py b/bot/exts/evergreen/wolfram.py index d3b4df5c..437d9e1a 100644 --- a/bot/exts/evergreen/wolfram.py +++ b/bot/exts/evergreen/wolfram.py @@ -171,6 +171,9 @@ class Wolfram(Cog): url_str = parse.urlencode({ "i": query, "appid": APPID, + "location": "the moon", + "latlong": "0.0,0.0", + "ip": "1.1.1.1" }) query = QUERY.format(request="simple", data=url_str) @@ -251,6 +254,9 @@ class Wolfram(Cog): url_str = parse.urlencode({ "i": query, "appid": APPID, + "location": "the moon", + "latlong": "0.0,0.0", + "ip": "1.1.1.1" }) query = QUERY.format(request="result", data=url_str) -- cgit v1.2.3