feat: Refactored all cogs implementation
Some checks failed
CI / Run tests (pull_request) Has been cancelled
Some checks failed
CI / Run tests (pull_request) Has been cancelled
This commit is contained in:
42
cogs/__init__.py
Normal file
42
cogs/__init__.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import discord, json, csv
|
||||
from discord.ext import commands
|
||||
from typing import Union, Any, Optional, Callable
|
||||
from setup.config import Config
|
||||
|
||||
class BaseCog(commands.Cog):
|
||||
def __init__(self,bot:discord.Bot):
|
||||
self.bot = bot
|
||||
|
||||
class ConfigurableCog(BaseCog):
|
||||
def __init__(self, bot: discord.Bot):
|
||||
super().__init__(bot)
|
||||
|
||||
|
||||
class FeatureCog(BaseCog):
|
||||
"""Base Cog that provides per-guild feature flag checks.
|
||||
|
||||
Subclasses should set `feature_name` or pass it to the constructor.
|
||||
This class reads the feature flags from `Config.GUILD_CONFIGS[guild_id]["features"]`.
|
||||
"""
|
||||
|
||||
def __init__(self, bot: discord.Bot, feature_name: Optional[str] = None):
|
||||
super().__init__(bot)
|
||||
self.feature_name = feature_name
|
||||
|
||||
def is_enabled(self, guild_id: int) -> bool:
|
||||
if not self.feature_name:
|
||||
return False
|
||||
guild_cfg = Config.GUILD_CONFIGS.get(guild_id)
|
||||
if not guild_cfg:
|
||||
return False
|
||||
features = guild_cfg.get("features", {})
|
||||
return bool(features.get(self.feature_name, False))
|
||||
|
||||
def require_enabled_check(self) -> Callable[[commands.Context], bool]:
|
||||
def predicate(ctx: commands.Context) -> bool:
|
||||
if not ctx.guild:
|
||||
return False
|
||||
return self.is_enabled(ctx.guild.id)
|
||||
|
||||
return commands.check(predicate)
|
||||
|
||||
Reference in New Issue
Block a user