Skip to content

feat: add assets/data for data files, refactor facts, improve substitutions, add __init__.py into tux/extensions #945

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

Merged
merged 1 commit into from
Jul 15, 2025

Conversation

electron271
Copy link
Member

@electron271 electron271 commented Jul 13, 2025

Description

feat(fact.py): refactor facts to pull facts from assets/data/facts.toml, implement pulling facts from apis, implement substitutions
feat(assets/data): create assets/data for future use
feat(substitutions): move activity substitutions into its own file for easy use in other files
fix(extensions): add init.py into tux/extensions to fix some issues

Guidelines

  • My code follows the style guidelines of this project (formatted with Ruff)

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • I have made corresponding changes to the documentation if needed

  • My changes generate no new warnings

  • I have tested this change

  • Any dependent changes have been merged and published in downstream modules

  • I have added all appropriate labels to this PR

  • I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

ran facts with multiple different uses
tested substitutions
worked on an extension

Summary by Sourcery

Refactor the facts feature to load data from an external TOML file, support API-driven and static facts with placeholder substitutions, add autocomplete for fact categories, extract substitution logic into a shared module, and prepare assets/data for future use and extensions.

New Features:

  • Load fact categories from an external TOML file and support dynamic fact fetching from APIs
  • Add autocomplete for the fact command’s category selection

Enhancements:

  • Move placeholder substitution logic into a dedicated utils/substitutions module
  • Refactor the fact command to use external data, handle category mappings, and apply substitutions
  • Update the activity handler to apply substitutions via the new helper function

Chores:

  • Create an assets/data directory with a facts.toml file and update .gitignore
  • Add init.py to tux/extensions to resolve import issues

…ml, implement pulling facts from apis, implement substitutions

feat(assets/data): create assets/data for future use
feat(substitutions): move activity substitutions into its own file for easy use in other files
fix(extensions): add __init__.py into tux/extensions to fix some issues
@electron271 electron271 self-assigned this Jul 13, 2025
@electron271 electron271 added category: commands category: meta Relating to architecture, systems and critical functions labels Jul 13, 2025
@electron271 electron271 linked an issue Jul 13, 2025 that may be closed by this pull request
Copy link
Contributor

sourcery-ai bot commented Jul 13, 2025

Reviewer's Guide

This PR externalizes static fact data into a TOML file, refactors the Fact cog to load and fetch facts dynamically (including API support, autocomplete and usage generation), introduces a global substitution utility for templating placeholders across facts and activities, simplifies the activity handler by removing inline substitution logic and adding logging, and adds the missing init.py in tux/extensions to resolve package initialization issues.

Sequence diagram for fetching a fact with substitutions and API support

sequenceDiagram
    actor User
    participant DiscordBot as Tux
    participant FactCog as Fact
    participant Substitution as handle_substitution
    participant API as ExternalFactAPI
    User->>DiscordBot: /fact [category]
    DiscordBot->>FactCog: fact(ctx, fact_type)
    FactCog->>FactCog: _fetch_fact(fact_type)
    alt fact_api_url present
        FactCog->>API: GET fact_api_url
        API-->>FactCog: {fact_api_field: fact}
        FactCog->>Substitution: handle_substitution(bot, fact)
        Substitution-->>FactCog: substituted_fact
    else
        FactCog->>Substitution: handle_substitution(bot, random_fact)
        Substitution-->>FactCog: substituted_fact
    end
    FactCog->>DiscordBot: send embed with substituted_fact
    DiscordBot-->>User: Fact embed
Loading

Class diagram for Fact cog refactor and substitution utility

classDiagram
    class Fact {
        - bot: Tux
        - facts_data: dict[str, dict[str, Any]]
        + __init__(bot: Tux)
        + _load_facts() None
        + _fetch_fact(fact_type: str) tuple[str, str] | None
        + fact_type_autocomplete(interaction, current) list[app_commands.Choice[str]]
        + fact(ctx, fact_type: str = "random") None
    }
    class handle_substitution {
        + handle_substitution(bot: Tux, text: str)
    }
    Fact --> "uses" handle_substitution : calls
    class Tux
    Fact --> Tux : has reference
    handle_substitution --> Tux : uses
Loading

Class diagram for new substitutions utility

classDiagram
    class handle_substitution {
        + handle_substitution(bot: Tux, text: str)
        - _get_member_count(bot: Tux)
    }
    class Tux
    handle_substitution --> Tux : uses
Loading

File-Level Changes

Change Details Files
Externalize fact definitions into a TOML data file
  • create assets/data/facts.toml with named categories, fact lists and optional API endpoints
  • add workspace_root path resolution and tomllib-based parser in Fact cog
assets/data/facts.toml
tux/cogs/fun/fact.py
Refactor Fact cog to use dynamic data and substitutions
  • remove hardcoded enums and fact lists
  • implement _load_facts, _fetch_fact and autocomplete methods
  • update fact command to build embed from dynamic results
tux/cogs/fun/fact.py
Introduce handle_substitution utility
  • extract placeholder replacement into tux/utils/substitutions.py
  • apply handle_substitution in Fact cog and activity handler
tux/utils/substitutions.py
tux/cogs/fun/fact.py
tux/handlers/activity.py
Simplify activity presence updates
  • remove inline handle_substitution and member count helpers
  • use handle_substitution for activity names
  • add warnings for missing activity names
tux/handlers/activity.py
Fix extensions package initialization
  • add init.py to tux/extensions to enable module imports
tux/extensions/__init__.py

Possibly linked issues

  • #0: PR creates assets/data/facts.toml and refactors code to use it, addressing issue.

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 @electron271 - I've reviewed your changes and they look great!


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.

Copy link

codecov bot commented Jul 13, 2025

Codecov Report

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

Project coverage is 9.32%. Comparing base (28f2176) to head (6a93645).
Report is 10 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/cogs/fun/fact.py 0.00% 63 Missing ⚠️
tux/utils/substitutions.py 0.00% 16 Missing ⚠️
tux/handlers/activity.py 0.00% 6 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #945   +/-   ##
=====================================
  Coverage   9.32%   9.32%           
=====================================
  Files        121     122    +1     
  Lines      10286   10327   +41     
  Branches    1259    1267    +8     
=====================================
+ Hits         959     963    +4     
- Misses      9226    9262   +36     
- Partials     101     102    +1     
Flag Coverage Δ *Carryforward flag
database 0.31% <ø> (+<0.01%) ⬆️ Carriedforward from 28f2176
integration 5.89% <0.00%> (-0.03%) ⬇️
unit 6.35% <0.00%> (-0.03%) ⬇️

*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.

@electron271 electron271 merged commit d773525 into main Jul 15, 2025
36 checks passed
@electron271 electron271 deleted the 862-feature-assetsdata branch July 15, 2025 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: commands category: meta Relating to architecture, systems and critical functions priority: low
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] - assets/data
1 participant