JacobCoffee / byte

The Discord bot built for developers
https://byte-bot.app/
MIT License
5 stars 2 forks source link

Enhancement: Add guild-specific settings #9

Closed JacobCoffee closed 11 months ago

JacobCoffee commented 1 year ago

Summary

Currently things like command prefix, help channel ID, etc. are hard coded.

To do anything useful past one guild we need to stand up a database schema that allows for settings thing per-guild.

Basic Example

Here is a rough stub that needs worked on. I think (?) that we should introduce a new path under src/ for database things specifically, so that they can be shared between the bot and web service.

"""Shared models.

.. todo:: This is mostly a stub package that needs to be
    fleshed out.
"""

from sqlalchemy import BigInteger, Boolean, ForeignKey, String, Table
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.database.orm import TimestampedDatabaseModel

allowed_roles_table = Table(
    "allowed_roles",
    TimestampedDatabaseModel.metadata,
    mapped_column("github_config_id", ForeignKey("github_configs.id"), primary_key=True),
    mapped_column("role_id", BigInteger, primary_key=True),
)

class GuildConfig(TimestampedDatabaseModel):
    __tablename__ = "guild_configs"

    guild_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    prefix: Mapped[str] = mapped_column(String(5), nullable=False, server_default="!", de

    help_channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    github_config: Mapped["GitHubConfig"] = relationship("GitHubConfig", uselist=False, back_populates="guild_config")

class GitHubConfig(TimestampedDatabaseModel):
    __tablename__ = "github_configs"

    guild_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("guild_configs.guild_id"), nullable=False)
    discussion_sync: Mapped[bool] = mapped_column(Boolean, default=False)
    github_organization: Mapped[str] = mapped_column(String, nullable=True)
    github_repository: Mapped[str] = mapped_column(String, nullable=True)
    sync_label: Mapped[str] = mapped_column(String, nullable=True)
    issue_linking: Mapped[bool] = mapped_column(Boolean, default=False)
    comment_linking: Mapped[bool] = mapped_column(Boolean, default=False)
    pep_linking: Mapped[bool] = mapped_column(Boolean, default=False)

    allowed_roles: Mapped[list[int]] = relationship(
        "Role",
        secondary=allowed_roles_table,
        back_populates="github_configs",
    )

    guild_config: Mapped[GuildConfig] = relationship("GuildConfig", back_populates="github_config")

class Role(TimestampedDatabaseModel):
    __tablename__ = "roles"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)

    github_configs: Mapped[list[GitHubConfig]] = relationship(
        "GitHubConfig",
        secondary=allowed_roles_table,
        back_populates="allowed_roles",
    )

Drawbacks and Impact

Complexity.

Unresolved questions


[!NOTE]
We utilize Polar.sh to engage in regular as well as pledge-based > sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.

Fund with Polar