aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/exts/evergreen/issues.py26
1 files changed, 17 insertions, 9 deletions
diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py
index 946da354..d6790edf 100644
--- a/bot/exts/evergreen/issues.py
+++ b/bot/exts/evergreen/issues.py
@@ -2,6 +2,7 @@ import logging
import random
import re
import typing as t
+from enum import Enum
import discord
from discord.ext import commands, tasks
@@ -17,12 +18,18 @@ BAD_RESPONSE = {
}
MAX_REQUESTS = 10
REQUEST_HEADERS = dict()
-PYDIS_REPOS = "https://api.github.com/orgs/python-discord/repos"
+PYTHON_DISCORD_REPOS = "https://api.github.com/orgs/{repo}/repos"
if GITHUB_TOKEN := Tokens.github:
REQUEST_HEADERS["Authorization"] = f"token {GITHUB_TOKEN}"
+class FetchIssueErrors(Enum):
+ value_error = "Numbers not found."
+ max_requests = "Max requests hit."
+
+
+
class Issues(commands.Cog):
"""Cog that allows users to retrieve issues from GitHub."""
@@ -34,11 +41,12 @@ class Issues(commands.Cog):
@tasks.loop(minutes=30)
async def get_pydis_repos(self) -> None:
"""Get all python-discord repositories on github."""
- async with self.bot.http_session.get(PYDIS_REPOS) as resp:
+ async with self.bot.http_session.get(PYTHON_DISCORD_REPOS.format(repo="python-discord")) as resp:
if resp.status == 200:
data = await resp.json()
for repo in data:
self.repos.append(repo["full_name"].split("/")[1])
+ self.repo_regex = "|".join(repo for repo in self.repos)
else:
log.debug(f"Failed to get latest Pydis repositories. Status code {resp.status}")
@@ -66,10 +74,10 @@ class Issues(commands.Cog):
"""Retrieve issue(s) from a GitHub repository."""
links = []
if not numbers:
- return "Numbers not found."
+ return FetchIssueErrors.value_error
if len(numbers) > MAX_REQUESTS:
- return "Max requests hit."
+ return FetchIssueErrors.max_requests
for number in numbers:
url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}"
@@ -135,10 +143,10 @@ class Issues(commands.Cog):
"""Command to retrieve issue(s) from a GitHub repository."""
result = await self.fetch_issues(set(numbers), repository, user)
- if result == "Numbers not found.":
+ if result == FetchIssueErrors.value_error:
await ctx.invoke(self.bot.get_command('help'), 'issue')
- elif result == "Max requests hit.":
+ elif result == FetchIssueErrors.max_requests:
embed = discord.Embed(
title=random.choice(ERROR_REPLIES),
color=Colours.soft_red,
@@ -155,10 +163,10 @@ class Issues(commands.Cog):
await ctx.send(result)
@commands.Cog.listener()
+ @override_in_channel(WHITELISTED_CHANNELS + (Channels.dev_contrib, Channels.dev_branding))
async def on_message(self, message: discord.Message) -> None:
"""Command to retrieve issue(s) from a GitHub repository using automatic linking if matching <repo>#<issue>."""
- repo_regex = "|".join(repo for repo in self.repos)
- message_repo_issue_map = re.findall(fr".+?({repo_regex})#(\d+)", message.content)
+ message_repo_issue_map = re.findall(fr".+?({self.repo_regex})#(\d+)", message.content)
links = []
if message_repo_issue_map:
@@ -168,7 +176,7 @@ class Issues(commands.Cog):
else:
result = await self.fetch_issues({repo_issue[1]}, repo_issue[0], "python-discord")
if isinstance(result, list):
- links.append(*result)
+ links.extend(result)
if not links:
return