From f40a9bcc9e24edb44050eee6b6ffb9485e3efab4 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:45:06 +0700 Subject: Add RFC utility command --- bot/exts/utilities/rfc.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 bot/exts/utilities/rfc.py (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py new file mode 100644 index 00000000..bb0771b9 --- /dev/null +++ b/bot/exts/utilities/rfc.py @@ -0,0 +1,65 @@ +import logging +import datetime + +from discord.ext import commands +from discord import Embed + +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): + """Load the Rfc cog.""" + await bot.add_cog(Rfc(bot)) -- cgit v1.2.3 From b87186e100677e207fe911fa513e6c47e4a6c42a Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:46:32 +0700 Subject: Fixed ruff issues --- bot/exts/utilities/rfc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index bb0771b9..2a31c6be 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -60,6 +60,6 @@ class Rfc(commands.Cog): await ctx.send(embed=embed) -async def setup(bot: Bot): +async def setup(bot: Bot) -> None: """Load the Rfc cog.""" await bot.add_cog(Rfc(bot)) -- cgit v1.2.3 From 3a9be593adeb56b3d4aa4b34d178189e1e777b0f Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:53:46 +0700 Subject: Applied `isort` to `rfc.py` --- bot/exts/utilities/rfc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index 2a31c6be..78f88a83 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -1,8 +1,8 @@ -import logging import datetime +import logging -from discord.ext import commands from discord import Embed +from discord.ext import commands from bot.bot import Bot from bot.constants import Colours -- cgit v1.2.3 From f19a5ee67dfaa5b5343fb1f02259540767267de1 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:35:32 +0700 Subject: Added cache to RFC command --- bot/exts/utilities/rfc.py | 97 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 31 deletions(-) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index 78f88a83..bbe6efc4 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -1,6 +1,7 @@ import datetime import logging +import pydantic from discord import Embed from discord.ext import commands @@ -9,55 +10,89 @@ from bot.constants import Colours logger = logging.getLogger(__name__) -BASE_URL = "https://datatracker.ietf.org/doc/rfc{query}/doc.json" +BASE_URL = "https://datatracker.ietf.org/doc/rfc{rfc_id}/doc.json" + + +class RfcDocument(pydantic.BaseModel): + """Represents an RFC document.""" + + title: str + description: str + revisions: str + created: datetime.datetime class Rfc(commands.Cog): - """Retrieve RFCs from their ID.""" + """Retrieves RFCs by their ID.""" def __init__(self, bot: Bot): self.bot = bot + self.cache: dict[int, RfcDocument] = {} - @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, - ) + async def retrieve_data(self, rfc_id: int) -> RfcDocument | None: + """Retrieves the RFC from the cache or API, and adds to the cache if it does not exist.""" + if rfc_id in self.cache: + return self.cache[rfc_id] - await ctx.send(embed=error) - return + async with self.bot.http_session.get(BASE_URL.format(rfc_id=rfc_id)) as resp: + if resp.status != 200: + return None data = await resp.json() - description = ( - data["abstract"] - or f"[Link](https://datatracker.ietf.org/doc/rfc{query})" - ) - title = data["title"] + description = ( + data["abstract"] or f"[Link](https://datatracker.ietf.org/doc/rfc{rfc_id})" + ) + + revisions = data["rev"] or len(data["rev_history"]) + + raw_date = data["rev_history"][0]["published"] + creation_date = datetime.datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%S%z") + document = RfcDocument( + title=data["title"], + description=description, + revisions=revisions, + created=creation_date, + ) + + self.cache[rfc_id] = document + + return document + + @commands.cooldown(1, 10, commands.BucketType.user) + @commands.command() + async def rfc(self, ctx: commands.Context, rfc_id: int) -> None: + """Sends the corresponding RFC with the given ID.""" + document = await self.retrieve_data(rfc_id) + + if not document: embed = Embed( - title=f"RFC {query} - {title}", - description=description, - colour=Colours.gold, + title="RFC not found", + description=f"RFC {rfc_id} does not exist.", + colour=Colours.soft_red, ) - embed.add_field( - name="Current Revision", - value=data["rev"] or len(data["rev_history"]), - ) + await ctx.send(embed=embed) - created = data["rev_history"][0]["published"] - created = datetime.datetime.strptime(created, "%Y-%m-%dT%H:%M:%S%z") + return - embed.add_field(name="Created", value=created.strftime("%Y-%m-%d")) + embed = Embed( + title=f"RFC {rfc_id} - {document.title}", + description=document.description, + colour=Colours.gold, + ) - await ctx.send(embed=embed) + embed.add_field( + name="Current Revision", + value=document.revisions, + ) + + embed.add_field( + name="Created", + value=document.created.strftime("%Y-%m-%d"), + ) + await ctx.send(embed=embed) async def setup(bot: Bot) -> None: -- cgit v1.2.3 From 3fce0bc148b7fdc00c2aed725e2dfc277a6363e8 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 11 Aug 2023 07:33:37 +0700 Subject: Added logging on fetching RFC --- bot/exts/utilities/rfc.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index bbe6efc4..b8a61052 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -77,6 +77,8 @@ class Rfc(commands.Cog): return + logger.info(f"Fetching RFC {rfc_id}") + embed = Embed( title=f"RFC {rfc_id} - {document.title}", description=document.description, -- cgit v1.2.3 From 27f8ef2227576bcfbb686f3cd94ff2fc47735d30 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Fri, 18 Aug 2023 20:49:55 +0700 Subject: Added URL to RFC embed --- bot/exts/utilities/rfc.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index b8a61052..8dbee03a 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -10,7 +10,8 @@ from bot.constants import Colours logger = logging.getLogger(__name__) -BASE_URL = "https://datatracker.ietf.org/doc/rfc{rfc_id}/doc.json" +API_URL = "https://datatracker.ietf.org/doc/rfc{rfc_id}/doc.json" +DOCUMENT_URL = "https://datatracker.ietf.org/doc/rfc{rfc_id}" class RfcDocument(pydantic.BaseModel): @@ -34,20 +35,22 @@ class Rfc(commands.Cog): if rfc_id in self.cache: return self.cache[rfc_id] - async with self.bot.http_session.get(BASE_URL.format(rfc_id=rfc_id)) as resp: + async with self.bot.http_session.get(API_URL.format(rfc_id=rfc_id)) as resp: if resp.status != 200: return None data = await resp.json() description = ( - data["abstract"] or f"[Link](https://datatracker.ietf.org/doc/rfc{rfc_id})" + data["abstract"] + or f"[Link]({DOCUMENT_URL.format(rfc_id=rfc_id)}" ) revisions = data["rev"] or len(data["rev_history"]) raw_date = data["rev_history"][0]["published"] - creation_date = datetime.datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%S%z") + creation_date = datetime.datetime.strptime( + raw_date, "%Y-%m-%dT%H:%M:%S%z") document = RfcDocument( title=data["title"], @@ -83,6 +86,7 @@ class Rfc(commands.Cog): title=f"RFC {rfc_id} - {document.title}", description=document.description, colour=Colours.gold, + url=DOCUMENT_URL.format(rfc_id=rfc_id), ) embed.add_field( -- cgit v1.2.3 From 6cff59d144d7b6902ca577030439f83cf8bea1b5 Mon Sep 17 00:00:00 2001 From: SomeHybrid <91648368+SomeHybrid@users.noreply.github.com> Date: Wed, 23 Aug 2023 19:55:43 +0700 Subject: Changed cooldown to 5s, removed description if abstract not found --- bot/exts/utilities/rfc.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'bot/exts/utilities/rfc.py') diff --git a/bot/exts/utilities/rfc.py b/bot/exts/utilities/rfc.py index 8dbee03a..d9840592 100644 --- a/bot/exts/utilities/rfc.py +++ b/bot/exts/utilities/rfc.py @@ -41,16 +41,12 @@ class Rfc(commands.Cog): data = await resp.json() - description = ( - data["abstract"] - or f"[Link]({DOCUMENT_URL.format(rfc_id=rfc_id)}" - ) + description = data["abstract"] revisions = data["rev"] or len(data["rev_history"]) raw_date = data["rev_history"][0]["published"] - creation_date = datetime.datetime.strptime( - raw_date, "%Y-%m-%dT%H:%M:%S%z") + creation_date = datetime.datetime.strptime(raw_date, "%Y-%m-%dT%H:%M:%S%z") document = RfcDocument( title=data["title"], @@ -63,7 +59,7 @@ class Rfc(commands.Cog): return document - @commands.cooldown(1, 10, commands.BucketType.user) + @commands.cooldown(1, 5, commands.BucketType.user) @commands.command() async def rfc(self, ctx: commands.Context, rfc_id: int) -> None: """Sends the corresponding RFC with the given ID.""" -- cgit v1.2.3