aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/evergreen/issues.py
blob: 840d9ead9e93cd17cdb01d0e0f97b432bb4abb6f (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
import logging

import discord
from discord.ext import commands

log = logging.getLogger(__name__)


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

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

    @commands.command(aliases=("issues",))
    async def issue(self, ctx, number: int, repository: str = "seasonalbot", user: str = "python-discord"):
        """Command to retrieve issues from a GitHub repository."""
        url = f"https://api.github.com/repos/{user}/{repository}/issues/{str(number)}"
        status = {"404": f"Issue #{str(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(url) as r:
            json_data = await r.json()

        if str(r.status) in status:
            return await ctx.send(status.get(str(r.status)))

        valid = discord.Embed(colour=0x00ff37)
        valid.add_field(name="Repository", value=f"{user}/{repository}", inline=False)
        valid.add_field(name="Issue Number", value=f"#{str(number)}", inline=False)
        valid.add_field(name="Status", value=json_data.get("state").title())
        valid.add_field(name="Link", value=url, inline=False)
        if len(json_data.get("body")) < 1024:
            valid.add_field(name="Description", value=json_data.get("body"), inline=False)
        await ctx.send(embed=valid)


def setup(bot):
    """Github Issues Cog Load."""
    bot.add_cog(Issues(bot))
    log.info("Issues cog loaded")