allthingslinux / tux

Tux is an all in one bot for the All Things Linux discord server.
https://discord.gg/linux
GNU General Public License v3.0
85 stars 25 forks source link

Levels #560

Closed Atmois closed 1 month ago

Atmois commented 1 month ago

Description

Implements levelling system, allows configuration of blacklisted channels and mutlipliers & level role rewards in settings.yml.

New commands: xpset levelset xpblacklist xp xpreset xplacklist is toggleable bool to make user unable to gain xp xpreset sets level and xp to 0 xpset and levelset update the relevant value and change the other to the appropiate amount

New db model "levels" to store all the new data into the db

New service levels.py to increment xp on message send

Checks to ensure that inputs are valid

Guidelines

How Has This Been Tested? (if applicable)

Tested extensivly in ATL Dev sandbox

Screenshots (if applicable)

n/a

Additional Information

thanks to @AmilieCoding for some coding contributions and @wlinator and @kzndotsh for improvements for a PR and all the contributors for input on how to implement and help on testing this!

Atmois commented 1 month ago

Almost ready just waiting on @AmilieCoding to push the embeds

sourcery-ai[bot] commented 1 month ago

Reviewer's Guide by Sourcery

This pull request implements a leveling system for the Discord bot. It introduces new commands for managing user levels and XP, adds a database model for storing level data, and includes a service for handling XP increments and level-ups. The implementation allows for configuration of blacklisted channels, XP multipliers, and level role rewards through a settings file.

User journey diagram for leveling system commands

journey
    title User Journey for Leveling System Commands
    section Level Commands
      level: User -> Bot: Fetch XP and level
      levelset: Admin -> Bot: Set user level
    section XP Commands
      xpset: Admin -> Bot: Set user XP
      xpreset: Admin -> Bot: Reset user XP
    section Blacklist Commands
      xpblacklist: Admin -> Bot: Toggle XP blacklist for user

ER diagram for Levels Database Model

erDiagram
    GUILD {
        int guild_id
    }
    LEVELS {
        int member_id
        int guild_id
        float xp
        int level
        datetime last_message
        bool blacklisted
    }
    GUILD ||--o{ LEVELS : "has"
    LEVELS }|--|| GUILD : "belongs to"

Class diagram for LevelsService and LevelsController

classDiagram
    class LevelsService {
        - Tux bot
        - LevelsController levels_controller
        - int xp_cooldown
        - float levels_exponent
        - dict xp_roles
        - dict xp_multipliers
        + xp_listener(discord.Message)
        + process_xp_gain(discord.Member, discord.Guild)
        + is_on_cooldown(datetime.datetime) bool
        + handle_level_up(discord.Member, discord.Guild, int)
        + update_roles(discord.Member, discord.Guild, int)
        + try_assign_role(discord.Member, discord.Role)
        + calculate_xp_for_level(int) float
        + calculate_xp_increment(discord.Member) float
        + calculate_level(float) int
        + valid_xplevel_input(int) discord.Embed | None
        + generate_progress_bar(int, int, int) str
        + get_level_progress(float, int) tuple[int, int]
    }
    class LevelsController {
        + ensure_guild_exists(int) Guild
        + get_xp(int, int) float
        + get_level(int, int) int
        + get_xp_and_level(int, int) tuple[float, int]
        + get_last_message_time(int, int) datetime.datetime | None
        + is_blacklisted(int, int) bool
        + update_xp_and_level(int, int, float, int, datetime.datetime)
        + toggle_blacklist(int, int) bool
        + reset_xp(int, int)
    }
    LevelsService --> LevelsController

File-Level Changes

Change Details Files
Implemented a leveling system service
  • Created LevelsService class to handle XP increments and level-ups
  • Implemented XP gain on message send with cooldown
  • Added methods for calculating XP and levels
  • Implemented role updates based on user levels
tux/cogs/services/levels.py
Added database controller for levels
  • Created LevelsController class for database operations
  • Implemented methods for getting and updating XP and levels
  • Added functions for managing user blacklist status
tux/database/controllers/levels.py
Implemented level-related commands
  • Added 'level' command to display user XP and level
  • Implemented 'levelset' command to set user level
  • Added 'xpset' command to set user XP
  • Created 'xpblacklist' command to toggle user XP gain
  • Implemented 'xpreset' command to reset user XP and level
tux/cogs/levels/level.py
tux/cogs/levels/levelset.py
tux/cogs/levels/xpset.py
tux/cogs/levels/xpblacklist.py
tux/cogs/levels/xpreset.py

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. #### Customizing Your Experience Access your [dashboard](https://app.sourcery.ai) 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 - [Contact our support team](mailto:support@sourcery.ai) for questions or feedback. - Visit our [documentation](https://docs.sourcery.ai) for detailed guides and information. - Keep in touch with the Sourcery team by following us on [X/Twitter](https://x.com/SourceryAI), [LinkedIn](https://www.linkedin.com/company/sourcery-ai/) or [GitHub](https://github.com/sourcery-ai).