aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Andi Qu <[email protected]>2020-10-27 15:55:34 +0200
committerGravatar Andi Qu <[email protected]>2020-10-27 15:55:34 +0200
commit3102c698e8892d5a3b1b0fcc2183bf2c480d60fd (patch)
treeab124cc2ea50da73cc0cbf2c8facfaeb36e14e90
parentMoved global functions into the cog and got rid of unnecessary aiohttp sessions (diff)
Used a list of tuples for on_message instead
-rw-r--r--bot/exts/info/code_snippets.py29
1 files changed, 11 insertions, 18 deletions
diff --git a/bot/exts/info/code_snippets.py b/bot/exts/info/code_snippets.py
index 12eb692d4..1bb00b677 100644
--- a/bot/exts/info/code_snippets.py
+++ b/bot/exts/info/code_snippets.py
@@ -199,25 +199,18 @@ class CodeSnippets(Cog):
@Cog.listener()
async def on_message(self, message: Message) -> None:
"""Checks if the message has a snippet link, removes the embed, then sends the snippet contents."""
- gh_match = GITHUB_RE.search(message.content)
- gh_gist_match = GITHUB_GIST_RE.search(message.content)
- gl_match = GITLAB_RE.search(message.content)
- bb_match = BITBUCKET_RE.search(message.content)
-
- if (gh_match or gh_gist_match or gl_match or bb_match) and not message.author.bot:
+ if not message.author.bot:
message_to_send = ''
-
- for gh in GITHUB_RE.finditer(message.content):
- message_to_send += await self._fetch_github_snippet(**gh.groupdict())
-
- for gh_gist in GITHUB_GIST_RE.finditer(message.content):
- message_to_send += await self._fetch_github_gist_snippet(**gh_gist.groupdict())
-
- for gl in GITLAB_RE.finditer(message.content):
- message_to_send += await self._fetch_gitlab_snippet(**gl.groupdict())
-
- for bb in BITBUCKET_RE.finditer(message.content):
- message_to_send += await self._fetch_bitbucket_snippet(**bb.groupdict())
+ pattern_handlers = [
+ (GITHUB_RE, self._fetch_github_snippet),
+ (GITHUB_GIST_RE, self._fetch_github_gist_snippet),
+ (GITLAB_RE, self._fetch_gitlab_snippet),
+ (BITBUCKET_RE, self._fetch_bitbucket_snippet)
+ ]
+
+ for pattern, handler in pattern_handlers:
+ for match in pattern.finditer(message.content):
+ message_to_send += await handler(**match.groupdict())
if 0 < len(message_to_send) <= 2000 and message_to_send.count('\n') <= 15:
await message.edit(suppress=True)