aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/utilities/rfc.py
blob: 78f88a83da54bbb7a9dee5ae2b2b79744b02996d (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
58
59
60
61
62
63
64
65
import datetime
import logging

from discord import Embed
from discord.ext import commands

from bot.bot import Bot
from bot.constants import Colours

logger = logging.getLogger(__name__)

BASE_URL = "https://datatracker.ietf.org/doc/rfc{query}/doc.json"


class Rfc(commands.Cog):
    """Retrieve RFCs from their ID."""

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

    @commands.cooldown(1, 10, commands.BucketType.user)
    @commands.command()
    async def rfc(self, ctx: commands.Context, query: int) -> None:
        """Sends the corresponding RFC with the given ID."""
        async with self.bot.http_session.get(BASE_URL.format(query=query)) as resp:
            if resp.status != 200:
                error = Embed(
                    title="RFC not found",
                    description=f"RFC {query} was not found.",
                    colour=Colours.soft_red,
                )

                await ctx.send(embed=error)
                return

            data = await resp.json()

            description = (
                data["abstract"]
                or f"[Link](https://datatracker.ietf.org/doc/rfc{query})"
            )
            title = data["title"]

            embed = Embed(
                title=f"RFC {query} - {title}",
                description=description,
                colour=Colours.gold,
            )

            embed.add_field(
                name="Current Revision",
                value=data["rev"] or len(data["rev_history"]),
            )

            created = data["rev_history"][0]["published"]
            created = datetime.datetime.strptime(created, "%Y-%m-%dT%H:%M:%S%z")

            embed.add_field(name="Created", value=created.strftime("%Y-%m-%d"))

            await ctx.send(embed=embed)


async def setup(bot: Bot) -> None:
    """Load the Rfc cog."""
    await bot.add_cog(Rfc(bot))