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

Conversation

Atmois
Copy link
Contributor

@Atmois Atmois commented Jul 24, 2025

Add subcommand to search wikipedia

Summary by Sourcery

Add support for searching Wikipedia from the wiki command by adding a new 'wikipedia' subcommand, configuring its API endpoint, and extending URL generation for Wikipedia results.

New Features:

  • Add 'wikipedia' subcommand (alias 'wp') to the wiki cog for searching Wikipedia

Enhancements:

  • Introduce wikipedia_api_url and set up usage for the new subcommand
  • Update query_wiki to construct Wikipedia article URLs when using the Wikipedia API

Copy link
Contributor

sourcery-ai bot commented Jul 24, 2025

Reviewer's Guide

This PR extends the existing wiki cog by integrating a new Wikipedia subcommand: it registers the Wikipedia API URL, updates URL routing logic to support Wikipedia articles, and adds the wikipedia (alias wp) command that queries and renders Wikipedia pages in embeds.

Sequence diagram for the new Wikipedia subcommand flow

sequenceDiagram
    actor User
    participant Bot
    participant WikiCog
    User->>Bot: !wiki wikipedia <query>
    Bot->>WikiCog: wikipedia(ctx, query)
    WikiCog->>WikiCog: query_wiki(wikipedia_api_url, query)
    WikiCog->>WikiCog: create_embed(title, ctx)
    WikiCog->>Bot: send(embed)
    Bot->>User: Display Wikipedia result embed
Loading

Class diagram for updated Wiki cog with Wikipedia support

classDiagram
    class Wiki {
        - bot: Tux
        - arch_wiki_api_url: str
        - atl_wiki_api_url: str
        - wikipedia_api_url: str
        + __init__(bot: Tux)
        + create_embed(title: tuple[str, str], ctx: commands.Context[Tux]) : discord.Embed
        + query_wiki(base_url: str, search_term: str) : tuple[str, str]
        + arch_wiki(ctx: commands.Context[Tux], query: str)
        + atl_wiki(ctx: commands.Context[Tux], query: str)
        + wikipedia(ctx: commands.Context[Tux], query: str)  <<added>>
    }
    Wiki : +wikipedia_api_url <<added>>
    Wiki : +wikipedia(ctx, query) <<added>>
Loading

File-Level Changes

Change Details Files
Initialize Wikipedia integration in constructor
  • Define self.wikipedia_api_url for Wikipedia API
  • Assign self.wikipedia.usage using generate_usage
tux/cogs/utility/wiki.py
Handle Wikipedia URLs in query_wiki logic
  • Add elif branch for 'wikipedia.org' in base URL check
  • Construct Wikipedia-specific article URL format
tux/cogs/utility/wiki.py
Implement wikipedia subcommand
  • Add @wiki.command decorator for 'wikipedia' (alias 'wp')
  • Invoke query_wiki with the new wikipedia_api_url
  • Create embed from returned title and send it in context
tux/cogs/utility/wiki.py

Possibly linked issues

  • #0: The PR adds a Wikipedia subcommand, directly implementing the issue's suggestion for wiki command additions.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Atmois - I've reviewed your changes - here's some feedback:

  • The URL construction in query_wiki relies on string matching; consider using the API’s fullurl field for Wikipedia (and other wikis) to generate more reliable links.
  • The new wikipedia subcommand logic is almost identical to the existing ones—refactor into a generic helper that takes the wiki identifier and API URL to avoid code duplication.
  • There’s no handling for cases where the search returns no results; adding a user-friendly message or fallback in query_wiki/create_embed would improve the UX.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The URL construction in query_wiki relies on string matching; consider using the API’s fullurl field for Wikipedia (and other wikis) to generate more reliable links.
- The new wikipedia subcommand logic is almost identical to the existing ones—refactor into a generic helper that takes the wiki identifier and API URL to avoid code duplication.
- There’s no handling for cases where the search returns no results; adding a user-friendly message or fallback in query_wiki/create_embed would improve the UX.

## Individual Comments

### Comment 1
<location> `tux/cogs/utility/wiki.py:166` </location>
<code_context>
+        name="wikipedia",
+        aliases=["wp"],
+    )
+    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)
+

</code_context>

<issue_to_address>
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.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    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)
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +166 to +182
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)
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)

Copy link

codecov bot commented Jul 24, 2025

Codecov Report

Attention: Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.

Project coverage is 9.29%. Comparing base (3e4f69e) to head (97c3f9d).
Report is 2 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/cogs/utility/wiki.py 0.00% 9 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #975      +/-   ##
========================================
+ Coverage   9.26%   9.29%   +0.03%     
========================================
  Files        123     123              
  Lines      10390   10399       +9     
  Branches    1276    1277       +1     
========================================
+ Hits         963     967       +4     
- Misses      9325    9328       +3     
- Partials     102     104       +2     
Flag Coverage Δ *Carryforward flag
database 0.31% <ø> (ø) Carriedforward from 3e4f69e
integration 5.85% <0.00%> (-0.01%) ⬇️
unit 6.30% <0.00%> (-0.01%) ⬇️

*This pull request uses carry forward flags. Click here to find out more.

Components Coverage Δ
Core Bot Infrastructure 16.45% <ø> (ø)
Database Layer 0.00% <ø> (ø)
Bot Commands & Features 0.00% <0.00%> (ø)
Event & Error Handling ∅ <ø> (∅)
Utilities & Helpers ∅ <ø> (∅)
User Interface Components 0.00% <ø> (ø)
CLI Interface ∅ <ø> (∅)
External Service Wrappers ∅ <ø> (∅)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 97c3f9d
Status: ✅  Deploy successful!
Preview URL: https://9817e2a3.tux-afh.pages.dev
Branch Preview URL: https://wikicmd.tux-afh.pages.dev

View logs

@electron271 electron271 changed the title Add Wikipedia support to the wiki command feat(wiki): Add subcommand to search wikipedia Jul 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant