Skip to content

Commit

Permalink
Prevent unsanitized queries from creating Cloudflare errors (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 authored Nov 2, 2024
1 parent 5570703 commit 51e8d02
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions bot/cogs/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from typing import TYPE_CHECKING, Any, Optional

import aiohttp
import discord
import msgspec
from discord import app_commands
Expand Down Expand Up @@ -301,6 +302,18 @@ def determine_author(self, author: Optional[str]) -> str:
author_link = str(BASE_URL / f"@{author}")
return f"[{author}]({author_link})"

async def _handle_invalid_response(
self, interaction: discord.Interaction, error: app_commands.AppCommandError
) -> None:
if (
isinstance(error, app_commands.CommandInvokeError)
and isinstance(error.original, aiohttp.ContentTypeError)
and error.original.status == 403
):
await interaction.response.send_message(
"Unable to validate forbidden query"
)

@app_commands.command(name="terms")
@app_commands.describe(query="The term to look for")
async def terms(
Expand Down Expand Up @@ -362,6 +375,26 @@ async def inclusive(
pages = InclusivePages(entries=data, interaction=interaction)
await pages.start()

### Error Handlers

@terms.error
async def on_terms_error(
self, interaction: discord.Interaction, error: app_commands.AppCommandError
) -> None:
await self._handle_invalid_response(interaction, error)

@nouns.error
async def on_nouns_error(
self, interaction: discord.Interaction, error: app_commands.AppCommandError
) -> None:
await self._handle_invalid_response(interaction, error)

@inclusive.error
async def on_inclusive_error(
self, interaction: discord.Interaction, error: app_commands.AppCommandError
) -> None:
await self._handle_invalid_response(interaction, error)


async def setup(bot: Catherine) -> None:
await bot.add_cog(Dictionary(bot))
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Use proper type checking imports (https://github.com/No767/Catherine-Chan/pull/216)
- Fix exception violations with pronouns tester (https://github.com/No767/Catherine-Chan/pull/212)
- Migrate from Bandit, Black, Autoflake and Isort to Ruff (https://github.com/No767/Catherine-Chan/pull/218, https://github.com/No767/Catherine-Chan/pull/223)
- Prevent unsanitized queries from creating Cloudflare errors (https://github.com/No767/Catherine-Chan/pull/226)

## ✨ Additions

Expand Down

0 comments on commit 51e8d02

Please sign in to comment.