From 42caf7fdfc7c5b7fc7a72039763d78a756bfdd44 Mon Sep 17 00:00:00 2001 From: Andi Qu <31325319+dolphingarlic@users.noreply.github.com> Date: Tue, 27 Apr 2021 12:56:35 +0200 Subject: Fixed the line limit and halved the char limit --- bot/exts/info/code_snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index c20115830..3f07e7193 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -234,7 +234,7 @@ class CodeSnippets(Cog): # Sorts the list of snippets by their match index and joins them into a single message message_to_send = '\n'.join(map(lambda x: x[1], sorted(all_snippets))) - if 0 < len(message_to_send) <= 2000 and len(all_snippets) <= 15: + if 0 < len(message_to_send) <= 1000 and message_to_send.count('\n') <= 15: await message.edit(suppress=True) await wait_for_deletion( await message.channel.send(message_to_send), -- cgit v1.2.3 From 9bf55faab91feea9a663d8110f5ab3a4c40ad837 Mon Sep 17 00:00:00 2001 From: Andi Qu <31325319+dolphingarlic@users.noreply.github.com> Date: Tue, 27 Apr 2021 13:21:38 +0200 Subject: Redirect to #bot-commands if the message's length is > 1000 --- bot/exts/info/code_snippets.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index 3f07e7193..d93ace31c 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -8,6 +8,7 @@ from discord import Message from discord.ext.commands import Cog from bot.bot import Bot +from bot.constants import Channels from bot.utils.messages import wait_for_deletion log = logging.getLogger(__name__) @@ -234,12 +235,22 @@ class CodeSnippets(Cog): # Sorts the list of snippets by their match index and joins them into a single message message_to_send = '\n'.join(map(lambda x: x[1], sorted(all_snippets))) - if 0 < len(message_to_send) <= 1000 and message_to_send.count('\n') <= 15: + if 0 < len(message_to_send) <= 2000 and message_to_send.count('\n') <= 15: await message.edit(suppress=True) - await wait_for_deletion( - await message.channel.send(message_to_send), - (message.author.id,) - ) + if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: + # Redirects to #bot-commands if the snippet contents are too long + await message.channel.send(('The snippet you tried to send was too long. Please ' + f'see <#{Channels.bot_commands}> for the full snippet.')) + bot_commands_channel = self.bot.get_channel(Channels.bot_commands) + await wait_for_deletion( + await bot_commands_channel.send(message_to_send), + (message.author.id,) + ) + else: + await wait_for_deletion( + await message.channel.send(message_to_send), + (message.author.id,) + ) def setup(bot: Bot) -> None: -- cgit v1.2.3 From c01caf42401b6d029014f90a52966ee55a649194 Mon Sep 17 00:00:00 2001 From: Andi Qu <31325319+dolphingarlic@users.noreply.github.com> Date: Tue, 27 Apr 2021 18:28:49 +0200 Subject: Wait for cache to fill before redirecting --- bot/exts/info/code_snippets.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index d93ace31c..6ebc5e74b 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -239,6 +239,7 @@ class CodeSnippets(Cog): await message.edit(suppress=True) if len(message_to_send) > 1000 and message.channel.id != Channels.bot_commands: # Redirects to #bot-commands if the snippet contents are too long + await self.bot.wait_until_guild_available() await message.channel.send(('The snippet you tried to send was too long. Please ' f'see <#{Channels.bot_commands}> for the full snippet.')) bot_commands_channel = self.bot.get_channel(Channels.bot_commands) -- cgit v1.2.3 From 197279d7045536d520db91f49db8d04c23fea090 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 28 Apr 2021 22:20:22 -0700 Subject: CodeSnippets: avoid returning None when request raises an exception Move the exception handling to `on_message` to avoid writing a lot of None checks; `_fetch_response` is used multiple times in various places. Fixes #1554 Fixes BOT-Z7 --- bot/exts/info/code_snippets.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index c20115830..b552efcea 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -45,14 +45,11 @@ class CodeSnippets(Cog): async def _fetch_response(self, url: str, response_format: str, **kwargs) -> str: """Makes http requests using aiohttp.""" - try: - async with self.bot.http_session.get(url, raise_for_status=True, **kwargs) as response: - if response_format == 'text': - return await response.text() - elif response_format == 'json': - return await response.json() - except ClientResponseError as error: - log.error(f'Failed to fetch code snippet from {url}. HTTP Status: {error.status}. Message: {str(error)}.') + async with self.bot.http_session.get(url, raise_for_status=True, **kwargs) as response: + if response_format == 'text': + return await response.text() + elif response_format == 'json': + return await response.json() def _find_ref(self, path: str, refs: tuple) -> tuple: """Loops through all branches and tags to find the required ref.""" @@ -228,8 +225,14 @@ class CodeSnippets(Cog): for pattern, handler in self.pattern_handlers: for match in pattern.finditer(message.content): - snippet = await handler(**match.groupdict()) - all_snippets.append((match.start(), snippet)) + try: + snippet = await handler(**match.groupdict()) + all_snippets.append((match.start(), snippet)) + except ClientResponseError as error: + log.error( + f'Failed to fetch code snippet from {error.request_info.real_url}. ' + f'Status: {error.status}. Message: {error}.' + ) # Sorts the list of snippets by their match index and joins them into a single message message_to_send = '\n'.join(map(lambda x: x[1], sorted(all_snippets))) -- cgit v1.2.3 From 742618f71763184ad679f86f9a2bfb6419ec8646 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 28 Apr 2021 22:20:39 -0700 Subject: CodeSnippets: use a lower log level for 404 responses Just cause a URL looks valid doesn't mean it will be valid, so a 404 is a normal and harmless error. Fixes #1553 Fixes BOT-Z4 Fixes BOT-Z8 Fixes BOT-Z9 --- bot/exts/info/code_snippets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index b552efcea..5c6cb5ae2 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -229,7 +229,8 @@ class CodeSnippets(Cog): snippet = await handler(**match.groupdict()) all_snippets.append((match.start(), snippet)) except ClientResponseError as error: - log.error( + log.log( + logging.DEBUG if error.status == 404 else logging.ERROR, f'Failed to fetch code snippet from {error.request_info.real_url}. ' f'Status: {error.status}. Message: {error}.' ) -- cgit v1.2.3 From 1d5a01b2c9e49ed3a24f8630374dc63a703a2187 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 28 Apr 2021 22:36:25 -0700 Subject: CodeSnippets: add more detail to the request error message --- bot/exts/info/code_snippets.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index 5c6cb5ae2..3c8b862c3 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -229,10 +229,11 @@ class CodeSnippets(Cog): snippet = await handler(**match.groupdict()) all_snippets.append((match.start(), snippet)) except ClientResponseError as error: + error_message = error.message # noqa: B306 log.log( logging.DEBUG if error.status == 404 else logging.ERROR, - f'Failed to fetch code snippet from {error.request_info.real_url}. ' - f'Status: {error.status}. Message: {error}.' + f'Failed to fetch code snippet from {match[0]!r}: {error.status} ' + f'{error_message} for GET {error.request_info.real_url.human_repr()}' ) # Sorts the list of snippets by their match index and joins them into a single message -- cgit v1.2.3 From 41b5c2409fff548545e463759f22500e9244b375 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 28 Apr 2021 22:43:51 -0700 Subject: CodeSnippets: fix type annotations --- bot/exts/info/code_snippets.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py index 3c8b862c3..3b2a8a0a5 100644 --- a/bot/exts/info/code_snippets.py +++ b/bot/exts/info/code_snippets.py @@ -1,6 +1,7 @@ import logging import re import textwrap +from typing import Any from urllib.parse import quote_plus from aiohttp import ClientResponseError @@ -43,7 +44,7 @@ class CodeSnippets(Cog): Matches each message against a regex and prints the contents of all matched snippets. """ - async def _fetch_response(self, url: str, response_format: str, **kwargs) -> str: + async def _fetch_response(self, url: str, response_format: str, **kwargs) -> Any: """Makes http requests using aiohttp.""" async with self.bot.http_session.get(url, raise_for_status=True, **kwargs) as response: if response_format == 'text': @@ -61,7 +62,7 @@ class CodeSnippets(Cog): ref = possible_ref['name'] file_path = path[len(ref) + 1:] break - return (ref, file_path) + return ref, file_path async def _fetch_github_snippet( self, @@ -145,8 +146,8 @@ class CodeSnippets(Cog): repo: str, ref: str, file_path: str, - start_line: int, - end_line: int + start_line: str, + end_line: str ) -> str: """Fetches a snippet from a BitBucket repo.""" file_contents = await self._fetch_response( -- cgit v1.2.3