Skip to content

feat(wiki): Add subcommand to search wikipedia #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions tux/cogs/utility/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
self.bot = bot
self.arch_wiki_api_url = "https://wiki.archlinux.org/api.php"
self.atl_wiki_api_url = "https://atl.wiki/api.php"
self.wikipedia_api_url = "https://en.wikipedia.org/w/api.php"

Check warning on line 16 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L16

Added line #L16 was not covered by tests
self.wiki.usage = generate_usage(self.wiki)
self.arch_wiki.usage = generate_usage(self.arch_wiki)
self.atl_wiki.usage = generate_usage(self.atl_wiki)
self.wikipedia.usage = generate_usage(self.wikipedia)

Check warning on line 20 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L20

Added line #L20 was not covered by tests

def create_embed(self, title: tuple[str, str], ctx: commands.Context[Tux]) -> discord.Embed:
"""
Expand Down Expand Up @@ -90,6 +92,8 @@
url_title = title.replace(" ", "_")
if "atl.wiki" in base_url:
url = f"https://atl.wiki/{url_title}"
elif "wikipedia.org" in base_url:
url = f"https://en.wikipedia.org/wiki/{url_title}"

Check warning on line 96 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L96

Added line #L96 was not covered by tests
else:
url = f"https://wiki.archlinux.org/title/{url_title}"
return title, url
Expand Down Expand Up @@ -155,6 +159,28 @@

await ctx.send(embed=embed)

@wiki.command(

Check warning on line 162 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L162

Added line #L162 was not covered by tests
name="wikipedia",
aliases=["wp"],
)
async def wikipedia(self, ctx: commands.Context[Tux], *, query: str) -> None:

Check warning on line 166 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L166

Added line #L166 was not covered by tests
"""
Search Wikipedia

Parameters
----------
ctx : commands.Context[Tux]
The context object for the command.
query : str
The search query.
"""

title: tuple[str, str] = self.query_wiki(self.wikipedia_api_url, query)

Check warning on line 178 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L178

Added line #L178 was not covered by tests

embed = self.create_embed(title, ctx)

Check warning on line 180 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L180

Added line #L180 was not covered by tests

await ctx.send(embed=embed)

Check warning on line 182 in tux/cogs/utility/wiki.py

View check run for this annotation

Codecov / codecov/patch

tux/cogs/utility/wiki.py#L182

Added line #L182 was not covered by tests
Comment on lines +166 to +182
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: No error handling for failed or empty Wikipedia queries.

Add error handling to manage cases where no Wikipedia article is found or the query fails, to prevent incomplete or misleading embeds.

Suggested change
async def wikipedia(self, ctx: commands.Context[Tux], *, query: str) -> None:
"""
Search Wikipedia
Parameters
----------
ctx : commands.Context[Tux]
The context object for the command.
query : str
The search query.
"""
title: tuple[str, str] = self.query_wiki(self.wikipedia_api_url, query)
embed = self.create_embed(title, ctx)
await ctx.send(embed=embed)
async def wikipedia(self, ctx: commands.Context[Tux], *, query: str) -> None:
"""
Search Wikipedia
Parameters
----------
ctx : commands.Context[Tux]
The context object for the command.
query : str
The search query.
"""
title: tuple[str, str] | None = self.query_wiki(self.wikipedia_api_url, query)
if not title:
await ctx.send("❌ No Wikipedia article found for your query, or the request failed.")
return
embed = self.create_embed(title, ctx)
await ctx.send(embed=embed)



async def setup(bot: Tux) -> None:
await bot.add_cog(Wiki(bot))