aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/evergreen/issues.py
blob: 438ab475eac949c761efea29d7f0e23876d6657e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging

import discord
from discord.ext import commands

from bot.constants import Colours
from bot.decorators import override_in_channel

log = logging.getLogger(__name__)


class Issues(commands.Cog):
    """Cog that allows users to retrieve issues from GitHub."""

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

    @commands.command(aliases=("issues",))
    @override_in_channel()
    async def issue(
        self, ctx: commands.Context, number: int, repository: str = "seasonalbot", user: str = "python-discord"
    ) -> None:
        """Command to retrieve issues from a GitHub repository."""
        api_url = f"https://api.github.com/repos/{user}/{repository}/issues/{number}"
        failed_status = {
            404: f"Issue #{number} doesn't exist in the repository {user}/{repository}.",
            403: f"Rate limit exceeded. Please wait a while before trying again!"
        }

        async with self.bot.http_session.get(api_url) as r:
            json_data = await r.json()
            response_code = r.status

        if response_code in failed_status:
            return await ctx.send(failed_status[response_code])

        repo_url = f"https://github.com/{user}/{repository}"
        issue_embed = discord.Embed(colour=Colours.bright_green)
        issue_embed.add_field(name="Repository", value=f"[{user}/{repository}]({repo_url})", inline=False)
        issue_embed.add_field(name="Issue Number", value=f"#{number}", inline=False)
        issue_embed.add_field(name="Status", value=json_data["state"].title())
        issue_embed.add_field(name="Link", value=json_data["html_url"], inline=False)

        description = json_data["body"]
        if len(description) > 1024:
            placeholder = " [...]"
            description = f"{description[:1024 - len(placeholder)]}{placeholder}"

        issue_embed.add_field(name="Description", value=description, inline=False)

        await ctx.send(embed=issue_embed)


def setup(bot: commands.Bot) -> None:
    """Github Issues Cog Load."""
    bot.add_cog(Issues(bot))
    log.info("Issues cog loaded")