Skip to content

Commit 94e5433

Browse files
committed
refactor(wiki): enhance language code matching and interaction handling
- Updated the language pattern matching to only include known translation language markers, improving accuracy in identifying translated pages. - Modified interaction handling to defer responses early for slash commands, ensuring a better user experience during API calls.
1 parent aaac66c commit 94e5433

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/tux/modules/utility/wiki.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,14 @@ async def query_wiki(self, base_url: str, search_term: str) -> tuple[str, str]:
100100
"srlimit": 50, # Get more results to find English ones
101101
}
102102

103-
# Pattern to match language codes in parentheses at the start of title or after a slash
103+
# Pattern to match known language codes in parentheses at the start of title
104104
# ArchWiki uses format like "Page Name (Language)/Subpage" for translated pages
105-
# Matches patterns like "(Italiano)", "(Español)", "(Français)" etc.
106-
# Only matches if it appears early in the title (before first slash or in first 50 chars)
107-
language_pattern = re.compile(r"^[^/]*\([^)]+\)")
105+
# Only matches known translation language markers to avoid false negatives
106+
known_languages = (
107+
"Italiano|Español|Français|Deutsch|Português|Русский|日本語|中文|"
108+
"한국어|Polski|Türkçe|العربية|فارسی|हिन्दी"
109+
)
110+
language_pattern = re.compile(rf"^[^/]*\(({known_languages})\)")
108111

109112
try:
110113
# Send a GET request to the wiki API
@@ -179,8 +182,9 @@ async def arch_wiki(self, ctx: commands.Context[Tux], query: str) -> None:
179182
query : str
180183
The search query.
181184
"""
182-
# Defer early to acknowledge interaction before async work
183-
await ctx.defer(ephemeral=True)
185+
# Defer early to acknowledge interaction before async work (slash commands only)
186+
if ctx.interaction:
187+
await ctx.defer(ephemeral=True)
184188

185189
title: tuple[str, str] = await self.query_wiki(self.arch_wiki_api_url, query)
186190

@@ -205,8 +209,9 @@ async def atl_wiki(self, ctx: commands.Context[Tux], query: str) -> None:
205209
query : str
206210
The search query.
207211
"""
208-
# Defer early to acknowledge interaction before async work
209-
await ctx.defer(ephemeral=True)
212+
# Defer early to acknowledge interaction before async work (slash commands only)
213+
if ctx.interaction:
214+
await ctx.defer(ephemeral=True)
210215

211216
title: tuple[str, str] = await self.query_wiki(self.atl_wiki_api_url, query)
212217

0 commit comments

Comments
 (0)