From e754a10241af2d7bf995e5936961c2fcaae66797 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 11:56:42 +0100 Subject: [PATCH 1/6] Added command checks #114 --- kdb-bot/src/bot/main.py | 2 + .../bot_core/core_extension/core_extension.py | 28 +++++++ kdb-bot/src/bot_core/exception/__init__.py | 0 kdb-bot/src/bot_core/exception/check_error.py | 7 ++ kdb-bot/src/bot_core/helper/command_checks.py | 76 +++++++++++++++++++ .../modules/base/command/restart_command.py | 14 +--- .../events/base_on_command_error_event.py | 4 + 7 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 kdb-bot/src/bot_core/core_extension/core_extension.py create mode 100644 kdb-bot/src/bot_core/exception/__init__.py create mode 100644 kdb-bot/src/bot_core/exception/check_error.py create mode 100644 kdb-bot/src/bot_core/helper/command_checks.py diff --git a/kdb-bot/src/bot/main.py b/kdb-bot/src/bot/main.py index 9747634bb7..b5e2a6c22f 100644 --- a/kdb-bot/src/bot/main.py +++ b/kdb-bot/src/bot/main.py @@ -12,6 +12,7 @@ from bot.startup_migration_extension import StartupMigrationExtension from bot.startup_module_extension import StartupModuleExtension from bot.startup_settings_extension import StartupSettingsExtension from bot_api.app_api_extension import AppApiExtension +from bot_core.core_extension.core_extension import CoreExtension from modules.boot_log.boot_log_extension import BootLogExtension from modules.database.database_extension import DatabaseExtension @@ -31,6 +32,7 @@ class Program: .use_extension(BootLogExtension) \ .use_extension(DatabaseExtension) \ .use_extension(AppApiExtension) \ + .use_extension(CoreExtension) \ .use_startup(Startup) self.app: Application = await app_builder.build_async() await self.app.run_async() diff --git a/kdb-bot/src/bot_core/core_extension/core_extension.py b/kdb-bot/src/bot_core/core_extension/core_extension.py new file mode 100644 index 0000000000..fba66dd7af --- /dev/null +++ b/kdb-bot/src/bot_core/core_extension/core_extension.py @@ -0,0 +1,28 @@ +from cpl_core.application import ApplicationExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_translation import TranslatePipe + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +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 modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class CoreExtension(ApplicationExtensionABC): + + def __init__(self): + ApplicationExtensionABC.__init__(self) + + async def run(self, config: ConfigurationABC, services: ServiceProviderABC): + feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) + if not feature_flags.get_flag(FeatureFlagsEnum.core_module): + return + + permissions: PermissionServiceABC = services.get_service(PermissionServiceABC) + client_utils: ClientUtilsServiceABC = services.get_service(ClientUtilsServiceABC) + message_service: MessageServiceABC = services.get_service(MessageServiceABC) + t: TranslatePipe = services.get_service(TranslatePipe) + CommandChecks.init(permissions, client_utils, message_service, t) diff --git a/kdb-bot/src/bot_core/exception/__init__.py b/kdb-bot/src/bot_core/exception/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/kdb-bot/src/bot_core/exception/check_error.py b/kdb-bot/src/bot_core/exception/check_error.py new file mode 100644 index 0000000000..d5c9a2b8ab --- /dev/null +++ b/kdb-bot/src/bot_core/exception/check_error.py @@ -0,0 +1,7 @@ +from discord.ext.commands import CommandError + + +class CheckError(CommandError): + + def __init__(self, message, *args): + CommandError.__init__(self, message, *args) diff --git a/kdb-bot/src/bot_core/helper/command_checks.py b/kdb-bot/src/bot_core/helper/command_checks.py new file mode 100644 index 0000000000..6bb3fa3024 --- /dev/null +++ b/kdb-bot/src/bot_core/helper/command_checks.py @@ -0,0 +1,76 @@ +from typing import Optional + +from cpl_translation import TranslatePipe +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.exception.check_error import CheckError +from modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class CommandChecks: + _permissions: Optional[PermissionServiceABC] = None + _client_utils: Optional[ClientUtilsServiceABC] = None + _message_service: Optional[MessageServiceABC] = None + _t: Optional[TranslatePipe] = None + + @classmethod + def init( + cls, + permissions: PermissionServiceABC, + client_utils: ClientUtilsServiceABC, + message_service: MessageServiceABC, + translate: TranslatePipe, + ): + cls._permissions = permissions + cls._client_utils = client_utils + cls._message_service = message_service + cls._t = translate + + @classmethod + def check_is_ready(cls): + async def check_if_bot_is_ready_yet_and_respond(ctx: Context) -> bool: + result = await cls._client_utils.check_if_bot_is_ready_yet_and_respond(ctx) + if not result: + raise CheckError(f'Bot is not ready') + return result + + return commands.check(check_if_bot_is_ready_yet_and_respond) + + @classmethod + def check_is_member_admin(cls): + async def check_is_member_admin(ctx: Context): + has_permission = cls._permissions.is_member_admin(ctx.author) + if not has_permission: + await cls._message_service.send_ctx_msg(ctx, cls._t.transform('common.no_permission_message')) + raise CheckError(f'Member {ctx.author.name} is not admin') + + return has_permission + + return commands.check(check_is_member_admin) + + @classmethod + def check_is_member_technician(cls): + async def check_is_member_technician(ctx: Context): + has_permission = cls._permissions.is_member_technician(ctx.author) + if not has_permission: + await cls._message_service.send_ctx_msg(ctx, cls._t.transform('common.no_permission_message')) + raise CheckError(f'Member {ctx.author.name} is not technician') + + return has_permission + + return commands.check(check_is_member_technician) + + @classmethod + def check_is_member_moderator(cls): + async def check_is_member_moderator(ctx: Context): + has_permission = cls._permissions.is_member_moderator(ctx.author) + if not has_permission: + await cls._message_service.send_ctx_msg(ctx, cls._t.transform('common.no_permission_message')) + raise CheckError(f'Member {ctx.author.name} is not moderator') + + return has_permission + + return commands.check(check_is_member_moderator) diff --git a/kdb-bot/src/modules/base/command/restart_command.py b/kdb-bot/src/modules/base/command/restart_command.py index 829810e0a2..c1fd3ae59a 100644 --- a/kdb-bot/src/modules/base/command/restart_command.py +++ b/kdb-bot/src/modules/base/command/restart_command.py @@ -1,6 +1,5 @@ import asyncio -import discord from cpl_core.configuration import ConfigurationABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC @@ -11,6 +10,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.bot_settings import BotSettings +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -43,18 +43,10 @@ class RestartCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def restart(self, ctx: Context): self._logger.debug(__name__, f'Received command restart {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished restart command') - return self._config.add_configuration('IS_RESTART', 'true') await self._client_utils.presence_game('common.presence.restart') diff --git a/kdb-bot/src/modules/base/events/base_on_command_error_event.py b/kdb-bot/src/modules/base/events/base_on_command_error_event.py index 72a7a20e94..6233d9788a 100644 --- a/kdb-bot/src/modules/base/events/base_on_command_error_event.py +++ b/kdb-bot/src/modules/base/events/base_on_command_error_event.py @@ -13,6 +13,7 @@ from cpl_discord.events.on_command_error_abc import OnCommandErrorABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.bot_settings import BotSettings +from bot_core.exception.check_error import CheckError class BaseOnCommandErrorEvent(OnCommandErrorABC): @@ -35,6 +36,9 @@ class BaseOnCommandErrorEvent(OnCommandErrorABC): self._t = translate async def on_command_error(self, ctx: Context, error: CommandError): + if isinstance(error, CheckError): + return + error = getattr(error, 'original', error) uid = uuid.uuid4() self._logger.error(__name__, f'Got error: {type(error).__name__} UID: {uid}') From 7173dee28d9e778e08ea8f5ef14a355e6e77ff73 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 11:58:09 +0100 Subject: [PATCH 2/6] Added event checks #114 --- .../bot_core/core_extension/core_extension.py | 2 ++ kdb-bot/src/bot_core/helper/event_checks.py | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 kdb-bot/src/bot_core/helper/event_checks.py diff --git a/kdb-bot/src/bot_core/core_extension/core_extension.py b/kdb-bot/src/bot_core/core_extension/core_extension.py index fba66dd7af..a482bcc3f9 100644 --- a/kdb-bot/src/bot_core/core_extension/core_extension.py +++ b/kdb-bot/src/bot_core/core_extension/core_extension.py @@ -8,6 +8,7 @@ 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.helper.event_checks import EventChecks from modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -26,3 +27,4 @@ class CoreExtension(ApplicationExtensionABC): message_service: MessageServiceABC = services.get_service(MessageServiceABC) t: TranslatePipe = services.get_service(TranslatePipe) CommandChecks.init(permissions, client_utils, message_service, t) + EventChecks.init(client_utils) diff --git a/kdb-bot/src/bot_core/helper/event_checks.py b/kdb-bot/src/bot_core/helper/event_checks.py new file mode 100644 index 0000000000..9d4e9ccbc2 --- /dev/null +++ b/kdb-bot/src/bot_core/helper/event_checks.py @@ -0,0 +1,31 @@ +from typing import Optional + +from cpl_translation import TranslatePipe +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.exception.check_error import CheckError +from modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class EventChecks: + _client_utils: Optional[ClientUtilsServiceABC] = None + + @classmethod + def init( + cls, + client_utils: ClientUtilsServiceABC, + ): + cls._client_utils = client_utils + + @classmethod + def check_is_ready(cls): + async def check_if_bot_is_ready_yet_and_respond(ctx: Context) -> bool: + result = await cls._client_utils.check_if_bot_is_ready_yet_and_respond(ctx) + if not result: + raise CheckError(f'Bot is not ready') + return result + + return commands.check(check_if_bot_is_ready_yet_and_respond) From 5cdf2834cc91a65647d0e77fa0703e5981671b33 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 12:01:34 +0100 Subject: [PATCH 3/6] Added is ready check to events #114 --- .../modules/auto_role/events/auto_role_on_raw_reaction_add.py | 2 ++ .../auto_role/events/auto_role_on_raw_reaction_remove.py | 2 ++ kdb-bot/src/modules/base/events/base_on_member_join_event.py | 2 ++ kdb-bot/src/modules/base/events/base_on_member_remove_event.py | 2 ++ kdb-bot/src/modules/base/events/base_on_message_event.py | 2 ++ .../src/modules/base/events/base_on_voice_state_update_event.py | 2 ++ kdb-bot/src/modules/level/events/level_on_message_event.py | 2 ++ .../modules/level/events/level_on_voice_state_update_event.py | 2 ++ 8 files changed, 16 insertions(+) diff --git a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py index f86de1f064..5893d4638e 100644 --- a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py +++ b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py @@ -3,6 +3,7 @@ from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC from cpl_discord.service import DiscordBotServiceABC from discord import RawReactionActionEvent +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 modules.auto_role.helper.reaction_handler import ReactionHandler @@ -26,6 +27,7 @@ class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): self._auto_roles = auto_roles self._reaction_handler = reaction_handler + @EventChecks.check_is_ready() async def on_raw_reaction_add(self, payload: RawReactionActionEvent): self._logger.debug(__name__, f'Module {type(self)} started') diff --git a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py index 309366762e..49899a0e5d 100644 --- a/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py +++ b/kdb-bot/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py @@ -3,6 +3,7 @@ from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC from cpl_discord.service import DiscordBotServiceABC from discord import RawReactionActionEvent +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 modules.auto_role.helper.reaction_handler import ReactionHandler @@ -26,6 +27,7 @@ class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): self._auto_roles = auto_roles self._reaction_handler = reaction_handler + @EventChecks.check_is_ready() async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): self._logger.debug(__name__, f'Module {type(self)} started') diff --git a/kdb-bot/src/modules/base/events/base_on_member_join_event.py b/kdb-bot/src/modules/base/events/base_on_member_join_event.py index ebd5116e4f..72dcebce21 100644 --- a/kdb-bot/src/modules/base/events/base_on_member_join_event.py +++ b/kdb-bot/src/modules/base/events/base_on_member_join_event.py @@ -9,6 +9,7 @@ from cpl_discord.events import OnMemberJoinABC from cpl_translation import TranslatePipe from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.event_checks import EventChecks from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC @@ -91,6 +92,7 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC): except Exception as e: self._logger.error(__name__, f'Cannot get user {member.id}', e) + @EventChecks.check_is_ready() async def on_member_join(self, member: discord.Member): self._logger.debug(__name__, f'Module {type(self)} started') self._check_for_known_user(member) diff --git a/kdb-bot/src/modules/base/events/base_on_member_remove_event.py b/kdb-bot/src/modules/base/events/base_on_member_remove_event.py index b3d36b8a72..e9987fc604 100644 --- a/kdb-bot/src/modules/base/events/base_on_member_remove_event.py +++ b/kdb-bot/src/modules/base/events/base_on_member_remove_event.py @@ -8,6 +8,7 @@ from cpl_discord.events import OnMemberRemoveABC from cpl_translation import TranslatePipe from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.event_checks import EventChecks from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC @@ -59,6 +60,7 @@ class BaseOnMemberRemoveEvent(OnMemberRemoveABC): except Exception as e: self._logger.error(__name__, f'Cannot get user {member.id}', e) + @EventChecks.check_is_ready() async def on_member_remove(self, member: discord.Member): self._logger.debug(__name__, f'Module {type(self)} started') await self._remove_user(member) diff --git a/kdb-bot/src/modules/base/events/base_on_message_event.py b/kdb-bot/src/modules/base/events/base_on_message_event.py index 7f6cb926ef..2d562c663b 100644 --- a/kdb-bot/src/modules/base/events/base_on_message_event.py +++ b/kdb-bot/src/modules/base/events/base_on_message_event.py @@ -5,6 +5,7 @@ from cpl_core.database.context import DatabaseContextABC from cpl_discord.events import OnMessageABC from cpl_discord.service import DiscordBotServiceABC +from bot_core.helper.event_checks import EventChecks from bot_core.helper.log_message_helper import LogMessageHelper from bot_core.logging.message_logger import MessageLogger from bot_data.abc.client_repository_abc import ClientRepositoryABC @@ -70,6 +71,7 @@ class BaseOnMessageEvent(OnMessageABC): self._logger.debug(__name__, f'User {user} sent message. xp: from {old_xp} to {user.xp}') + @EventChecks.check_is_ready() async def on_message(self, message: discord.Message): self._logger.debug(__name__, f'Module {type(self)} started') self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') diff --git a/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py b/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py index 5869d44672..176977a968 100644 --- a/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py +++ b/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py @@ -7,6 +7,7 @@ from cpl_core.database.context import DatabaseContextABC from cpl_core.logging import LoggerABC from cpl_discord.events import OnVoiceStateUpdateABC +from bot_core.helper.event_checks import EventChecks from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC @@ -83,6 +84,7 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC): except Exception as e: self._logger.error(__name__, f'Ontime validation failed', e) + @EventChecks.check_is_ready() async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState): self._logger.debug(__name__, f'Module {type(self)} started') self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}') diff --git a/kdb-bot/src/modules/level/events/level_on_message_event.py b/kdb-bot/src/modules/level/events/level_on_message_event.py index cd69622e07..293fb08f31 100644 --- a/kdb-bot/src/modules/level/events/level_on_message_event.py +++ b/kdb-bot/src/modules/level/events/level_on_message_event.py @@ -1,6 +1,7 @@ import discord from cpl_discord.events import OnMessageABC +from bot_core.helper.event_checks import EventChecks from bot_core.logging.message_logger import MessageLogger from modules.level.service.level_service import LevelService @@ -16,6 +17,7 @@ class LevelOnMessageEvent(OnMessageABC): 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') await self._level.check_level(message.author) diff --git a/kdb-bot/src/modules/level/events/level_on_voice_state_update_event.py b/kdb-bot/src/modules/level/events/level_on_voice_state_update_event.py index c9127e0376..910e570a26 100644 --- a/kdb-bot/src/modules/level/events/level_on_voice_state_update_event.py +++ b/kdb-bot/src/modules/level/events/level_on_voice_state_update_event.py @@ -2,6 +2,7 @@ import discord from cpl_core.logging import LoggerABC from cpl_discord.events import OnVoiceStateUpdateABC +from bot_core.helper.event_checks import EventChecks from modules.level.service.level_service import LevelService @@ -18,6 +19,7 @@ class LevelOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC): self._logger.info(__name__, f'Module {type(self)} loaded') + @EventChecks.check_is_ready() async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState): self._logger.debug(__name__, f'Module {type(self)} started') await self._level.check_level(member) From d38fa7775710850b8d07cfb98838fbb9ed1edafa Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 12:07:50 +0100 Subject: [PATCH 4/6] Added checks to commands #114 --- .../auto_role/command/auto_role_group.py | 61 +++---------- .../src/modules/base/command/afk_command.py | 5 +- .../src/modules/base/command/help_command.py | 5 +- .../src/modules/base/command/info_command.py | 5 +- .../src/modules/base/command/ping_command.py | 5 +- .../src/modules/base/command/purge_command.py | 11 +-- .../modules/base/command/shutdown_command.py | 13 +-- .../src/modules/base/command/user_group.py | 11 +-- .../src/modules/level/command/level_group.py | 90 ++++--------------- .../src/modules/stats/command/stats_group.py | 51 +++-------- 10 files changed, 58 insertions(+), 199 deletions(-) diff --git a/kdb-bot/src/modules/auto_role/command/auto_role_group.py b/kdb-bot/src/modules/auto_role/command/auto_role_group.py index 6dbf51c803..ee599473a5 100644 --- a/kdb-bot/src/modules/auto_role/command/auto_role_group.py +++ b/kdb-bot/src/modules/auto_role/command/auto_role_group.py @@ -13,6 +13,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +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 @@ -56,16 +57,10 @@ class AutoRoleGroup(DiscordCommandABC): @auto_role.command(alias='auto-roles') @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role list') - return if ctx.guild is None: return @@ -95,16 +90,10 @@ class AutoRoleGroup(DiscordCommandABC): @auto_role.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role add') - return message = List(discord.Message, [message async for message in channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() if message is None: @@ -142,16 +131,10 @@ class AutoRoleGroup(DiscordCommandABC): @auto_role.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role remove') - return auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role) if auto_role_from_db is None: @@ -183,16 +166,10 @@ class AutoRoleGroup(DiscordCommandABC): @rule.command(alias='rules') @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role rule list') - return embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), @@ -227,16 +204,10 @@ class AutoRoleGroup(DiscordCommandABC): @rule.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role rule add') - return emoji = discord.utils.get(self._bot.emojis, name=emoji_name) if emoji is None: @@ -300,16 +271,10 @@ class AutoRoleGroup(DiscordCommandABC): @rule.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @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}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command auto-role remove') - return auto_role_from_db = self._auto_roles.get_auto_role_rule_by_id(auto_role_rule) if auto_role_from_db is None: diff --git a/kdb-bot/src/modules/base/command/afk_command.py b/kdb-bot/src/modules/base/command/afk_command.py index 5b21cf7510..234c24feae 100644 --- a/kdb-bot/src/modules/base/command/afk_command.py +++ b/kdb-bot/src/modules/base/command/afk_command.py @@ -8,6 +8,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from modules.base.configuration.base_server_settings import BaseServerSettings @@ -36,11 +37,9 @@ class AFKCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() async def afk(self, ctx: Context): self._logger.debug(__name__, f'Received command afk {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}') if ctx.author.voice is None or ctx.author.voice.channel is None: diff --git a/kdb-bot/src/modules/base/command/help_command.py b/kdb-bot/src/modules/base/command/help_command.py index b85dacd890..0c690ecba2 100644 --- a/kdb-bot/src/modules/base/command/help_command.py +++ b/kdb-bot/src/modules/base/command/help_command.py @@ -10,6 +10,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from modules.base.configuration.base_server_settings import BaseServerSettings @@ -36,11 +37,9 @@ class HelpCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() async def help(self, ctx: Context, persistent_flag: str = None): self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}') is_persistent = persistent_flag == '--stay' await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent) diff --git a/kdb-bot/src/modules/base/command/info_command.py b/kdb-bot/src/modules/base/command/info_command.py index 2358777834..974bc4914b 100644 --- a/kdb-bot/src/modules/base/command/info_command.py +++ b/kdb-bot/src/modules/base/command/info_command.py @@ -11,6 +11,7 @@ from discord.ext.commands import Context import bot from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger @@ -38,11 +39,9 @@ class InfoCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() async def info(self, ctx: Context, *, wait: int = None): self._logger.debug(__name__, f'Received command info {ctx},{wait}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id) embed = discord.Embed( diff --git a/kdb-bot/src/modules/base/command/ping_command.py b/kdb-bot/src/modules/base/command/ping_command.py index cedbdcefa5..9ccf1420af 100644 --- a/kdb-bot/src/modules/base/command/ping_command.py +++ b/kdb-bot/src/modules/base/command/ping_command.py @@ -6,6 +6,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger @@ -31,10 +32,8 @@ class PingCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() async def ping(self, ctx: Context): self._logger.debug(__name__, f'Received command ping {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.base.pong')) self._logger.trace(__name__, f'Finished ping command') diff --git a/kdb-bot/src/modules/base/command/purge_command.py b/kdb-bot/src/modules/base/command/purge_command.py index 11b90c22ce..8ec78effe2 100644 --- a/kdb-bot/src/modules/base/command/purge_command.py +++ b/kdb-bot/src/modules/base/command/purge_command.py @@ -9,6 +9,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.server_settings import ServerSettings +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -37,18 +38,12 @@ class PurgeCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def purge(self, ctx: Context): self._logger.debug(__name__, f'Received command purge {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) server_settings: ServerSettings = self._config.get_configuration(f'ServerSettings_{ctx.guild.id}') - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished purge command') - return - await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.moderator.purge_message')) await asyncio.sleep(server_settings.message_delete_timer) try: diff --git a/kdb-bot/src/modules/base/command/shutdown_command.py b/kdb-bot/src/modules/base/command/shutdown_command.py index e2541e8d5b..f8ebce5d05 100644 --- a/kdb-bot/src/modules/base/command/shutdown_command.py +++ b/kdb-bot/src/modules/base/command/shutdown_command.py @@ -11,6 +11,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.bot_settings import BotSettings +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -43,18 +44,10 @@ class ShutdownCommand(DiscordCommandABC): @commands.hybrid_command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def shutdown(self, ctx: Context): self._logger.debug(__name__, f'Received command shutdown {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished shutdown command') - return await self._client_utils.presence_game('common.presence.shutdown') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.admin.shutdown_message')) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index a2efb8ba72..b08bd07230 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -10,6 +10,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe from bot_data.abc.server_repository_abc import ServerRepositoryABC @@ -60,16 +61,10 @@ class UserGroup(DiscordCommandABC): @user.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def info(self, ctx: Context, member: Optional[discord.Member] = None, *, wait: int = None): self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished purge command') - return if member is None or not isinstance(member, discord.Member): member = ctx.author diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py index c8253a2c4d..502c09361c 100644 --- a/kdb-bot/src/modules/level/command/level_group.py +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -12,6 +12,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC @@ -99,16 +100,10 @@ class LevelGroup(DiscordCommandABC): @level.command(alias='levels') @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def list(self, ctx: Context, wait: int = None): self._logger.debug(__name__, f'Received command level list {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level list') - return if ctx.guild is None: return @@ -141,16 +136,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_admin() async def create(self, ctx: Context, name: str, color: str, min_xp: int, permissions: int): self._logger.debug(__name__, f'Received command level create {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_admin(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level remove') - return try: color = hex(discord.Colour.from_str(color).value) @@ -199,19 +188,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_admin() async def edit(self, ctx: Context, level: str, name: str = None, color: str = None, min_xp: int = None, permissions: int = None): self._logger.debug(__name__, f'Received command level edit {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_admin(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level remove') - return - - if ctx.guild is None: - return server = self._servers.get_server_by_discord_id(ctx.guild.id) level_from_db = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default() @@ -248,7 +228,8 @@ class LevelGroup(DiscordCommandABC): self._levels.update_level(level_from_db) self._db.save_changes() await role.edit(name=level_from_db.name, permissions=discord.Permissions(level_from_db.permissions), colour=discord.Colour(int(level_from_db.color, 16))) - self._logger.info(__name__, f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}') + self._logger.info(__name__, + f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}') except Exception as e: self._logger.error(__name__, f'Could not save level {level} with color {color}, min_xp {min_xp} and permissions {permissions}', e) else: @@ -271,20 +252,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_admin() async def remove(self, ctx: Context, level: str): self._logger.debug(__name__, f'Received command level remove {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_admin(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level remove') - return - - if ctx.guild is None: - self._logger.trace(__name__, f'Finished command level remove') - return server = self._servers.get_server_by_discord_id(ctx.guild.id) level_from_db = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).first_or_default() @@ -318,19 +289,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def down(self, ctx: Context, member: discord.Member): self._logger.debug(__name__, f'Received command level down {ctx} {member}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level down') - return - - if ctx.guild is None: - return if member.bot: return @@ -360,19 +322,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def up(self, ctx: Context, member: discord.Member): self._logger.debug(__name__, f'Received command level up {ctx} {member}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level up') - return - - if ctx.guild is None: - return if member.bot: return @@ -402,19 +355,10 @@ class LevelGroup(DiscordCommandABC): @level.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def set(self, ctx: Context, member: discord.Member, level: str): self._logger.debug(__name__, f'Received command level up {ctx} {member}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command level up') - return - - if ctx.guild is None: - return if member.bot: return diff --git a/kdb-bot/src/modules/stats/command/stats_group.py b/kdb-bot/src/modules/stats/command/stats_group.py index 41d40c5e08..b035e54a36 100644 --- a/kdb-bot/src/modules/stats/command/stats_group.py +++ b/kdb-bot/src/modules/stats/command/stats_group.py @@ -10,6 +10,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC from bot_core.abc.message_service_abc import MessageServiceABC +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.statistic_repository_abc import StatisticRepositoryABC @@ -51,16 +52,10 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def list(self, ctx: Context, wait: int = None): self._logger.debug(__name__, f'Received command stats list {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats list') - return if ctx.guild is None: return @@ -91,16 +86,10 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() async def view(self, ctx: Context, name: str, wait: int = None): self._logger.debug(__name__, f'Received command stats view {ctx}:{name}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_moderator(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats view') - return if ctx.guild is None: return @@ -139,16 +128,10 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_technician() async def add(self, ctx: Context, name: str): self._logger.debug(__name__, f'Received command stats add {ctx}: {name}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_technician(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats add') - return if ctx.guild is None: return @@ -161,16 +144,10 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_technician() async def edit(self, ctx: Context, name: str): self._logger.debug(__name__, f'Received command stats edit {ctx}: {name}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_technician(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats edit') - return try: server = self._servers.get_server_by_discord_id(ctx.guild.id) @@ -192,16 +169,10 @@ class StatsGroup(DiscordCommandABC): @stats.command() @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_technician() async def remove(self, ctx: Context, name: str): self._logger.debug(__name__, f'Received command stats remove {ctx}: {name}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - self._client_utils.received_command(ctx.guild.id) - - if not self._permissions.is_member_technician(ctx.author): - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) - self._logger.trace(__name__, f'Finished command stats remove') - return try: server = self._servers.get_server_by_discord_id(ctx.guild.id) From 026d989789daebd26c6898b631eb4d5124beef9c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 12:11:01 +0100 Subject: [PATCH 5/6] Improved event checks #114 --- kdb-bot/src/bot_core/abc/client_utils_service_abc.py | 3 +++ kdb-bot/src/bot_core/helper/event_checks.py | 6 +++--- kdb-bot/src/bot_core/service/client_utils_service.py | 10 ++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/bot_core/abc/client_utils_service_abc.py b/kdb-bot/src/bot_core/abc/client_utils_service_abc.py index 0345ba3607..da5d099a50 100644 --- a/kdb-bot/src/bot_core/abc/client_utils_service_abc.py +++ b/kdb-bot/src/bot_core/abc/client_utils_service_abc.py @@ -17,6 +17,9 @@ class ClientUtilsServiceABC(ABC): @abstractmethod def get_client(self, dc_ic: int, guild_id: int): pass + @abstractmethod + async def check_if_bot_is_ready_yet(self) -> bool: pass + @abstractmethod async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: pass diff --git a/kdb-bot/src/bot_core/helper/event_checks.py b/kdb-bot/src/bot_core/helper/event_checks.py index 9d4e9ccbc2..0eba341c39 100644 --- a/kdb-bot/src/bot_core/helper/event_checks.py +++ b/kdb-bot/src/bot_core/helper/event_checks.py @@ -22,10 +22,10 @@ class EventChecks: @classmethod def check_is_ready(cls): - async def check_if_bot_is_ready_yet_and_respond(ctx: Context) -> bool: - result = await cls._client_utils.check_if_bot_is_ready_yet_and_respond(ctx) + async def check_if_bot_is_ready() -> bool: + result = await cls._client_utils.check_if_bot_is_ready() if not result: raise CheckError(f'Bot is not ready') return result - return commands.check(check_if_bot_is_ready_yet_and_respond) + return commands.check(check_if_bot_is_ready) diff --git a/kdb-bot/src/bot_core/service/client_utils_service.py b/kdb-bot/src/bot_core/service/client_utils_service.py index 733e3903b0..56f5296348 100644 --- a/kdb-bot/src/bot_core/service/client_utils_service.py +++ b/kdb-bot/src/bot_core/service/client_utils_service.py @@ -59,14 +59,20 @@ class ClientUtilsService(ClientUtilsServiceABC): client = self._clients.find_client_by_discord_id_and_server_id(self._bot.user.id, server.server_id) return client - async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: + async def check_if_bot_is_ready_yet(self) -> bool: if self._config.get_configuration('IS_READY') == 'true': return True self._logger.debug(__name__, f'Bot is not ready yet {self._t.transform("common.errors.bot_not_ready_yet")}') - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'), without_tracking=True) return False + async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: + result = await self.check_if_bot_is_ready_yet() + if result: + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'), without_tracking=True) + + return result + async def presence_game(self, t_key: str): if not self._feature_flags.get_flag(FeatureFlagsEnum.presence): return From 905182931c313c2d35ba2fe34553ed4899b58cf4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Nov 2022 12:15:53 +0100 Subject: [PATCH 6/6] Improved event checks #114 --- kdb-bot/src/bot_core/service/client_utils_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kdb-bot/src/bot_core/service/client_utils_service.py b/kdb-bot/src/bot_core/service/client_utils_service.py index 56f5296348..3d072700a1 100644 --- a/kdb-bot/src/bot_core/service/client_utils_service.py +++ b/kdb-bot/src/bot_core/service/client_utils_service.py @@ -68,7 +68,7 @@ class ClientUtilsService(ClientUtilsServiceABC): async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: result = await self.check_if_bot_is_ready_yet() - if result: + if not result: await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'), without_tracking=True) return result