Improved feature flag for server handling #334

This commit is contained in:
Sven Heidemann 2023-08-15 23:24:29 +02:00
parent 113b188a40
commit 1b2bb85b37
13 changed files with 144 additions and 4 deletions

View File

@ -34,6 +34,17 @@ class FeatureFlagsSettings(ConfigurationModelABC):
for flag in [f.value for f in FeatureFlagsEnum]:
self._load_flag(kwargs, FeatureFlagsEnum(flag))
@classmethod
def get_flag_from_dict(cls, flags: dict, key: FeatureFlagsEnum) -> bool:
def get_flag():
if key.value not in cls._flags:
return False
return cls._flags[key.value]
if key.value not in flags:
return get_flag()
return flags[key.value]
def get_flag(self, key: FeatureFlagsEnum) -> bool:
if key.value not in self._flags:
return False

View File

@ -14,7 +14,7 @@ from modules.achievements.events.achievement_on_message_event import Achievement
class AchievementsModule(ModuleABC):
def __init__(self, dc: DiscordCollectionABC):
ModuleABC.__init__(self, dc, FeatureFlagsEnum.auto_role_module)
ModuleABC.__init__(self, dc, FeatureFlagsEnum.achievements_module)
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
pass

View File

@ -1,4 +1,5 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -6,16 +7,20 @@ from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.achievements.achievement_service import AchievementService
class AchievementGroup(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
@ -26,6 +31,7 @@ class AchievementGroup(DiscordCommandABC):
):
DiscordCommandABC.__init__(self)
self._config = config
self._logger = logger
self._message_service = message_service
self._bot = bot
@ -45,6 +51,11 @@ class AchievementGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def check(self, ctx: Context, member: discord.Member):
self._logger.debug(__name__, f"Received command achievement check {ctx}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
server = self._servers.get_server_by_discord_id(member.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id)

View File

@ -1,18 +1,23 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnMessageABC
from cpl_discord.service import DiscordBotServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.achievements.achievement_service import AchievementService
class AchievementOnMessageEvent(OnMessageABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
achievements: AchievementService,
@ -22,6 +27,7 @@ class AchievementOnMessageEvent(OnMessageABC):
):
OnMessageABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._achievements = achievements
@ -31,6 +37,12 @@ class AchievementOnMessageEvent(OnMessageABC):
@EventChecks.check_is_ready()
async def on_message(self, message: discord.Message):
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{message.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
if message.author.bot:
return

View File

@ -1,6 +1,7 @@
from typing import List as TList
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
@ -12,18 +13,22 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
from bot_data.model.server_config import ServerConfig
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class AutoRoleGroup(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
@ -36,6 +41,7 @@ class AutoRoleGroup(DiscordCommandABC):
):
DiscordCommandABC.__init__(self)
self._config = config
self._logger = logger
self._message_service = message_service
self._bot = bot
@ -69,6 +75,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def list(self, ctx: Context, wait: int = None):
self._logger.debug(__name__, f"Received command auto-role list {ctx}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
if ctx.guild is None:
return
@ -110,6 +119,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def add(self, ctx: Context, channel: discord.TextChannel, message_id: str):
self._logger.debug(__name__, f"Received command auto-role add {ctx} {message_id}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
message = (
List(
@ -170,6 +182,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def remove(self, ctx: Context, auto_role: int):
self._logger.debug(__name__, f"Received command auto-role remove {ctx} {auto_role}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role)
if auto_role_from_db is None:
@ -210,6 +225,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def list(self, ctx: Context, auto_role: int, wait: int = None):
self._logger.debug(__name__, f"Received command auto-role rule list {ctx}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
embed = discord.Embed(
title=self._t.transform("modules.auto_role.list.title"),
@ -262,6 +280,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def add(self, ctx: Context, auto_role: int, emoji_name: str, role_id: str):
self._logger.debug(__name__, f"Received command auto-role add {ctx} {auto_role}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
emoji = discord.utils.get(self._bot.emojis, name=emoji_name)
if emoji is None:
@ -354,6 +375,9 @@ class AutoRoleGroup(DiscordCommandABC):
@CommandChecks.check_is_member_moderator()
async def remove(self, ctx: Context, auto_role_rule: int):
self._logger.debug(__name__, f"Received command auto-role remove {ctx} {auto_role_rule}")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{ctx.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
auto_role_from_db = self._auto_roles.get_auto_role_rule_by_id(auto_role_rule)
if auto_role_from_db is None:

View File

@ -1,17 +1,22 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.auto_role.helper.auto_role_reaction_handler import AutoRoleReactionHandler
class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
@ -20,6 +25,7 @@ class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
):
OnRawReactionAddABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._servers = servers
@ -29,6 +35,9 @@ class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC):
@EventChecks.check_is_ready()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{payload.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
await self._reaction_handler.handle(payload, "add")

View File

@ -1,17 +1,22 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.server_config import ServerConfig
from modules.auto_role.helper.auto_role_reaction_handler import AutoRoleReactionHandler
class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
@ -20,6 +25,7 @@ class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
):
OnRawReactionRemoveABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._servers = servers
@ -29,6 +35,9 @@ class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
@EventChecks.check_is_ready()
async def on_raw_reaction_remove(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{payload.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.auto_role_module):
return
await self._reaction_handler.handle(payload, "remove")

View File

@ -8,6 +8,8 @@ from cpl_translation import TranslatePipe
from discord import guild
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.model.server_config import ServerConfig
@ -60,6 +62,11 @@ class BootLogOnReadyEvent(OnReadyABC):
self._logger.error(__name__, f"Config {type(self).__name__}_{g.id} not found!")
return
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.boot_log_module
):
continue
self._bot.loop.create_task(
self._message_service.send_channel_message(
self._bot.get_channel(server_config.login_message_channel_id),

View File

@ -1,18 +1,29 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_discord.events import OnMemberJoinABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_core.logging.message_logger import MessageLogger
from bot_data.model.server_config import ServerConfig
from modules.level.service.level_service import LevelService
class LevelOnMemberJoinEvent(OnMemberJoinABC):
def __init__(self, logger: MessageLogger, level: LevelService):
def __init__(self, config: ConfigurationABC, logger: MessageLogger, level: LevelService):
OnMemberJoinABC.__init__(self)
self._config = config
self._logger = logger
self._level = level
@EventChecks.check_is_ready()
async def on_member_join(self, member: discord.Member):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
await self._level.check_level(member)

View File

@ -1,20 +1,31 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_discord.events import OnMessageABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_core.logging.message_logger import MessageLogger
from bot_data.model.server_config import ServerConfig
from modules.level.service.level_service import LevelService
class LevelOnMessageEvent(OnMessageABC):
def __init__(self, logger: MessageLogger, level: LevelService):
def __init__(self, config: ConfigurationABC, logger: MessageLogger, level: LevelService):
OnMessageABC.__init__(self)
self._config = config
self._logger = logger
self._level = level
@EventChecks.check_is_ready()
async def on_message(self, message: discord.Message):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{message.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
try:
await self._level.check_level(message.author)
except Exception as e:

View File

@ -1,21 +1,27 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.model.server_config import ServerConfig
from modules.level.service.level_service import LevelService
class LevelOnRawReactionAddEvent(OnRawReactionAddABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
level: LevelService,
):
OnRawReactionAddABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._level = level
@ -23,6 +29,12 @@ class LevelOnRawReactionAddEvent(OnRawReactionAddABC):
@EventChecks.check_is_ready()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{payload.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
try:
self._logger.trace(__name__, f"Handle reaction {payload} for level")

View File

@ -1,21 +1,27 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC
from cpl_discord.service import DiscordBotServiceABC
from discord import RawReactionActionEvent
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.model.server_config import ServerConfig
from modules.level.service.level_service import LevelService
class LevelOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
bot: DiscordBotServiceABC,
level: LevelService,
):
OnRawReactionRemoveABC.__init__(self)
self._config = config
self._logger = logger
self._bot = bot
self._level = level
@ -23,6 +29,12 @@ class LevelOnRawReactionRemoveEvent(OnRawReactionRemoveABC):
@EventChecks.check_is_ready()
async def on_raw_reaction_remove(self, payload: RawReactionActionEvent):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{payload.guild_id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
try:
self._logger.trace(__name__, f"Handle reaction {payload} for level")

View File

@ -1,14 +1,19 @@
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnVoiceStateUpdateABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.helper.event_checks import EventChecks
from bot_data.model.server_config import ServerConfig
from modules.level.service.level_service import LevelService
class LevelOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
def __init__(self, logger: LoggerABC, level: LevelService):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, level: LevelService):
OnVoiceStateUpdateABC.__init__(self)
self._config = config
self._logger = logger
self._level = level
@ -22,4 +27,10 @@ class LevelOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
after: discord.VoiceState,
):
self._logger.debug(__name__, f"Module {type(self)} started")
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}")
if not FeatureFlagsSettings.get_flag_from_dict(
server_config.feature_flags, FeatureFlagsEnum.achievements_module
):
return
await self._level.check_level(member)