Skip to content

feat: enhance service cogs with improved functionality #959

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kzndotsh
Copy link
Contributor

@kzndotsh kzndotsh commented Jul 20, 2025

  • Add new reminders service
  • Update levels service integration
  • Improve influxdb logging capabilities
  • Enhance gif limiter and status roles

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. If this change fixes any issues please put "Fixes #XX" in the description. Please also ensure to add the appropriate labels to the PR.

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)

Please describe how you tested your code. e.g describe what commands you ran, what arguments, and any config stuff (if applicable)

Screenshots (if applicable)

Please add screenshots to help explain your changes.

Additional Information

Please add any other information that is important to this PR.

Summary by Sourcery

Add a full-featured reminders service and enhance existing service cogs with improved background tasks, error handling, and configuration checks to strengthen InfluxDB logging, leveling, status roles, and GIF limiting functionality

New Features:

  • Add ReminderService cog to schedule, process, and send user reminders

Enhancements:

  • Introduce a dedicated InfluxDB guild stats logging loop with lifecycle management and conditional cog loading
  • Optimize Levels cog by consolidating database queries and refining XP gain logic
  • Simplify StatusRoles cog by only loading it when configuration is present
  • Name and wrap GifLimiter cleanup loop with before_loop and error handlers for improved resilience

Copy link
Contributor

sourcery-ai bot commented Jul 20, 2025

Reviewer's Guide

This PR augments existing service cogs by enriching InfluxDB logging with guild stats, streamlining database interactions in the levels service, simplifying status-role loading, strengthening gif-limiter task handling, and introduces a new ReminderService implementing a priority‐queue–driven scheduling loop for user reminders.

Sequence diagram for scheduling and sending reminders in ReminderService

sequenceDiagram
    participant User
    participant ReminderService
    participant DatabaseController as DB
    participant Discord as DiscordAPI
    User->>ReminderService: Create reminder (via command)
    ReminderService->>DB: Store reminder
    DB-->>ReminderService: Confirmation
    ReminderService->>ReminderService: schedule_reminder(reminder)
    Note over ReminderService: Reminder added to priority queue
    ReminderService->>ReminderService: reminder_processor loop triggers
    ReminderService->>ReminderService: send_reminder(reminder)
    ReminderService->>DiscordAPI: Send DM to user
    alt DM fails
        ReminderService->>DiscordAPI: Send in channel
    end
    ReminderService->>DB: Delete reminder by ID
Loading

Class diagram for the new ReminderService and ScheduledReminder

classDiagram
    class ReminderService {
        - Tux bot
        - DatabaseController db
        - bool _initialized
        - list~ScheduledReminder~ _reminder_queue
        - asyncio.Lock _queue_lock
        - set~asyncio.Task~ _reminder_tasks
        + __init__(bot: Tux)
        + _create_reminder_task(coro, name)
        + cog_unload()
        + reminder_processor()
        + before_reminder_processor()
        + on_reminder_processor_error(error)
        + schedule_reminder(reminder: Reminder)
        + send_reminder(reminder: Reminder)
        + on_ready()
    }
    class ScheduledReminder {
        + float timestamp
        + Reminder reminder
    }
    ReminderService --> ScheduledReminder
    ReminderService --> Reminder
    ScheduledReminder --> Reminder
Loading

Class diagram for updated InfluxLogger (InfluxDB logging)

classDiagram
    class InfluxLogger {
        - Tux bot
        - DatabaseController db
        - Any|None influx_write_api
        - str influx_org
        + __init__(bot: Tux)
        + init_influx() bool
        + _log_guild_stats()
        + before_log_guild_stats()
        + on_log_guild_stats_error(error)
        + logger()
        + before_logger()
        + on_logger_error(error)
        + cog_unload()
    }
Loading

Class diagram for updated Levels service integration

classDiagram
    class LevelsService {
        + xp_listener(message: discord.Message)
        + process_xp_gain(member: discord.Member, guild: discord.Guild, user_level_data: Levels|None)
        + is_on_cooldown(last_message_time)
        + calculate_xp_increment(member)
    }
    class Levels {
        + float xp
        + int level
        + bool blacklisted
        + datetime last_message
    }
    LevelsService --> Levels : uses
Loading

Class diagram for updated StatusRoles cog

classDiagram
    class StatusRoles {
        - Bot bot
        - status_roles
        + __init__(bot)
        + on_ready()
        + check_and_update_roles(member)
    }
Loading

Class diagram for updated GifLimiter cog

classDiagram
    class GifLimiter {
        + on_message(message: discord.Message)
        + old_gif_remover()
        + before_old_gif_remover()
        + on_old_gif_remover_error(error)
        + cog_unload()
    }
Loading

File-Level Changes

Change Details Files
Enhanced InfluxDB logging with additional guild‐stats loop, error handling, and conditional setup
  • Added a separate _log_guild_stats loop for real-time guild metrics
  • Introduced before_loop and error handlers on both logger loops
  • Implemented cog_unload to cancel background tasks
  • Renamed loops via name parameter and refined type hints
  • Conditional cog loading based on CONFIG completeness
tux/cogs/services/influxdblogger.py
Refactored levels service to batch‐fetch user state and eliminate redundant DB calls
  • Switched to bot.get_context to ignore commands instead of prefix checks
  • Fetched full user_level_data in one query inside xp_listener
  • Moved blacklist and cooldown logic into xp_listener
  • Updated process_xp_gain signature to accept pre-fetched data and remove redundant queries
tux/cogs/services/levels.py
Streamlined status_roles cog initialization and loading
  • Removed dynamic unload task and its helper method
  • Added config presence check in setup to skip loading when empty
  • Simplified logging to report loaded role count
tux/cogs/services/status_roles.py
Augmented gif_limiter with named tasks, startup checks, and error handling
  • Named the old_gif_remover loop and added before_loop for readiness
  • Added error handler to capture and report loop exceptions
  • Ensured clean cog_unload cancellation of the remover task
tux/cogs/services/gif_limiter.py
Introduced ReminderService cog with priority‐queue scheduling
  • Created ReminderService with a heap-based priority queue and lock
  • Implemented reminder_processor loop with before_loop, error handler, and task creation
  • Added schedule_reminder and send_reminder methods with DM fallback
  • Managed task references to avoid GC and cleaned up in cog_unload
  • Loaded existing reminders on on_ready to seed the queue
tux/cogs/services/reminders.py

Possibly linked issues

  • Server guide/quick links #123: The PR directly addresses the issue by adding detailed stats for cases, implementing a new reminders service, and updating the level system.

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

github-actions bot commented Jul 20, 2025

Dependency Review

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

Scanned Files

None

Copy link

codecov bot commented Jul 20, 2025

Codecov Report

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

Project coverage is 9.33%. Comparing base (3e4f69e) to head (13e1fd4).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/cogs/services/reminders.py 0.00% 87 Missing ⚠️
tux/cogs/services/influxdblogger.py 0.00% 51 Missing ⚠️
tux/cogs/services/levels.py 0.00% 15 Missing ⚠️
tux/cogs/services/gif_limiter.py 0.00% 11 Missing ⚠️
tux/cogs/services/status_roles.py 0.00% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #959      +/-   ##
========================================
+ Coverage   9.26%   9.33%   +0.06%     
========================================
  Files        123     124       +1     
  Lines      10390   10514     +124     
  Branches    1276    1291      +15     
========================================
+ Hits         963     981      +18     
- Misses      9325    9424      +99     
- Partials     102     109       +7     
Flag Coverage Δ *Carryforward flag
database 0.31% <ø> (+<0.01%) ⬆️ Carriedforward from 3e4f69e
integration 5.79% <0.00%> (-0.07%) ⬇️
unit 6.23% <0.00%> (-0.08%) ⬇️

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

- Add new reminders service
- Update levels service integration
- Improve influxdb logging capabilities
- Enhance gif limiter and status roles
@kzndotsh kzndotsh force-pushed the 03-services-split branch from 3298959 to 13e1fd4 Compare July 20, 2025 04:38
Copy link

cloudflare-workers-and-pages bot commented Jul 20, 2025

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 13e1fd4
Status: ✅  Deploy successful!
Preview URL: https://8cd63c4a.tux-afh.pages.dev
Branch Preview URL: https://03-services-split.tux-afh.pages.dev

View logs

@kzndotsh
Copy link
Contributor Author

Depends on: #957

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