Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cogs/rtfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import asyncpg # type: ignore # rtfs
import discord
import httpx
from discord import app_commands, ui # type: ignore # rtfs
from discord.ext import commands, tasks # type: ignore # rtfs
from jishaku.codeblocks import Codeblock, codeblock_converter
Expand Down Expand Up @@ -143,6 +144,12 @@ class RTFX(commands.Cog):

def __init__(self, bot: Ayaka):
self.bot = bot
# aiohttp did something in 3.10 that RTD's Cloudflare protection doesn't
# like, so we use httpx for this.
self.httpx = httpx.AsyncClient()

async def cog_unload(self) -> None:
await self.httpx.aclose()

@property
def display_emoji(self) -> discord.PartialEmoji:
Expand Down Expand Up @@ -207,13 +214,13 @@ def parse_object_inv(self, stream: SphinxObjectFileReader, url: str) -> dict[str
async def build_rtfm_lookup_table(self) -> None:
cache = {}
for key, page in RTFM_PAGES.items():
_ = cache[key] = {}
async with self.bot.session.get(page + '/objects.inv') as resp:
if resp.status != 200:
raise RuntimeError('Cannot build rtfm lookup table, try again later.')

stream = SphinxObjectFileReader(await resp.read())
cache[key] = self.parse_object_inv(stream, page)
resp = await self.httpx.get(f'{page}/objects.inv')
if resp.status_code != 200:
raise RuntimeError('Cannot build rtfm lookup table, try again later.')
cache[key] = {}
stream = SphinxObjectFileReader(await resp.aread())
await resp.aclose()
cache[key] = self.parse_object_inv(stream, page)
self._rtfm_cache = cache

async def do_rtfm(self, ctx: Context, key: str, obj: str | None) -> None:
Expand Down
Loading
Loading