feat(seautils): add [p]rfc command

This commit is contained in:
Seaswimmer 2024-05-28 20:20:21 -04:00
parent 2886d5e80d
commit a641cae640
Signed by untrusted user: cswimr
GPG key ID: 5D671B5D03D65A7F
4 changed files with 67 additions and 2 deletions

View file

@ -8,5 +8,6 @@
"hidden": true,
"disabled": false,
"min_bot_version": "3.5.0",
"min_python_version": [3, 8, 0]
"min_python_version": [3, 8, 0],
"requirements": ["beautifulsoup4"]
}

View file

@ -12,7 +12,9 @@ from asyncio.subprocess import Process
from functools import partial, partialmethod
from typing import Any
import aiohttp
import yaml
from bs4 import BeautifulSoup
from discord import Color, Embed, app_commands
from discord.utils import CachedSlotProperty, cached_property
from redbot.core import commands
@ -185,3 +187,32 @@ class SeaUtils(commands.Cog):
await ctx.send(content= warning + cf.box(text=ns_stdout.decode()))
except (FileNotFoundError):
await ctx.maybe_send_embed(message=cf.error("Neither `dig` nor `nslookup` are installed on the system. Unable to resolve DNS query."))
async def get_results(self, ctx: commands.Context, soup: BeautifulSoup) -> list:
pre_tags = soup.find_all('pre')
content = []
for pre_tag in pre_tags:
if await ctx.embed_requested():
embed = Embed(
title="RFC Document",
description=pre_tag.text,
color=await ctx.embed_color()
)
content.append(embed)
else:
content.append(pre_tag.text)
return content
@commands.command()
async def rfc(self, ctx: commands.Context, number: int) -> None:
"""Retrieve the text of an RFC document."""
url = f"https://www.rfc-editor.org/rfc/rfc{number}.html"
async with aiohttp.ClientSession() as session:
async with session.get(url=url) as response:
if response.status == 200:
html = await response.text()
soup = BeautifulSoup(html, 'html.parser')
content = await self.get_results(ctx, soup)
await SimpleMenu(pages=content, disable_after_timeout=True, timeout=300).start(ctx)
else:
await ctx.maybe_send_embed(content=cf.error(f"An error occurred while fetching RFC {number}. Status code: {response.status}."))