From 901aa4e1a58768b02149b5e1a69629021198bec6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 16:19:54 +0200 Subject: [PATCH 01/20] Added auto role tables #54 --- cpl-workspace.json | 1 + src/bot/config/feature-flags.json | 1 + src/bot/module_list.py | 2 + src/bot/startup_migration_extension.py | 2 + .../configuration/feature_flags_enum.py | 1 + .../configuration/feature_flags_settings.py | 1 + src/bot_data/migration/auto_role_migration.py | 45 ++++++++++++ src/bot_data/model/auto_role.py | 66 +++++++++++++++++ src/bot_data/model/auto_role_rule.py | 69 ++++++++++++++++++ src/bot_data/model/server.py | 4 +- .../service/auto_role_repository_service.py | 72 +++++++++++++++++++ src/modules/autorole/__init__.py | 26 +++++++ src/modules/autorole/auto-role.json | 46 ++++++++++++ src/modules/autorole/auto_role_module.py | 21 ++++++ 14 files changed, 355 insertions(+), 2 deletions(-) create mode 100644 src/bot_data/migration/auto_role_migration.py create mode 100644 src/bot_data/model/auto_role.py create mode 100644 src/bot_data/model/auto_role_rule.py create mode 100644 src/bot_data/service/auto_role_repository_service.py create mode 100644 src/modules/autorole/__init__.py create mode 100644 src/modules/autorole/auto-role.json create mode 100644 src/modules/autorole/auto_role_module.py diff --git a/cpl-workspace.json b/cpl-workspace.json index 098991ab..663b790a 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -6,6 +6,7 @@ "bot-core": "src/bot_core/bot-core.json", "bot-data": "src/bot_data/bot-data.json", "admin": "src/modules/admin/admin.json", + "auto-role": "src/modules/admin/auto-role.json", "base": "src/modules/base/base.json", "boot-log": "src/modules/boot_log/boot-log.json", "database": "src/modules/database/database.json", diff --git a/src/bot/config/feature-flags.json b/src/bot/config/feature-flags.json index 5c347644..b20c9cca 100644 --- a/src/bot/config/feature-flags.json +++ b/src/bot/config/feature-flags.json @@ -1,6 +1,7 @@ { "FeatureFlags": { "AdminModule": true, + "AutoRoleModule": true, "BaseModule": true, "BootLogModule": true, "CoreModule": true, diff --git a/src/bot/module_list.py b/src/bot/module_list.py index f7690033..6e9670ff 100644 --- a/src/bot/module_list.py +++ b/src/bot/module_list.py @@ -4,6 +4,7 @@ from bot_core.core_extension.core_extension_module import CoreExtensionModule from bot_core.core_module import CoreModule from bot_data.data_module import DataModule from modules.admin.admin_module import AdminModule +from modules.autorole.auto_role_module import AutoRoleModule from modules.base.base_module import BaseModule from modules.boot_log.boot_log_module import BootLogModule from modules.database.database_module import DatabaseModule @@ -20,6 +21,7 @@ class ModuleList: CoreModule, # has to be first! DataModule, AdminModule, + AutoRoleModule, BaseModule, DatabaseModule, ModeratorModule, diff --git a/src/bot/startup_migration_extension.py b/src/bot/startup_migration_extension.py index a40a0516..b8f7ff0e 100644 --- a/src/bot/startup_migration_extension.py +++ b/src/bot/startup_migration_extension.py @@ -4,6 +4,7 @@ from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC from bot_data.abc.migration_abc import MigrationABC +from bot_data.migration.auto_role_migration import AutoRoleMigration from bot_data.migration.initial_migration import InitialMigration from bot_data.service.migration_service import MigrationService @@ -19,3 +20,4 @@ class StartupMigrationExtension(StartupExtensionABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(MigrationService) services.add_transient(MigrationABC, InitialMigration) + services.add_transient(MigrationABC, AutoRoleMigration) # 03.10.2022 #54 - 0.2.2 diff --git a/src/bot_core/configuration/feature_flags_enum.py b/src/bot_core/configuration/feature_flags_enum.py index 91e2ca91..dac193b1 100644 --- a/src/bot_core/configuration/feature_flags_enum.py +++ b/src/bot_core/configuration/feature_flags_enum.py @@ -5,6 +5,7 @@ class FeatureFlagsEnum(Enum): # modules admin_module = 'AdminModule' + auto_role_module = 'AutoRoleModule' base_module = 'BaseModule' boot_log_module = 'BootLogModule' core_module = 'CoreModule' diff --git a/src/bot_core/configuration/feature_flags_settings.py b/src/bot_core/configuration/feature_flags_settings.py index 8fb815f7..4556e784 100644 --- a/src/bot_core/configuration/feature_flags_settings.py +++ b/src/bot_core/configuration/feature_flags_settings.py @@ -15,6 +15,7 @@ class FeatureFlagsSettings(ConfigurationModelABC): self._flags = { # modules FeatureFlagsEnum.admin_module.value: False, # 02.10.2022 #48 + FeatureFlagsEnum.auto_role_module.value: True, # 03.10.2022 #54 FeatureFlagsEnum.base_module.value: True, # 02.10.2022 #48 FeatureFlagsEnum.boot_log_module.value: True, # 02.10.2022 #48 FeatureFlagsEnum.core_module.value: True, # 03.10.2022 #56 diff --git a/src/bot_data/migration/auto_role_migration.py b/src/bot_data/migration/auto_role_migration.py new file mode 100644 index 00000000..9aad5f72 --- /dev/null +++ b/src/bot_data/migration/auto_role_migration.py @@ -0,0 +1,45 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class AutoRoleMigration(MigrationABC): + + def __init__(self, logger: DatabaseLogger, db: DBContext): + self._logger = logger + self._db = db + self._cursor = db.cursor + + def upgrade(self): + self._logger.debug(__name__, 'Running upgrade') + + self._cursor.execute( + str(f""" + CREATE TABLE IF NOT EXISTS `AutoRoles` ( + `AutoRoleId` BIGINT NOT NULL AUTO_INCREMENT, + `ServerId` BIGINT, + `DiscordMessageId` BIGINT NOT NULL, + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + PRIMARY KEY(`AutoRoleId`) + ); + """) + ) + + self._cursor.execute( + str(f""" + CREATE TABLE IF NOT EXISTS `AutoRoleRules` ( + `AutoRoleRuleId` BIGINT NOT NULL AUTO_INCREMENT, + `AutoRoleId` BIGINT, + `DiscordEmojiId` BIGINT NOT NULL, + `DiscordRoleId` BIGINT NOT NULL, + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + PRIMARY KEY(`AutoRoleRuleId`) + ); + """) + ) + + def downgrade(self): + self._cursor.execute('DROP TABLE `AutoRole`;') + self._cursor.execute('DROP TABLE `AutoRoleRules`;') diff --git a/src/bot_data/model/auto_role.py b/src/bot_data/model/auto_role.py new file mode 100644 index 00000000..9d02e514 --- /dev/null +++ b/src/bot_data/model/auto_role.py @@ -0,0 +1,66 @@ +from datetime import datetime +from typing import Optional + +from cpl_core.database import TableABC + + +class AutoRole(TableABC): + + def __init__(self, server_id: int, dc_message_id: int, created_at: datetime=None, modified_at: datetime=None, id=0): + self._auto_role_id = id + self._server_id = server_id + self._discord_message_id = dc_message_id + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @staticmethod + def get_select_all_string() -> str: + return str(f""" + SELECT * FROM `AutoRoles`; + """) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `AutoRoles` + WHERE `AutoRoleId` = {id}; + """) + + @staticmethod + def get_select_by_server_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `AutoRoles` + WHERE `ServerId` = {id}; + """) + + @property + def insert_string(self) -> str: + return str(f""" + INSERT INTO `AutoRoles` ( + `ServerId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._server_id}, + {self._discord_message_id}, + '{self._created_at}', + '{self._modified_at}' + ); + """) + + @property + def udpate_string(self) -> str: + return str(f""" + UPDATE `AutoRoles` + SET `ServerId` = {self._server_id}, + SET `DiscordMessageId` = {self._discord_message_id}, + `LastModifiedAt` = '{self._modified_at}' + WHERE `AutoRoleId` = {self._auto_role_id}; + """) + + @property + def delete_string(self) -> str: + return str(f""" + DELETE FROM `AutoRoles` + WHERE `AutoRoleId` = {self._auto_role_id}; + """) diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py new file mode 100644 index 00000000..f123615a --- /dev/null +++ b/src/bot_data/model/auto_role_rule.py @@ -0,0 +1,69 @@ +from datetime import datetime +from typing import Optional + +from cpl_core.database import TableABC + + +class AutoRoleRule(TableABC): + + def __init__(self, auto_role_id: int, discord_emoji_id: int, discord_role_id: int, created_at: datetime=None, modified_at: datetime=None, id=0): + self._auto_role_rule_id = id + self._auto_role_id = auto_role_id + self._discord_emoji_id = discord_emoji_id + self._discord_role_id = discord_role_id + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @staticmethod + def get_select_all_string() -> str: + return str(f""" + SELECT * FROM `AutoRoleRules`; + """) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `AutoRoleRules` + WHERE `AutoRoleRuleId` = {id}; + """) + + @staticmethod + def get_select_by_server_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `AutoRoleRules` + WHERE `ServerId` = {id}; + """) + + @property + def insert_string(self) -> str: + return str(f""" + INSERT INTO `AutoRoleRules` ( + `AutoRoleId`, `DiscordEmojiId`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._auto_role_id}, + {self._discord_emoji_id}, + {self._discord_role_id}, + '{self._created_at}', + '{self._modified_at}' + ); + """) + + @property + def udpate_string(self) -> str: + return str(f""" + UPDATE `AutoRoleRules` + SET `AutoRoleId` = {self._auto_role_id}, + SET `DiscordEmojiId` = {self._discord_emoji_id}, + SET `DiscordRoleId` = {self._discord_role_id}, + `LastModifiedAt` = '{self._modified_at}' + WHERE `AutoRoleRuleId` = {self._auto_role_id}; + """) + + @property + def delete_string(self) -> str: + return str(f""" + DELETE FROM `AutoRoleRules` + WHERE `AutoRoleRuleId` = {self._auto_role_id}; + """) diff --git a/src/bot_data/model/server.py b/src/bot_data/model/server.py index 14cddefd..c3054952 100644 --- a/src/bot_data/model/server.py +++ b/src/bot_data/model/server.py @@ -60,12 +60,12 @@ class Server(TableABC): UPDATE `Servers` SET `DiscordServerId` = {self._discord_server_id}, `LastModifiedAt` = '{self._modified_at}' - WHERE `Id` = {self._server_id}; + WHERE `ServerId` = {self._server_id}; """) @property def delete_string(self) -> str: return str(f""" DELETE FROM `Servers` - WHERE `Id` = {self._server_id}; + WHERE `ServerId` = {self._server_id}; """) diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py new file mode 100644 index 00000000..728d2bc6 --- /dev/null +++ b/src/bot_data/service/auto_role_repository_service.py @@ -0,0 +1,72 @@ +from typing import Optional + +from cpl_core.database.context import DatabaseContextABC +from cpl_query.extension import List + +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.server import Server + + +class ServerRepositoryService(ServerRepositoryABC): + + def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC): + self._logger = logger + self._context = db_context + + ServerRepositoryABC.__init__(self) + + def get_servers(self) -> List[Server]: + servers = List(Server) + self._logger.trace(__name__, f'Send SQL command: {Server.get_select_all_string()}') + results = self._context.select(Server.get_select_all_string()) + for result in results: + servers.append(Server( + result[1], + id=result[0] + )) + + return servers + + def get_server_by_id(self, server_id: int) -> Server: + self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_id_string(server_id)}') + result = self._context.select(Server.get_select_by_id_string(server_id))[0] + return Server( + result[1], + id=result[0] + ) + + def get_server_by_discord_id(self, discord_id: int) -> Server: + self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}') + result = self._context.select(Server.get_select_by_discord_id_string(discord_id))[0] + return Server( + result[1], + id=result[0] + ) + + def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]: + self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}') + result = self._context.select(Server.get_select_by_discord_id_string(discord_id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return Server( + result[1], + result[2], + result[3], + id=result[0] + ) + + def add_server(self, server: Server): + self._logger.trace(__name__, f'Send SQL command: {server.insert_string}') + self._context.cursor.execute(server.insert_string) + + def update_server(self, server: Server): + self._logger.trace(__name__, f'Send SQL command: {server.udpate_string}') + self._context.cursor.execute(server.udpate_string) + + def delete_server(self, server: Server): + self._logger.trace(__name__, f'Send SQL command: {server.delete_string}') + self._context.cursor.execute(server.delete_string) diff --git a/src/modules/autorole/__init__.py b/src/modules/autorole/__init__.py new file mode 100644 index 00000000..fc1dc7f9 --- /dev/null +++ b/src/modules/autorole/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.admin' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.1' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='1') diff --git a/src/modules/autorole/auto-role.json b/src/modules/autorole/auto-role.json new file mode 100644 index 00000000..8e29094c --- /dev/null +++ b/src/modules/autorole/auto-role.json @@ -0,0 +1,46 @@ +{ + "ProjectSettings": { + "Name": "auto-role", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "cpl-core>=2022.10.0.post5" + ], + "DevDependencies": [ + "cpl-cli>=2022.10.0" + ], + "PythonVersion": ">=3.10.4", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "admin.main", + "EntryPoint": "admin", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py new file mode 100644 index 00000000..9afd273e --- /dev/null +++ b/src/modules/autorole/auto_role_module.py @@ -0,0 +1,21 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum + + +class AutoRoleModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, FeatureFlagsEnum.auto_role_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + # commands + # events + pass From f150df2dfd9f2ce38bd879b6a573d862aee966da Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 16:36:11 +0200 Subject: [PATCH 02/20] Added auto role repository #54 --- src/bot_data/abc/auto_role_repository_abc.py | 62 ++++++ src/bot_data/data_module.py | 5 +- src/bot_data/model/auto_role.py | 7 + src/bot_data/model/auto_role_rule.py | 4 +- .../service/auto_role_repository_service.py | 201 ++++++++++++++---- 5 files changed, 229 insertions(+), 50 deletions(-) create mode 100644 src/bot_data/abc/auto_role_repository_abc.py diff --git a/src/bot_data/abc/auto_role_repository_abc.py b/src/bot_data/abc/auto_role_repository_abc.py new file mode 100644 index 00000000..6afa0730 --- /dev/null +++ b/src/bot_data/abc/auto_role_repository_abc.py @@ -0,0 +1,62 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from cpl_query.extension import List + +from bot_data.model.auto_role import AutoRole +from bot_data.model.auto_role_rule import AutoRoleRule +from bot_data.model.server import Server + + +class AutoRoleRepositoryABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def get_auto_roles(self) -> List[AutoRole]: pass + + @abstractmethod + def get_auto_role_by_id(self, id: int) -> AutoRole: pass + + @abstractmethod + def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass + + @abstractmethod + def find_auto_roles_by_server_id(self, id: int) -> Optional[AutoRole]: pass + + @abstractmethod + def get_auto_roles_by_message_id(self, id: int) -> AutoRole: pass + + @abstractmethod + def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass + + @abstractmethod + def add_auto_role(self, server: AutoRole): pass + + @abstractmethod + def update_auto_role(self, server: AutoRole): pass + + @abstractmethod + def delete_auto_role(self, server: AutoRole): pass + + @abstractmethod + def get_auto_role_rules(self) -> List[AutoRoleRule]: pass + + @abstractmethod + def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: pass + + @abstractmethod + def get_auto_role_rules_by_auto_role_id(self, id: int) -> AutoRoleRule: pass + + @abstractmethod + def find_auto_role_rules_by_auto_role_id(self, id: int) -> Optional[AutoRoleRule]: pass + + @abstractmethod + def add_auto_role_rule(self, server: AutoRoleRule): pass + + @abstractmethod + def update_auto_role_rule(self, server: AutoRoleRule): pass + + @abstractmethod + def delete_auto_role_rule(self, server: AutoRoleRule): pass diff --git a/src/bot_data/data_module.py b/src/bot_data/data_module.py index 099ba2fd..99fc12ad 100644 --- a/src/bot_data/data_module.py +++ b/src/bot_data/data_module.py @@ -5,12 +5,14 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC 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 from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService from bot_data.service.client_repository_service import ClientRepositoryService from bot_data.service.known_user_repository_service import KnownUserRepositoryService from bot_data.service.server_repository_service import ServerRepositoryService @@ -33,4 +35,5 @@ class DataModule(ModuleABC): services.add_transient(ClientRepositoryABC, ClientRepositoryService) services.add_transient(KnownUserRepositoryABC, KnownUserRepositoryService) services.add_transient(UserJoinedServerRepositoryABC, UserJoinedServerRepositoryService) - services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService) \ No newline at end of file + services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService) + services.add_transient(AutoRoleRepositoryABC, AutoRoleRepositoryService) diff --git a/src/bot_data/model/auto_role.py b/src/bot_data/model/auto_role.py index 9d02e514..b81c1c76 100644 --- a/src/bot_data/model/auto_role.py +++ b/src/bot_data/model/auto_role.py @@ -35,6 +35,13 @@ class AutoRole(TableABC): WHERE `ServerId` = {id}; """) + @staticmethod + def get_select_by_message_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `AutoRoles` + WHERE `DiscordMessageId` = {id}; + """) + @property def insert_string(self) -> str: return str(f""" diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index f123615a..15ec3e32 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -30,10 +30,10 @@ class AutoRoleRule(TableABC): """) @staticmethod - def get_select_by_server_id_string(id: int) -> str: + def get_select_by_auto_role_id_string(id: int) -> str: return str(f""" SELECT * FROM `AutoRoleRules` - WHERE `ServerId` = {id}; + WHERE `AutoRoleId` = {id}; """) @property diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 728d2bc6..654d7635 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -4,69 +4,176 @@ from cpl_core.database.context import DatabaseContextABC from cpl_query.extension import List from bot_core.logging.database_logger import DatabaseLogger -from bot_data.abc.server_repository_abc import ServerRepositoryABC -from bot_data.model.server import Server +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_data.model.auto_role_rule import AutoRoleRule -class ServerRepositoryService(ServerRepositoryABC): +class AutoRoleRepositoryService(AutoRoleRepositoryABC): def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC): self._logger = logger self._context = db_context - ServerRepositoryABC.__init__(self) + AutoRoleRepositoryABC.__init__(self) - def get_servers(self) -> List[Server]: - servers = List(Server) - self._logger.trace(__name__, f'Send SQL command: {Server.get_select_all_string()}') - results = self._context.select(Server.get_select_all_string()) + def get_auto_roles(self) -> List[AutoRole]: + auto_roles = List(AutoRole) + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_all_string()}') + results = self._context.select(AutoRole.get_select_all_string()) for result in results: - servers.append(Server( + auto_roles.append(AutoRole( result[1], + result[2], + result[3], + result[4], id=result[0] )) - return servers + return auto_roles - def get_server_by_id(self, server_id: int) -> Server: - self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_id_string(server_id)}') - result = self._context.select(Server.get_select_by_id_string(server_id))[0] - return Server( - result[1], - id=result[0] - ) - - def get_server_by_discord_id(self, discord_id: int) -> Server: - self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}') - result = self._context.select(Server.get_select_by_discord_id_string(discord_id))[0] - return Server( - result[1], - id=result[0] - ) - - def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]: - self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}') - result = self._context.select(Server.get_select_by_discord_id_string(discord_id)) - if result is None or len(result) == 0: - return None - - result = result[0] - - return Server( + def get_auto_role_by_id(self, id: int) -> AutoRole: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_id_string(id))[0] + return AutoRole( result[1], result[2], result[3], + result[4], id=result[0] ) - - def add_server(self, server: Server): - self._logger.trace(__name__, f'Send SQL command: {server.insert_string}') - self._context.cursor.execute(server.insert_string) - - def update_server(self, server: Server): - self._logger.trace(__name__, f'Send SQL command: {server.udpate_string}') - self._context.cursor.execute(server.udpate_string) - - def delete_server(self, server: Server): - self._logger.trace(__name__, f'Send SQL command: {server.delete_string}') - self._context.cursor.execute(server.delete_string) + + def get_auto_roles_by_server_id(self, id: int) -> AutoRole: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0] + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + + def find_auto_roles_by_server_id(self, id: int) -> Optional[AutoRole]: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_server_id_string(id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + + def get_auto_roles_by_message_id(self, id: int) -> AutoRole: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0] + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + + def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_message_id_string(id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + + def add_auto_role(self, auto_role: AutoRole): + self._logger.trace(__name__, f'Send SQL command: {auto_role.insert_string}') + self._context.cursor.execute(auto_role.insert_string) + + def update_auto_role(self, auto_role: AutoRole): + self._logger.trace(__name__, f'Send SQL command: {auto_role.udpate_string}') + self._context.cursor.execute(auto_role.udpate_string) + + def delete_auto_role(self, auto_role: AutoRole): + self._logger.trace(__name__, f'Send SQL command: {auto_role.delete_string}') + self._context.cursor.execute(auto_role.delete_string) + + def get_auto_role_rules(self) -> List[AutoRoleRule]: + auto_role_rules = List(AutoRoleRule) + self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_all_string()}') + results = self._context.select(AutoRoleRule.get_select_all_string()) + for result in results: + auto_role_rules.append(AutoRoleRule( + result[1], + result[2], + result[3], + result[4], + result[5], + id=result[0] + )) + + return auto_role_rules + + def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: + self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_id_string(id)}') + result = self._context.select(AutoRoleRule.get_select_by_id_string(id))[0] + return AutoRoleRule( + result[1], + result[2], + result[3], + result[4], + result[5], + id=result[0] + ) + + def get_auto_role_rules_by_auto_role_id(self, id: int) -> AutoRoleRule: + self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_auto_role_id_string(id)}') + result = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id))[0] + return AutoRoleRule( + result[1], + result[2], + result[3], + result[4], + result[5], + id=result[0] + ) + + def find_auto_role_rules_by_auto_role_id(self, id: int) -> Optional[AutoRoleRule]: + self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_auto_role_id_string(id)}') + result = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return AutoRoleRule( + result[1], + result[2], + result[3], + result[4], + result[5], + id=result[0] + ) + + def add_auto_role_rule(self, auto_role_rule: AutoRoleRule): + self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') + self._context.cursor.execute(auto_role_rule.delete_string) + + def update_auto_role_rule(self, auto_role_rule: AutoRoleRule): + self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') + self._context.cursor.execute(auto_role_rule.delete_string) + + def delete_auto_role_rule(self, auto_role_rule: AutoRoleRule): + self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') + self._context.cursor.execute(auto_role_rule.delete_string) From fa98f081611085fdf5d540a8e1d030f9acae494c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 18:24:29 +0200 Subject: [PATCH 03/20] Added auto role add #54 --- src/bot_data/abc/auto_role_repository_abc.py | 5 +- src/bot_data/abc/migration_abc.py | 1 + src/bot_data/migration/auto_role_migration.py | 4 +- src/bot_data/migration/initial_migration.py | 2 + src/bot_data/model/auto_role.py | 14 ++++- src/bot_data/model/auto_role_rule.py | 18 +++++-- .../service/auto_role_repository_service.py | 39 +++++--------- src/bot_data/service/migration_service.py | 3 +- src/modules/autorole/auto_role_module.py | 6 ++- src/modules/autorole/events/__init__.py | 0 .../events/auto_role_on_raw_reaction_add.py | 34 ++++++++++++ src/modules/autorole/helper/__init__.py | 0 .../autorole/helper/reaction_handler.py | 52 +++++++++++++++++++ 13 files changed, 139 insertions(+), 39 deletions(-) create mode 100644 src/modules/autorole/events/__init__.py create mode 100644 src/modules/autorole/events/auto_role_on_raw_reaction_add.py create mode 100644 src/modules/autorole/helper/__init__.py create mode 100644 src/modules/autorole/helper/reaction_handler.py diff --git a/src/bot_data/abc/auto_role_repository_abc.py b/src/bot_data/abc/auto_role_repository_abc.py index 6afa0730..7b9c0ed1 100644 --- a/src/bot_data/abc/auto_role_repository_abc.py +++ b/src/bot_data/abc/auto_role_repository_abc.py @@ -47,10 +47,7 @@ class AutoRoleRepositoryABC(ABC): def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: pass @abstractmethod - def get_auto_role_rules_by_auto_role_id(self, id: int) -> AutoRoleRule: pass - - @abstractmethod - def find_auto_role_rules_by_auto_role_id(self, id: int) -> Optional[AutoRoleRule]: pass + def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass @abstractmethod def add_auto_role_rule(self, server: AutoRoleRule): pass diff --git a/src/bot_data/abc/migration_abc.py b/src/bot_data/abc/migration_abc.py index 53b1696e..a3ce7ddc 100644 --- a/src/bot_data/abc/migration_abc.py +++ b/src/bot_data/abc/migration_abc.py @@ -2,6 +2,7 @@ from abc import ABC, abstractmethod class MigrationABC(ABC): + name = None @abstractmethod def __init__(self): pass diff --git a/src/bot_data/migration/auto_role_migration.py b/src/bot_data/migration/auto_role_migration.py index 9aad5f72..2294ff95 100644 --- a/src/bot_data/migration/auto_role_migration.py +++ b/src/bot_data/migration/auto_role_migration.py @@ -4,8 +4,10 @@ from bot_data.db_context import DBContext class AutoRoleMigration(MigrationABC): + name = '0.2.1_AutoRoleMigration' def __init__(self, logger: DatabaseLogger, db: DBContext): + MigrationABC.__init__(self) self._logger = logger self._db = db self._cursor = db.cursor @@ -31,7 +33,7 @@ class AutoRoleMigration(MigrationABC): CREATE TABLE IF NOT EXISTS `AutoRoleRules` ( `AutoRoleRuleId` BIGINT NOT NULL AUTO_INCREMENT, `AutoRoleId` BIGINT, - `DiscordEmojiId` BIGINT NOT NULL, + `DiscordEmojiName` VARCHAR(64), `DiscordRoleId` BIGINT NOT NULL, `CreatedAt` DATETIME(6), `LastModifiedAt` DATETIME(6), diff --git a/src/bot_data/migration/initial_migration.py b/src/bot_data/migration/initial_migration.py index 76dd481d..54c2ae8f 100644 --- a/src/bot_data/migration/initial_migration.py +++ b/src/bot_data/migration/initial_migration.py @@ -4,8 +4,10 @@ from bot_data.db_context import DBContext class InitialMigration(MigrationABC): + name = '0.1_InitialMigration' def __init__(self, logger: DatabaseLogger, db: DBContext): + MigrationABC.__init__(self) self._logger = logger self._db = db self._cursor = db.cursor diff --git a/src/bot_data/model/auto_role.py b/src/bot_data/model/auto_role.py index b81c1c76..26d5dba2 100644 --- a/src/bot_data/model/auto_role.py +++ b/src/bot_data/model/auto_role.py @@ -14,7 +14,19 @@ class AutoRole(TableABC): TableABC.__init__(self) self._created_at = created_at if created_at is not None else self._created_at self._modified_at = modified_at if modified_at is not None else self._modified_at - + + @property + def auto_role_id(self) -> int: + return self._auto_role_id + + @property + def server_id(self) -> int: + return self._server_id + + @property + def discord_message_id(self) -> int: + return self._discord_message_id + @staticmethod def get_select_all_string() -> str: return str(f""" diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index 15ec3e32..ca9fce32 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -6,16 +6,24 @@ from cpl_core.database import TableABC class AutoRoleRule(TableABC): - def __init__(self, auto_role_id: int, discord_emoji_id: int, discord_role_id: int, created_at: datetime=None, modified_at: datetime=None, id=0): + def __init__(self, auto_role_id: int, discord_emoji_name: str, discord_role_id: int, created_at: datetime=None, modified_at: datetime=None, id=0): self._auto_role_rule_id = id self._auto_role_id = auto_role_id - self._discord_emoji_id = discord_emoji_id + self._discord_emoji_name = discord_emoji_name self._discord_role_id = discord_role_id TableABC.__init__(self) self._created_at = created_at if created_at is not None else self._created_at self._modified_at = modified_at if modified_at is not None else self._modified_at + @property + def emoji_name(self): + return self._discord_emoji_name + + @property + def role_id(self): + return self._discord_role_id + @staticmethod def get_select_all_string() -> str: return str(f""" @@ -40,10 +48,10 @@ class AutoRoleRule(TableABC): def insert_string(self) -> str: return str(f""" INSERT INTO `AutoRoleRules` ( - `AutoRoleId`, `DiscordEmojiId`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` + `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._auto_role_id}, - {self._discord_emoji_id}, + {self._discord_emoji_name}, {self._discord_role_id}, '{self._created_at}', '{self._modified_at}' @@ -55,7 +63,7 @@ class AutoRoleRule(TableABC): return str(f""" UPDATE `AutoRoleRules` SET `AutoRoleId` = {self._auto_role_id}, - SET `DiscordEmojiId` = {self._discord_emoji_id}, + SET `DiscordEmojiName` = {self._discord_emoji_name}, SET `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' WHERE `AutoRoleRuleId` = {self._auto_role_id}; diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 654d7635..7b02bc72 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -137,34 +137,21 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): id=result[0] ) - def get_auto_role_rules_by_auto_role_id(self, id: int) -> AutoRoleRule: + def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: + auto_role_rules = List(AutoRoleRule) self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_auto_role_id_string(id)}') - result = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id))[0] - return AutoRoleRule( - result[1], - result[2], - result[3], - result[4], - result[5], - id=result[0] - ) + results = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id)) + for result in results: + auto_role_rules.append(AutoRoleRule( + result[1], + result[2], + result[3], + result[4], + result[5], + id=result[0] + )) - def find_auto_role_rules_by_auto_role_id(self, id: int) -> Optional[AutoRoleRule]: - self._logger.trace(__name__, f'Send SQL command: {AutoRoleRule.get_select_by_auto_role_id_string(id)}') - result = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id)) - if result is None or len(result) == 0: - return None - - result = result[0] - - return AutoRoleRule( - result[1], - result[2], - result[3], - result[4], - result[5], - id=result[0] - ) + return auto_role_rules def add_auto_role_rule(self, auto_role_rule: AutoRoleRule): self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') diff --git a/src/bot_data/service/migration_service.py b/src/bot_data/service/migration_service.py index fc2d5913..9df395bf 100644 --- a/src/bot_data/service/migration_service.py +++ b/src/bot_data/service/migration_service.py @@ -2,6 +2,7 @@ from typing import Type from cpl_core.database.context import DatabaseContextABC from cpl_core.dependency_injection import ServiceProviderABC +from cpl_query.extension import List from bot_core.logging.database_logger import DatabaseLogger from bot_data.abc.migration_abc import MigrationABC @@ -17,7 +18,7 @@ class MigrationService: self._db = db self._cursor = db.cursor - self._migrations: list[Type[MigrationABC]] = MigrationABC.__subclasses__() + self._migrations = List(type, MigrationABC.__subclasses__()).order_by(lambda x: x.name) def migrate(self): self._logger.info(__name__, f"Running Migrations") diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index 9afd273e..c8f83990 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -1,10 +1,13 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent +from modules.autorole.helper.reaction_handler import ReactionHandler class AutoRoleModule(ModuleABC): @@ -16,6 +19,7 @@ class AutoRoleModule(ModuleABC): pass def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + services.add_transient(ReactionHandler) # commands # events - pass + self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) diff --git a/src/modules/autorole/events/__init__.py b/src/modules/autorole/events/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/modules/autorole/events/auto_role_on_raw_reaction_add.py b/src/modules/autorole/events/auto_role_on_raw_reaction_add.py new file mode 100644 index 00000000..586f87fd --- /dev/null +++ b/src/modules/autorole/events/auto_role_on_raw_reaction_add.py @@ -0,0 +1,34 @@ +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_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.autorole.helper.reaction_handler import ReactionHandler + + +class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + reaction_handler: ReactionHandler + ): + OnRawReactionAddABC.__init__(self) + + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + self._reaction_handler = reaction_handler + + async def on_raw_reaction_add(self, payload: RawReactionActionEvent): + self._logger.debug(__name__, f'Module {type(self)} started') + + await self._reaction_handler.handle(payload, 'add') + + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/src/modules/autorole/helper/__init__.py b/src/modules/autorole/helper/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/modules/autorole/helper/reaction_handler.py b/src/modules/autorole/helper/reaction_handler.py new file mode 100644 index 00000000..528a8968 --- /dev/null +++ b/src/modules/autorole/helper/reaction_handler.py @@ -0,0 +1,52 @@ +import discord +from cpl_core.logging import LoggerABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List +from discord import RawReactionActionEvent + +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 + + +class ReactionHandler: + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC + ): + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + + self._message_ids = self._auto_roles.get_auto_roles().select(lambda x: x.discord_message_id) + self._roles = self._auto_roles.get_auto_roles() + + async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: + if payload.message_id not in self._message_ids: + return + + guild = self._bot.get_guild(payload.guild_id) + user = await guild.fetch_member(payload.user_id) + if user.bot: + return + + emoji = payload.emoji.name + auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id).first_or_default() + if auto_role is None: + return + + rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id) + + for rule in rules: + if emoji != rule.emoji_name: + continue + + role = guild.get_role(rule.role_id) + self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') + await user.add_roles(role) From d0ed413150af09f5c6722bd1035f052061a71caa Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 18:27:07 +0200 Subject: [PATCH 04/20] Added auto role remove #54 --- src/modules/autorole/auto_role_module.py | 2 ++ .../auto_role_on_raw_reaction_remove.py | 35 +++++++++++++++++++ .../autorole/helper/reaction_handler.py | 12 +++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/modules/autorole/events/auto_role_on_raw_reaction_remove.py diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index c8f83990..3206b7e8 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -7,6 +7,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent +from modules.autorole.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent from modules.autorole.helper.reaction_handler import ReactionHandler @@ -23,3 +24,4 @@ class AutoRoleModule(ModuleABC): # commands # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) + self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py b/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py new file mode 100644 index 00000000..fbd86822 --- /dev/null +++ b/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py @@ -0,0 +1,35 @@ +from cpl_core.logging import LoggerABC +from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC +from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC +from cpl_discord.service import DiscordBotServiceABC +from discord import RawReactionActionEvent + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.autorole.helper.reaction_handler import ReactionHandler + + +class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): + + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + reaction_handler: ReactionHandler + ): + OnRawReactionRemoveABC.__init__(self) + + self._logger = logger + self._bot = bot + self._servers = servers + self._auto_roles = auto_roles + self._reaction_handler = reaction_handler + + async def on_raw_reaction_remove(self, payload: RawReactionActionEvent): + self._logger.debug(__name__, f'Module {type(self)} started') + + await self._reaction_handler.handle(payload, 'remove') + + self._logger.debug(__name__, f'Module {type(self)} stopped') diff --git a/src/modules/autorole/helper/reaction_handler.py b/src/modules/autorole/helper/reaction_handler.py index 528a8968..383044cb 100644 --- a/src/modules/autorole/helper/reaction_handler.py +++ b/src/modules/autorole/helper/reaction_handler.py @@ -47,6 +47,12 @@ class ReactionHandler: if emoji != rule.emoji_name: continue - role = guild.get_role(rule.role_id) - self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') - await user.add_roles(role) + if r_type == 'add': + role = guild.get_role(rule.role_id) + self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') + await user.add_roles(role) + + if r_type == 'remove': + role = guild.get_role(rule.role_id) + self._logger.debug(__name__, f'Remove role {role.name} to {user.name}') + await user.remove_roles(role) From 9daa0b1c54b2e634e43f55c9f602fe73510406ad Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 19:26:48 +0200 Subject: [PATCH 05/20] Added auto-role list #54 --- src/bot/translation/de.json | 8 ++ src/modules/autorole/auto_role_module.py | 2 + src/modules/autorole/command/__init__.py | 0 .../autorole/command/auto_role_command.py | 82 +++++++++++++++++++ src/modules/base/command/info_command.py | 2 +- 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/modules/autorole/command/__init__.py create mode 100644 src/modules/autorole/command/auto_role_command.py diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 02da2823..d009e38e 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -53,6 +53,14 @@ "shutdown_message": "Trauert nicht um mich, es war eine logische Entscheidung. Das Wohl von Vielen, es wiegt schwerer als das Wohl von Wenigen oder eines Einzelnen. Ich war es und ich werde es immer sein, Euer Freund. Lebt lange und in Frieden :)", "deploy_message": "Der neue Stand wurde hochgeladen." }, + "auto_role": { + "list": { + "title": "beobachtete Nachrichten:", + "description": "Von auto-role beobachtete Nachrichten:", + "auto_role_id": "auto-role Id", + "message_id": "Nachricht-Id" + } + }, "moderator": { "purge_message": "Na gut..., ich lösche alle Nachrichten wenns sein muss." }, diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index 3206b7e8..2d6c5377 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -6,6 +6,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from modules.autorole.command.auto_role_command import AutoRoleCommand from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent from modules.autorole.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent from modules.autorole.helper.reaction_handler import ReactionHandler @@ -22,6 +23,7 @@ class AutoRoleModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(ReactionHandler) # commands + self._dc.add_command(AutoRoleCommand) # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/command/__init__.py b/src/modules/autorole/command/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/modules/autorole/command/auto_role_command.py b/src/modules/autorole/command/auto_role_command.py new file mode 100644 index 00000000..75af4a1b --- /dev/null +++ b/src/modules/autorole/command/auto_role_command.py @@ -0,0 +1,82 @@ +import discord +from cpl_discord.command import DiscordCommandABC +from cpl_discord.service import DiscordBotServiceABC +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.logging.command_logger import CommandLogger +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC + + +class AutoRoleCommand(DiscordCommandABC): + + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + translate: TranslatePipe, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._t = translate + self._servers = servers + self._auto_roles = auto_roles + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.hybrid_group(name="auto-role") + @commands.guild_only() + async def auto_role(self, ctx: Context): + pass + + @auto_role.command() + @commands.guild_only() + 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) + + embed = discord.Embed( + title=self._t.transform('modules.auto_role.list.title'), + description=self._t.transform('modules.auto_role.list.description'), + color=int('ef9d0d', 16) + ) + + auto_role_id = '' + for id in self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id): + auto_role_id += f'\n{id}' + + message_id = '' + for id in self._auto_roles.get_auto_roles().select(lambda x: x.discord_message_id): + message_id += f'\n{id}' + embed.add_field(name=self._t.transform('modules.auto_role.list.auto_role_id'), value=auto_role_id, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True) + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f'Finished command auto-role list') + + @auto_role.command() + @commands.guild_only() + async def add(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role add {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + @auto_role.command() + @commands.guild_only() + async def remove(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role remove {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return diff --git a/src/modules/base/command/info_command.py b/src/modules/base/command/info_command.py index f7183cde..23587778 100644 --- a/src/modules/base/command/info_command.py +++ b/src/modules/base/command/info_command.py @@ -62,7 +62,7 @@ class InfoCommand(DiscordCommandABC): embed.add_field(name=self._t.transform('modules.base.info.fields.moved_users_count'), value=client.moved_users_count) from bot.module_list import ModuleList modules = ModuleList.get_modules() - modules = modules.select(lambda x: x.replace('Module', '')) + modules = modules.select(lambda x: x.__name__.replace('Module', '')) embed.add_field(name=self._t.transform('modules.base.info.fields.modules'), value='\n'.join(modules), inline=False) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) From b35b023b0260d6ec7f51573ee828d7a36a03762a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 19:47:06 +0200 Subject: [PATCH 06/20] [WIP] Added auto-role-rules list #54 --- src/modules/autorole/auto_role_module.py | 2 + .../autorole/command/auto_role_command.py | 2 +- .../command/auto_role_rule_command.py | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/modules/autorole/command/auto_role_rule_command.py diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/autorole/auto_role_module.py index 2d6c5377..61f51b8e 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/autorole/auto_role_module.py @@ -7,6 +7,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.autorole.command.auto_role_command import AutoRoleCommand +from modules.autorole.command.auto_role_rule_command import AutoRoleRuleCommand from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent from modules.autorole.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent from modules.autorole.helper.reaction_handler import ReactionHandler @@ -24,6 +25,7 @@ class AutoRoleModule(ModuleABC): services.add_transient(ReactionHandler) # commands self._dc.add_command(AutoRoleCommand) + self._dc.add_command(AutoRoleRuleCommand) # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/command/auto_role_command.py b/src/modules/autorole/command/auto_role_command.py index 75af4a1b..0960c6eb 100644 --- a/src/modules/autorole/command/auto_role_command.py +++ b/src/modules/autorole/command/auto_role_command.py @@ -41,7 +41,7 @@ class AutoRoleCommand(DiscordCommandABC): async def auto_role(self, ctx: Context): pass - @auto_role.command() + @auto_role.command(alias='auto-roles') @commands.guild_only() async def list(self, ctx: Context, wait: int = None): self._logger.debug(__name__, f'Received command auto-role list {ctx}') diff --git a/src/modules/autorole/command/auto_role_rule_command.py b/src/modules/autorole/command/auto_role_rule_command.py new file mode 100644 index 00000000..3e2ab240 --- /dev/null +++ b/src/modules/autorole/command/auto_role_rule_command.py @@ -0,0 +1,81 @@ +from typing import List + +import discord +from cpl_discord.command import DiscordCommandABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_translation import TranslatePipe +from discord import app_commands +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.logging.command_logger import CommandLogger +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC + + +class AutoRoleRuleCommand(DiscordCommandABC): + + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + translate: TranslatePipe, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._t = translate + self._servers = servers + self._auto_roles = auto_roles + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.hybrid_group(name="auto-role-rule") + @commands.guild_only() + async def auto_role(self, ctx: Context): + pass + + @auto_role.command(alias='auto-role-rules') + @commands.guild_only() + 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) + + embed = discord.Embed( + title=self._t.transform('modules.auto_role.list.title'), + description=self._t.transform('modules.auto_role.list.description'), + color=int('ef9d0d', 16) + ) + + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f'Finished command auto-role list') + + @list.autocomplete('auto_role') + async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) + return [app_commands.Choice(name=key, value=key) for key in auto_roles] + + @auto_role.command() + @commands.guild_only() + async def add(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role-rule add {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + @auto_role.command() + @commands.guild_only() + async def remove(self, ctx: Context): + self._logger.debug(__name__, f'Received command auto-role-rule remove {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return From 21c34436b9024f6aba2f6611da659102f8bd6021 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 3 Oct 2022 22:22:51 +0200 Subject: [PATCH 07/20] Improved auto-role-rule list #54 --- src/bot/translation/de.json | 17 ++++++++++++++++- .../autorole/command/auto_role_command.py | 14 +++++++++----- .../autorole/command/auto_role_rule_command.py | 18 +++++++++++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index d009e38e..db98a9a7 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -55,10 +55,25 @@ }, "auto_role": { "list": { - "title": "beobachtete Nachrichten:", + "title": "Beobachtete Nachrichten:", "description": "Von auto-role beobachtete Nachrichten:", "auto_role_id": "auto-role Id", "message_id": "Nachricht-Id" + }, + "rule": { + "list": { + "title": "auto-role Regeln:", + "description": "Von auto-role angewendete Regeln:", + "auto_role_id": "auto-role Id", + "emoji": "Emoji", + "role": "Rolle" + }, + "error": { + "id_not_found": "Kein auto-role Eintrag mit der Id gefunden!" + } + }, + "error": { + "nothing_found": "Keine auto-role Einträge gefunden." } }, "moderator": { diff --git a/src/modules/autorole/command/auto_role_command.py b/src/modules/autorole/command/auto_role_command.py index 0960c6eb..733d413f 100644 --- a/src/modules/autorole/command/auto_role_command.py +++ b/src/modules/autorole/command/auto_role_command.py @@ -54,14 +54,18 @@ class AutoRoleCommand(DiscordCommandABC): description=self._t.transform('modules.auto_role.list.description'), color=int('ef9d0d', 16) ) + auto_roles = self._auto_roles.get_auto_roles() + if auto_roles.count() < 1: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.error.nothing_found')) + self._logger.trace(__name__, f'Finished command auto-role list') + return auto_role_id = '' - for id in self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id): - auto_role_id += f'\n{id}' - message_id = '' - for id in self._auto_roles.get_auto_roles().select(lambda x: x.discord_message_id): - message_id += f'\n{id}' + for auto_role in auto_roles: + auto_role_id += f'\n{auto_role.auto_role_id}' + message_id += f'\n{auto_role.discord_message_id}' + embed.add_field(name=self._t.transform('modules.auto_role.list.auto_role_id'), value=auto_role_id, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) diff --git a/src/modules/autorole/command/auto_role_rule_command.py b/src/modules/autorole/command/auto_role_rule_command.py index 3e2ab240..3027acaf 100644 --- a/src/modules/autorole/command/auto_role_rule_command.py +++ b/src/modules/autorole/command/auto_role_rule_command.py @@ -57,9 +57,25 @@ class AutoRoleRuleCommand(DiscordCommandABC): description=self._t.transform('modules.auto_role.list.description'), color=int('ef9d0d', 16) ) + rules = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role) + if rules.count() < 1: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.error.id_not_found')) + self._logger.trace(__name__, f'Finished command auto-role-rule list') + return + auto_role_rule_id = '' + emoji = '' + role = '' + for rule in rules: + auto_role_rule_id += f'\n{rule.role_id}' + emoji += f'\n{rule.emoji_name}' + role += f'\n{ctx.guild.get_role(rule.role_id)}' + + embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_id'), value=auto_role_rule_id, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.rule.list.emoji'), value=emoji, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.rule.list.role'), value=role, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) - self._logger.trace(__name__, f'Finished command auto-role list') + self._logger.trace(__name__, f'Finished command auto-role-rule list') @list.autocomplete('auto_role') async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: From 97e4f8ec292dc8e402910de06c78d0f997246b3b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 13:18:55 +0200 Subject: [PATCH 08/20] Added auto-role rule list #54 --- cpl-workspace.json | 2 +- .../config/appsettings.edrafts-pc-ubuntu.json | 2 +- src/bot/module_list.py | 5 +- .../{autorole => auto_role}/__init__.py | 0 .../{autorole => auto_role}/auto-role.json | 0 .../auto_role_module.py | 12 ++- .../command/__init__.py | 0 .../command/auto_role_group.py} | 57 ++++++++---- .../events/__init__.py | 0 .../events/auto_role_on_raw_reaction_add.py | 2 +- .../auto_role_on_raw_reaction_remove.py | 2 +- .../helper/__init__.py | 0 .../helper/reaction_handler.py | 0 .../autorole/command/auto_role_command.py | 86 ------------------- 14 files changed, 51 insertions(+), 117 deletions(-) rename src/modules/{autorole => auto_role}/__init__.py (100%) rename src/modules/{autorole => auto_role}/auto-role.json (100%) rename src/modules/{autorole => auto_role}/auto_role_module.py (68%) rename src/modules/{autorole => auto_role}/command/__init__.py (100%) rename src/modules/{autorole/command/auto_role_rule_command.py => auto_role/command/auto_role_group.py} (70%) rename src/modules/{autorole => auto_role}/events/__init__.py (100%) rename src/modules/{autorole => auto_role}/events/auto_role_on_raw_reaction_add.py (94%) rename src/modules/{autorole => auto_role}/events/auto_role_on_raw_reaction_remove.py (94%) rename src/modules/{autorole => auto_role}/helper/__init__.py (100%) rename src/modules/{autorole => auto_role}/helper/reaction_handler.py (100%) delete mode 100644 src/modules/autorole/command/auto_role_command.py diff --git a/cpl-workspace.json b/cpl-workspace.json index 663b790a..9841dd57 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -6,7 +6,7 @@ "bot-core": "src/bot_core/bot-core.json", "bot-data": "src/bot_data/bot-data.json", "admin": "src/modules/admin/admin.json", - "auto-role": "src/modules/admin/auto-role.json", + "auto-role": "src/modules/auto_role/auto-role.json", "base": "src/modules/base/base.json", "boot-log": "src/modules/boot_log/boot-log.json", "database": "src/modules/database/database.json", diff --git a/src/bot/config/appsettings.edrafts-pc-ubuntu.json b/src/bot/config/appsettings.edrafts-pc-ubuntu.json index 076ae8d8..a05aac5f 100644 --- a/src/bot/config/appsettings.edrafts-pc-ubuntu.json +++ b/src/bot/config/appsettings.edrafts-pc-ubuntu.json @@ -2,7 +2,7 @@ "LoggingSettings": { "Path": "logs/", "Filename": "bot.log", - "ConsoleLogLevel": "DEBUG", + "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" }, "BotLoggingSettings": { diff --git a/src/bot/module_list.py b/src/bot/module_list.py index 6e9670ff..5b8f4e5b 100644 --- a/src/bot/module_list.py +++ b/src/bot/module_list.py @@ -4,7 +4,7 @@ from bot_core.core_extension.core_extension_module import CoreExtensionModule from bot_core.core_module import CoreModule from bot_data.data_module import DataModule from modules.admin.admin_module import AdminModule -from modules.autorole.auto_role_module import AutoRoleModule +from modules.auto_role.auto_role_module import AutoRoleModule from modules.base.base_module import BaseModule from modules.boot_log.boot_log_module import BootLogModule from modules.database.database_module import DatabaseModule @@ -26,6 +26,7 @@ class ModuleList: DatabaseModule, ModeratorModule, PermissionModule, + # has to be last! + BootLogModule, CoreExtensionModule, - BootLogModule # has to be last! ]) diff --git a/src/modules/autorole/__init__.py b/src/modules/auto_role/__init__.py similarity index 100% rename from src/modules/autorole/__init__.py rename to src/modules/auto_role/__init__.py diff --git a/src/modules/autorole/auto-role.json b/src/modules/auto_role/auto-role.json similarity index 100% rename from src/modules/autorole/auto-role.json rename to src/modules/auto_role/auto-role.json diff --git a/src/modules/autorole/auto_role_module.py b/src/modules/auto_role/auto_role_module.py similarity index 68% rename from src/modules/autorole/auto_role_module.py rename to src/modules/auto_role/auto_role_module.py index 61f51b8e..cf53c6c4 100644 --- a/src/modules/autorole/auto_role_module.py +++ b/src/modules/auto_role/auto_role_module.py @@ -6,11 +6,10 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum -from modules.autorole.command.auto_role_command import AutoRoleCommand -from modules.autorole.command.auto_role_rule_command import AutoRoleRuleCommand -from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent -from modules.autorole.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent -from modules.autorole.helper.reaction_handler import ReactionHandler +from modules.auto_role.command.auto_role_group import AutoRoleGroup +from modules.auto_role.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent +from modules.auto_role.events.auto_role_on_raw_reaction_remove import AutoRoleOnRawReactionRemoveEvent +from modules.auto_role.helper.reaction_handler import ReactionHandler class AutoRoleModule(ModuleABC): @@ -24,8 +23,7 @@ class AutoRoleModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(ReactionHandler) # commands - self._dc.add_command(AutoRoleCommand) - self._dc.add_command(AutoRoleRuleCommand) + self._dc.add_command(AutoRoleGroup) # events self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_remove.value, AutoRoleOnRawReactionRemoveEvent) diff --git a/src/modules/autorole/command/__init__.py b/src/modules/auto_role/command/__init__.py similarity index 100% rename from src/modules/autorole/command/__init__.py rename to src/modules/auto_role/command/__init__.py diff --git a/src/modules/autorole/command/auto_role_rule_command.py b/src/modules/auto_role/command/auto_role_group.py similarity index 70% rename from src/modules/autorole/command/auto_role_rule_command.py rename to src/modules/auto_role/command/auto_role_group.py index 3027acaf..29072ae9 100644 --- a/src/modules/autorole/command/auto_role_rule_command.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -15,7 +15,7 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -class AutoRoleRuleCommand(DiscordCommandABC): +class AutoRoleGroup(DiscordCommandABC): def __init__( self, @@ -39,12 +39,47 @@ class AutoRoleRuleCommand(DiscordCommandABC): self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - @commands.hybrid_group(name="auto-role-rule") + @commands.hybrid_group(name="auto-role") @commands.guild_only() - async def auto_role(self, ctx: Context): + async def auto_role(self, ctx: Context): pass + + @auto_role.command(alias='auto-roles') + @commands.guild_only() + 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) + + embed = discord.Embed( + title=self._t.transform('modules.auto_role.list.title'), + description=self._t.transform('modules.auto_role.list.description'), + color=int('ef9d0d', 16) + ) + auto_roles = self._auto_roles.get_auto_roles() + if auto_roles.count() < 1: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.error.nothing_found')) + self._logger.trace(__name__, f'Finished command auto-role list') + return + + auto_role_id = '' + message_id = '' + for auto_role in auto_roles: + auto_role_id += f'\n{auto_role.auto_role_id}' + message_id += f'\n{auto_role.discord_message_id}' + + embed.add_field(name=self._t.transform('modules.auto_role.list.auto_role_id'), value=auto_role_id, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True) + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f'Finished command auto-role list') pass - @auto_role.command(alias='auto-role-rules') + @auto_role.group() + @commands.guild_only() + async def rule(self, ctx: Context): + pass + + @rule.command(alias='rules') @commands.guild_only() async def list(self, ctx: Context, auto_role: int, wait: int = None): self._logger.debug(__name__, f'Received command auto-role-rule list {ctx}') @@ -81,17 +116,3 @@ class AutoRoleRuleCommand(DiscordCommandABC): async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) return [app_commands.Choice(name=key, value=key) for key in auto_roles] - - @auto_role.command() - @commands.guild_only() - async def add(self, ctx: Context): - self._logger.debug(__name__, f'Received command auto-role-rule add {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - - @auto_role.command() - @commands.guild_only() - async def remove(self, ctx: Context): - self._logger.debug(__name__, f'Received command auto-role-rule remove {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return diff --git a/src/modules/autorole/events/__init__.py b/src/modules/auto_role/events/__init__.py similarity index 100% rename from src/modules/autorole/events/__init__.py rename to src/modules/auto_role/events/__init__.py diff --git a/src/modules/autorole/events/auto_role_on_raw_reaction_add.py b/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py similarity index 94% rename from src/modules/autorole/events/auto_role_on_raw_reaction_add.py rename to src/modules/auto_role/events/auto_role_on_raw_reaction_add.py index 586f87fd..f86de1f0 100644 --- a/src/modules/autorole/events/auto_role_on_raw_reaction_add.py +++ b/src/modules/auto_role/events/auto_role_on_raw_reaction_add.py @@ -5,7 +5,7 @@ from discord import RawReactionActionEvent from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -from modules.autorole.helper.reaction_handler import ReactionHandler +from modules.auto_role.helper.reaction_handler import ReactionHandler class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): diff --git a/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py b/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py similarity index 94% rename from src/modules/autorole/events/auto_role_on_raw_reaction_remove.py rename to src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py index fbd86822..ca0ab888 100644 --- a/src/modules/autorole/events/auto_role_on_raw_reaction_remove.py +++ b/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py @@ -6,7 +6,7 @@ from discord import RawReactionActionEvent from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -from modules.autorole.helper.reaction_handler import ReactionHandler +from modules.auto_role.helper.reaction_handler import ReactionHandler class AutoRoleOnRawReactionRemoveEvent(OnRawReactionRemoveABC): diff --git a/src/modules/autorole/helper/__init__.py b/src/modules/auto_role/helper/__init__.py similarity index 100% rename from src/modules/autorole/helper/__init__.py rename to src/modules/auto_role/helper/__init__.py diff --git a/src/modules/autorole/helper/reaction_handler.py b/src/modules/auto_role/helper/reaction_handler.py similarity index 100% rename from src/modules/autorole/helper/reaction_handler.py rename to src/modules/auto_role/helper/reaction_handler.py diff --git a/src/modules/autorole/command/auto_role_command.py b/src/modules/autorole/command/auto_role_command.py deleted file mode 100644 index 733d413f..00000000 --- a/src/modules/autorole/command/auto_role_command.py +++ /dev/null @@ -1,86 +0,0 @@ -import discord -from cpl_discord.command import DiscordCommandABC -from cpl_discord.service import DiscordBotServiceABC -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.logging.command_logger import CommandLogger -from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC -from bot_data.abc.server_repository_abc import ServerRepositoryABC - - -class AutoRoleCommand(DiscordCommandABC): - - def __init__( - self, - logger: CommandLogger, - message_service: MessageServiceABC, - bot: DiscordBotServiceABC, - client_utils: ClientUtilsServiceABC, - translate: TranslatePipe, - servers: ServerRepositoryABC, - auto_roles: AutoRoleRepositoryABC - ): - DiscordCommandABC.__init__(self) - - self._logger = logger - self._message_service = message_service - self._bot = bot - self._client_utils = client_utils - self._t = translate - self._servers = servers - self._auto_roles = auto_roles - - self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - - @commands.hybrid_group(name="auto-role") - @commands.guild_only() - async def auto_role(self, ctx: Context): - pass - - @auto_role.command(alias='auto-roles') - @commands.guild_only() - 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) - - embed = discord.Embed( - title=self._t.transform('modules.auto_role.list.title'), - description=self._t.transform('modules.auto_role.list.description'), - color=int('ef9d0d', 16) - ) - auto_roles = self._auto_roles.get_auto_roles() - if auto_roles.count() < 1: - await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.error.nothing_found')) - self._logger.trace(__name__, f'Finished command auto-role list') - return - - auto_role_id = '' - message_id = '' - for auto_role in auto_roles: - auto_role_id += f'\n{auto_role.auto_role_id}' - message_id += f'\n{auto_role.discord_message_id}' - - embed.add_field(name=self._t.transform('modules.auto_role.list.auto_role_id'), value=auto_role_id, inline=True) - embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True) - await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) - self._logger.trace(__name__, f'Finished command auto-role list') - - @auto_role.command() - @commands.guild_only() - async def add(self, ctx: Context): - self._logger.debug(__name__, f'Received command auto-role add {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return - - @auto_role.command() - @commands.guild_only() - async def remove(self, ctx: Context): - self._logger.debug(__name__, f'Received command auto-role remove {ctx}') - if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): - return From a23815998cb76c5ba9bb68c0be63c83f41ed295b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 13:23:15 +0200 Subject: [PATCH 09/20] Improved some commands #54 --- src/modules/base/base_module.py | 2 -- src/modules/base/command/help_command.py | 9 +++++++++ .../{base => moderator}/command/user_info_command.py | 0 src/modules/moderator/moderator_module.py | 2 ++ 4 files changed, 11 insertions(+), 2 deletions(-) rename src/modules/{base => moderator}/command/user_info_command.py (100%) diff --git a/src/modules/base/base_module.py b/src/modules/base/base_module.py index ef52df05..d4257cd8 100644 --- a/src/modules/base/base_module.py +++ b/src/modules/base/base_module.py @@ -11,7 +11,6 @@ from modules.base.command.afk_command import AFKCommand from modules.base.command.help_command import HelpCommand from modules.base.command.info_command import InfoCommand from modules.base.command.ping_command import PingCommand -from modules.base.command.user_info_command import UserInfoCommand from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent from modules.base.events.base_on_command_event import BaseOnCommandEvent from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent @@ -36,7 +35,6 @@ class BaseModule(ModuleABC): self._dc.add_command(HelpCommand) self._dc.add_command(InfoCommand) self._dc.add_command(PingCommand) - self._dc.add_command(UserInfoCommand) # events self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) diff --git a/src/modules/base/command/help_command.py b/src/modules/base/command/help_command.py index a903e70a..b85dacd8 100644 --- a/src/modules/base/command/help_command.py +++ b/src/modules/base/command/help_command.py @@ -1,6 +1,10 @@ +from typing import List + +import discord from cpl_core.configuration import ConfigurationABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC +from discord import app_commands from discord.ext import commands from discord.ext.commands import Context @@ -41,3 +45,8 @@ class HelpCommand(DiscordCommandABC): is_persistent = persistent_flag == '--stay' await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent) self._logger.trace(__name__, f'Finished help command') + + @help.autocomplete('persistent_flag') + async def help_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + flags = ['--stay'] + return [app_commands.Choice(name=key, value=key) for key in flags] diff --git a/src/modules/base/command/user_info_command.py b/src/modules/moderator/command/user_info_command.py similarity index 100% rename from src/modules/base/command/user_info_command.py rename to src/modules/moderator/command/user_info_command.py diff --git a/src/modules/moderator/moderator_module.py b/src/modules/moderator/moderator_module.py index e47d994d..c981fc2d 100644 --- a/src/modules/moderator/moderator_module.py +++ b/src/modules/moderator/moderator_module.py @@ -6,6 +6,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.moderator.command.purge_command import PurgeCommand +from modules.moderator.command.user_info_command import UserInfoCommand class ModeratorModule(ModuleABC): @@ -19,4 +20,5 @@ class ModeratorModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): # commands self._dc.add_command(PurgeCommand) + self._dc.add_command(UserInfoCommand) # events From 8afc414d8b3490d02cfb91e4c10b4a35733c608c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 17:23:07 +0200 Subject: [PATCH 10/20] Removed old imports #54 --- src/bot/startup_discord_extension.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/bot/startup_discord_extension.py b/src/bot/startup_discord_extension.py index e0edbfde..de665b1a 100644 --- a/src/bot/startup_discord_extension.py +++ b/src/bot/startup_discord_extension.py @@ -1,31 +1,8 @@ -from typing import Optional - from cpl_core.application import StartupExtensionABC from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC from cpl_discord import get_discord_collection -from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum - -from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings -from modules.admin.command.restart_command import RestartCommand -from modules.admin.command.shutdown_command import ShutdownCommand -from modules.base.command.afk_command import AFKCommand -from modules.base.command.help_command import HelpCommand -from modules.base.command.info_command import InfoCommand -from modules.base.command.ping_command import PingCommand -from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent -from modules.base.events.base_on_command_event import BaseOnCommandEvent -from modules.moderator.command.purge_command import PurgeCommand -from modules.base.command.user_info_command import UserInfoCommand -from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent -from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent -from modules.base.events.base_on_message_event import BaseOnMessageEvent -from modules.base.events.base_on_voice_state_update_event import BaseOnVoiceStateUpdateEvent -from modules.boot_log.boot_log_on_ready_event import BootLogOnReadyEvent -from modules.database.database_on_ready_event import DatabaseOnReadyEvent -from modules.permission.events.permission_on_member_update_event import PermissionOnMemberUpdateEvent -from modules.permission.events.permission_on_ready_event import PermissionOnReadyEvent class StartupDiscordExtension(StartupExtensionABC): From 5795028fdbdb8b6d24b7d1666b4b2c9ee0113a15 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 20:31:03 +0200 Subject: [PATCH 11/20] Added auto-role add command #54 --- src/bot/translation/de.json | 7 +++ .../auto_role/command/auto_role_group.py | 48 ++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index db98a9a7..ee37a939 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -60,6 +60,13 @@ "auto_role_id": "auto-role Id", "message_id": "Nachricht-Id" }, + "add": { + "success": "auto-role für die Nachricht {} wurde hinzugefügt :D", + "error": { + "not_found": "auto-role für die Nachricht {} nicht gefunden!", + "already_exists": "auto-role für die Nachricht {} existiert bereits!" + } + }, "rule": { "list": { "title": "auto-role Regeln:", diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 29072ae9..9f5e86c4 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -1,8 +1,11 @@ -from typing import List +from typing import List as TList, Optional import discord +from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC +from cpl_discord.container import TextChannel from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List from cpl_translation import TranslatePipe from discord import app_commands from discord.ext import commands @@ -13,6 +16,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC 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 class AutoRoleGroup(DiscordCommandABC): @@ -25,7 +29,8 @@ class AutoRoleGroup(DiscordCommandABC): client_utils: ClientUtilsServiceABC, translate: TranslatePipe, servers: ServerRepositoryABC, - auto_roles: AutoRoleRepositoryABC + auto_roles: AutoRoleRepositoryABC, + db_context: DatabaseContextABC ): DiscordCommandABC.__init__(self) @@ -36,12 +41,14 @@ class AutoRoleGroup(DiscordCommandABC): self._t = translate self._servers = servers self._auto_roles = auto_roles + self._db_context = db_context self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') @commands.hybrid_group(name="auto-role") @commands.guild_only() - async def auto_role(self, ctx: Context): pass + async def auto_role(self, ctx: Context): + pass @auto_role.command(alias='auto-roles') @commands.guild_only() @@ -72,7 +79,36 @@ class AutoRoleGroup(DiscordCommandABC): embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) self._logger.trace(__name__, f'Finished command auto-role list') - pass + + @auto_role.command() + @commands.guild_only() + async def add(self, ctx: Context, message_id: str): + self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}') + message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() + if message is None: + self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id)) + self._logger.trace(__name__, f'Finished command auto-role add') + return + + if self._auto_roles.find_auto_roles_by_message_id(int(message_id)) is not None: + self._logger.debug(__name__, f'AutoRole for message {message_id} already exists') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id)) + self._logger.trace(__name__, f'Finished command auto-role add') + return + + server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id + self._auto_roles.add_auto_role(AutoRole(server_id, int(message_id))) + self._db_context.save_changes() + self._logger.info(__name__, f'Saved AutoRole for message {message_id} at server {server_id}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.success').format(message_id)) + self._logger.trace(__name__, f'Finished command auto-role add') + + @add.autocomplete('message_id') + async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id) + messages = [message async for message in channel.history(limit=10)] + return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)] @auto_role.group() @commands.guild_only() @@ -113,6 +149,6 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.trace(__name__, f'Finished command auto-role-rule list') @list.autocomplete('auto_role') - async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) - return [app_commands.Choice(name=key, value=key) for key in auto_roles] + return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] From b75777a339d26c69966a6947ce1789dd7f5f7e40 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 21:04:02 +0200 Subject: [PATCH 12/20] Added auto-role remove command & some refactoring #54 --- src/bot/translation/de.json | 6 ++ src/bot_data/abc/auto_role_repository_abc.py | 16 ++--- src/bot_data/model/auto_role_rule.py | 12 +++- .../service/auto_role_repository_service.py | 16 +++++ .../auto_role/command/auto_role_group.py | 59 ++++++++++++++++++- .../base/events/base_on_command_event.py | 6 +- 6 files changed, 100 insertions(+), 15 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index ee37a939..c7b0ce63 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -67,6 +67,12 @@ "already_exists": "auto-role für die Nachricht {} existiert bereits!" } }, + "remove": { + "success": "auto-role {} wurde entfernt :D", + "error": { + "not_found": "auto-role {} nicht gefunden!" + } + }, "rule": { "list": { "title": "auto-role Regeln:", diff --git a/src/bot_data/abc/auto_role_repository_abc.py b/src/bot_data/abc/auto_role_repository_abc.py index 7b9c0ed1..14422fc5 100644 --- a/src/bot_data/abc/auto_role_repository_abc.py +++ b/src/bot_data/abc/auto_role_repository_abc.py @@ -5,7 +5,6 @@ from cpl_query.extension import List from bot_data.model.auto_role import AutoRole from bot_data.model.auto_role_rule import AutoRoleRule -from bot_data.model.server import Server class AutoRoleRepositoryABC(ABC): @@ -19,6 +18,9 @@ class AutoRoleRepositoryABC(ABC): @abstractmethod def get_auto_role_by_id(self, id: int) -> AutoRole: pass + @abstractmethod + def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: pass + @abstractmethod def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass @@ -32,13 +34,13 @@ class AutoRoleRepositoryABC(ABC): def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass @abstractmethod - def add_auto_role(self, server: AutoRole): pass + def add_auto_role(self, auto_role: AutoRole): pass @abstractmethod - def update_auto_role(self, server: AutoRole): pass + def update_auto_role(self, auto_role: AutoRole): pass @abstractmethod - def delete_auto_role(self, server: AutoRole): pass + def delete_auto_role(self, auto_role: AutoRole): pass @abstractmethod def get_auto_role_rules(self) -> List[AutoRoleRule]: pass @@ -50,10 +52,10 @@ class AutoRoleRepositoryABC(ABC): def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass @abstractmethod - def add_auto_role_rule(self, server: AutoRoleRule): pass + def add_auto_role_rule(self, auto_role: AutoRoleRule): pass @abstractmethod - def update_auto_role_rule(self, server: AutoRoleRule): pass + def update_auto_role_rule(self, auto_role: AutoRoleRule): pass @abstractmethod - def delete_auto_role_rule(self, server: AutoRoleRule): pass + def delete_auto_role_rule(self, auto_role: AutoRoleRule): pass diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index ca9fce32..5fe3114e 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -17,11 +17,19 @@ class AutoRoleRule(TableABC): self._modified_at = modified_at if modified_at is not None else self._modified_at @property - def emoji_name(self): + def auto_role_rule_id(self) -> int: + return self._auto_role_rule_id + + @property + def auto_role_id(self) -> int: + return self._auto_role_id + + @property + def emoji_name(self) -> str: return self._discord_emoji_name @property - def role_id(self): + def role_id(self) -> int: return self._discord_role_id @staticmethod diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 7b02bc72..0810f858 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -43,6 +43,22 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): id=result[0] ) + def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: + self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}') + result = self._context.select(AutoRole.get_select_by_id_string(id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + ) + def get_auto_roles_by_server_id(self, id: int) -> AutoRole: self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0] diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 9f5e86c4..549d9030 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -17,6 +17,7 @@ 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 modules.permission.abc.permission_service_abc import PermissionServiceABC class AutoRoleGroup(DiscordCommandABC): @@ -30,7 +31,8 @@ class AutoRoleGroup(DiscordCommandABC): translate: TranslatePipe, servers: ServerRepositoryABC, auto_roles: AutoRoleRepositoryABC, - db_context: DatabaseContextABC + db_context: DatabaseContextABC, + permission_service: PermissionServiceABC, ): DiscordCommandABC.__init__(self) @@ -42,6 +44,7 @@ class AutoRoleGroup(DiscordCommandABC): self._servers = servers self._auto_roles = auto_roles self._db_context = db_context + self._permissions = permission_service self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') @@ -56,7 +59,11 @@ class AutoRoleGroup(DiscordCommandABC): 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 purge command') + return embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), @@ -84,6 +91,14 @@ class AutoRoleGroup(DiscordCommandABC): @commands.guild_only() async def add(self, ctx: Context, 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 + + 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 + message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() if message is None: self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}') @@ -110,6 +125,40 @@ class AutoRoleGroup(DiscordCommandABC): messages = [message async for message in channel.history(limit=10)] return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)] + @auto_role.command() + @commands.guild_only() + 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 + + 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 + + auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role) + if auto_role_from_db is None: + self._logger.debug(__name__, f'AutoRole {auto_role} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role)) + self._logger.trace(__name__, f'Finished command auto-role remove') + return + + for rule in self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_from_db.auto_role_id): + self._auto_roles.delete_auto_role_rule(rule) + self._logger.info(__name__, f'Removed AutoRole rule {rule.role_id}') + + self._auto_roles.delete_auto_role(auto_role_from_db) + self._db_context.save_changes() + self._logger.info(__name__, f'Removed AutoRole {auto_role}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.success').format(auto_role)) + self._logger.trace(__name__, f'Finished command auto-role remove') + + @remove.autocomplete('auto_role') + async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) + return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] + @auto_role.group() @commands.guild_only() async def rule(self, ctx: Context): @@ -121,7 +170,11 @@ class AutoRoleGroup(DiscordCommandABC): 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 purge command') + return embed = discord.Embed( title=self._t.transform('modules.auto_role.list.title'), diff --git a/src/modules/base/events/base_on_command_event.py b/src/modules/base/events/base_on_command_event.py index e1c46a20..348e9e4c 100644 --- a/src/modules/base/events/base_on_command_event.py +++ b/src/modules/base/events/base_on_command_event.py @@ -48,9 +48,9 @@ class BaseOnCommandEvent(OnCommandABC): self._clients = clients self._servers = servers - def _append_received_message_count(self, g_id: int): + def _append_received_command_count(self, g_id: int): try: - self._clients.append_received_message_count(self._bot.user.id, g_id, 1) + self._clients.append_received_command_count(self._bot.user.id, g_id, 1) self._db.save_changes() except Exception as e: self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e) @@ -87,4 +87,4 @@ class BaseOnCommandEvent(OnCommandABC): self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}') if ctx is None or ctx.guild is None: return - self._append_received_message_count(ctx.guild.id) + self._append_received_command_count(ctx.guild.id) From eb3bdac162239a55e2411967a0ed44e74333338a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 22:04:25 +0200 Subject: [PATCH 13/20] Added auto-role rule add command #54 --- src/bot/translation/de.json | 21 +++++- src/bot_data/model/auto_role_rule.py | 6 +- .../service/auto_role_repository_service.py | 8 +-- .../auto_role/command/auto_role_group.py | 72 +++++++++++++++++-- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index c7b0ce63..6fa2fc2d 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -73,6 +73,9 @@ "not_found": "auto-role {} nicht gefunden!" } }, + "error": { + "nothing_found": "Keine auto-role Einträge gefunden." + }, "rule": { "list": { "title": "auto-role Regeln:", @@ -81,12 +84,24 @@ "emoji": "Emoji", "role": "Rolle" }, + "add": { + "success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D", + "error": { + "not_found": "Regel für auto-role {} nicht gefunden!", + "emoji_not_found": "Eomji {} für auto-role Regel {} nicht gefunden!", + "rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!", + "already_exists": "Regel für auto-role {} existiert bereits!" + } + }, + "remove": { + "success": "Regel für auto-role {} wurde entfernt :D", + "error": { + "not_found": "Regel für auto-role {} nicht gefunden!" + } + }, "error": { "id_not_found": "Kein auto-role Eintrag mit der Id gefunden!" } - }, - "error": { - "nothing_found": "Keine auto-role Einträge gefunden." } }, "moderator": { diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index 5fe3114e..45f43c1b 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -59,7 +59,7 @@ class AutoRoleRule(TableABC): `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._auto_role_id}, - {self._discord_emoji_name}, + '{self._discord_emoji_name}', {self._discord_role_id}, '{self._created_at}', '{self._modified_at}' @@ -74,12 +74,12 @@ class AutoRoleRule(TableABC): SET `DiscordEmojiName` = {self._discord_emoji_name}, SET `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' - WHERE `AutoRoleRuleId` = {self._auto_role_id}; + WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; """) @property def delete_string(self) -> str: return str(f""" DELETE FROM `AutoRoleRules` - WHERE `AutoRoleRuleId` = {self._auto_role_id}; + WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; """) diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 0810f858..9801a17d 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -170,12 +170,12 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): return auto_role_rules def add_auto_role_rule(self, auto_role_rule: AutoRoleRule): - self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') - self._context.cursor.execute(auto_role_rule.delete_string) + self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.insert_string}') + self._context.cursor.execute(auto_role_rule.insert_string) def update_auto_role_rule(self, auto_role_rule: AutoRoleRule): - self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') - self._context.cursor.execute(auto_role_rule.delete_string) + self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.udpate_string}') + self._context.cursor.execute(auto_role_rule.udpate_string) def delete_auto_role_rule(self, auto_role_rule: AutoRoleRule): self._logger.trace(__name__, f'Send SQL command: {auto_role_rule.delete_string}') diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 549d9030..18bc5242 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -17,6 +17,7 @@ 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 modules.permission.abc.permission_service_abc import PermissionServiceABC @@ -62,7 +63,7 @@ class AutoRoleGroup(DiscordCommandABC): 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') + self._logger.trace(__name__, f'Finished command auto-role list') return embed = discord.Embed( @@ -96,7 +97,7 @@ class AutoRoleGroup(DiscordCommandABC): 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') + self._logger.trace(__name__, f'Finished command auto-role add') return message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() @@ -134,7 +135,7 @@ class AutoRoleGroup(DiscordCommandABC): 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') + 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) @@ -167,13 +168,13 @@ class AutoRoleGroup(DiscordCommandABC): @rule.command(alias='rules') @commands.guild_only() async def list(self, ctx: Context, auto_role: int, wait: int = None): - self._logger.debug(__name__, f'Received command auto-role-rule list {ctx}') + 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 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') + self._logger.trace(__name__, f'Finished command auto-role rule list') return embed = discord.Embed( @@ -184,7 +185,7 @@ class AutoRoleGroup(DiscordCommandABC): rules = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role) if rules.count() < 1: await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.error.id_not_found')) - self._logger.trace(__name__, f'Finished command auto-role-rule list') + self._logger.trace(__name__, f'Finished command auto-role rule list') return auto_role_rule_id = '' @@ -199,9 +200,66 @@ class AutoRoleGroup(DiscordCommandABC): embed.add_field(name=self._t.transform('modules.auto_role.rule.list.emoji'), value=emoji, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.rule.list.role'), value=role, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) - self._logger.trace(__name__, f'Finished command auto-role-rule list') + self._logger.trace(__name__, f'Finished command auto-role rule list') @list.autocomplete('auto_role') async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] + + @rule.command() + @commands.guild_only() + 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 + + 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: + self._logger.debug(__name__, f'auto-role rule add emoji {emoji_name} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.error.emoji_not_found').format(emoji_name, auto_role)) + self._logger.trace(__name__, f'Finished command auto-role add') + return + + role = ctx.guild.get_role(int(role_id)) + if role is None: + self._logger.debug(__name__, f'auto-role rule add role {role_id} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.error.role_not_found').format(role_id, auto_role)) + self._logger.trace(__name__, f'Finished command auto-role add') + return + + auto_role_from_db = self._auto_roles.get_auto_role_by_id(auto_role) + if auto_role_from_db is None: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role)) + self._logger.trace(__name__, f'Finished command auto-role rule add') + return + + if self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role).where(lambda r: r.emoji_name == emoji.name and int(role_id) == role.id).count() > 0: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(auto_role)) + self._logger.trace(__name__, f'Finished command auto-role rule add') + return + + self._auto_roles.add_auto_role_rule(AutoRoleRule(auto_role, emoji_name, int(role_id))) + self._db_context.save_changes() + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.add.success').format(emoji, role.name, auto_role)) + self._logger.trace(__name__, f'Finished command auto-role rule add') + + @add.autocomplete('auto_role') + async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id) + return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles] + + @add.autocomplete('emoji_name') + async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + emojis = interaction.guild.emojis + return [app_commands.Choice(name=emoji.name, value=emoji.name) for emoji in emojis] + + @add.autocomplete('role_id') + async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + roles = interaction.guild.roles + return [app_commands.Choice(name=role.name, value=str(role.id)) for role in roles] From 17befd9f050421035566735bfafd5f18347bdad9 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 22:24:20 +0200 Subject: [PATCH 14/20] Added auto-role rule remove command #54 --- src/bot/translation/de.json | 2 +- .../auto_role/command/auto_role_group.py | 42 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 6fa2fc2d..927c6b57 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -80,7 +80,7 @@ "list": { "title": "auto-role Regeln:", "description": "Von auto-role angewendete Regeln:", - "auto_role_id": "auto-role Id", + "auto_role_rule_id": "auto-role Regel Id", "emoji": "Emoji", "role": "Rolle" }, diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 18bc5242..c26210db 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -108,7 +108,7 @@ class AutoRoleGroup(DiscordCommandABC): return if self._auto_roles.find_auto_roles_by_message_id(int(message_id)) is not None: - self._logger.debug(__name__, f'AutoRole for message {message_id} already exists') + self._logger.debug(__name__, f'auto-role for message {message_id} already exists') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id)) self._logger.trace(__name__, f'Finished command auto-role add') return @@ -116,7 +116,7 @@ class AutoRoleGroup(DiscordCommandABC): server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id self._auto_roles.add_auto_role(AutoRole(server_id, int(message_id))) self._db_context.save_changes() - self._logger.info(__name__, f'Saved AutoRole for message {message_id} at server {server_id}') + self._logger.info(__name__, f'Saved auto-role for message {message_id} at server {server_id}') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.success').format(message_id)) self._logger.trace(__name__, f'Finished command auto-role add') @@ -140,18 +140,18 @@ class AutoRoleGroup(DiscordCommandABC): auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role) if auto_role_from_db is None: - self._logger.debug(__name__, f'AutoRole {auto_role} not found') + self._logger.debug(__name__, f'auto-role {auto_role} not found') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role)) self._logger.trace(__name__, f'Finished command auto-role remove') return for rule in self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_from_db.auto_role_id): self._auto_roles.delete_auto_role_rule(rule) - self._logger.info(__name__, f'Removed AutoRole rule {rule.role_id}') + self._logger.info(__name__, f'Removed auto-role rule {rule.role_id}') self._auto_roles.delete_auto_role(auto_role_from_db) self._db_context.save_changes() - self._logger.info(__name__, f'Removed AutoRole {auto_role}') + self._logger.info(__name__, f'Removed auto-role {auto_role}') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.success').format(auto_role)) self._logger.trace(__name__, f'Finished command auto-role remove') @@ -196,7 +196,7 @@ class AutoRoleGroup(DiscordCommandABC): emoji += f'\n{rule.emoji_name}' role += f'\n{ctx.guild.get_role(rule.role_id)}' - embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_id'), value=auto_role_rule_id, inline=True) + embed.add_field(name=self._t.transform('modules.auto_role.rule.list.auto_role_rule_id'), value=auto_role_rule_id, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.rule.list.emoji'), value=emoji, inline=True) embed.add_field(name=self._t.transform('modules.auto_role.rule.list.role'), value=role, inline=True) await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) @@ -263,3 +263,33 @@ class AutoRoleGroup(DiscordCommandABC): async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: roles = interaction.guild.roles return [app_commands.Choice(name=role.name, value=str(role.id)) for role in roles] + + @rule.command() + @commands.guild_only() + 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 + + 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: + self._logger.debug(__name__, f'auto-role rule {auto_role_rule} not found') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role_rule)) + self._logger.trace(__name__, f'Finished command auto-role rule remove') + return + + self._auto_roles.delete_auto_role_rule(auto_role_from_db) + self._db_context.save_changes() + self._logger.info(__name__, f'Removed auto-role rule {auto_role_rule}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.rule.remove.success').format(auto_role_rule)) + self._logger.trace(__name__, f'Finished command auto-role remove') + + @remove.autocomplete('auto_role_rule') + async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: + rules = self._auto_roles.get_auto_role_rules() + return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in rules] From c7c60edbd7a29674e9d0a166c81228ffcf01ae5a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Oct 2022 22:30:59 +0200 Subject: [PATCH 15/20] Build version 0.2.2 #54 --- src/bot/__init__.py | 4 ++-- src/bot/bot.json | 12 ++++++------ src/bot_core/__init__.py | 4 ++-- src/bot_core/abc/__init__.py | 4 ++-- src/bot_core/configuration/__init__.py | 4 ++-- src/bot_core/core_extension/__init__.py | 4 ++-- src/bot_core/events/__init__.py | 4 ++-- src/bot_core/helper/__init__.py | 4 ++-- src/bot_core/logging/__init__.py | 4 ++-- src/bot_core/pipes/__init__.py | 4 ++-- src/bot_core/service/__init__.py | 4 ++-- src/bot_data/__init__.py | 4 ++-- src/bot_data/abc/__init__.py | 4 ++-- src/bot_data/migration/__init__.py | 4 ++-- src/bot_data/model/__init__.py | 4 ++-- src/bot_data/service/__init__.py | 4 ++-- src/modules/admin/__init__.py | 4 ++-- src/modules/admin/command/__init__.py | 4 ++-- src/modules/base/__init__.py | 4 ++-- src/modules/base/abc/__init__.py | 4 ++-- src/modules/base/command/__init__.py | 4 ++-- src/modules/base/configuration/__init__.py | 4 ++-- src/modules/base/events/__init__.py | 4 ++-- src/modules/base/service/__init__.py | 4 ++-- src/modules/boot_log/__init__.py | 4 ++-- src/modules/boot_log/configuration/__init__.py | 4 ++-- src/modules/database/__init__.py | 4 ++-- src/modules/moderator/__init__.py | 4 ++-- src/modules/moderator/command/__init__.py | 4 ++-- src/modules/permission/__init__.py | 4 ++-- src/modules/permission/abc/__init__.py | 4 ++-- src/modules/permission/configuration/__init__.py | 4 ++-- src/modules/permission/events/__init__.py | 4 ++-- src/modules/permission/service/__init__.py | 4 ++-- 34 files changed, 72 insertions(+), 72 deletions(-) diff --git a/src/bot/__init__.py b/src/bot/__init__.py index db32fd91..a3f4df3d 100644 --- a/src/bot/__init__.py +++ b/src/bot/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot/bot.json b/src/bot/bot.json index 9554e15c..206ef9d4 100644 --- a/src/bot/bot.json +++ b/src/bot/bot.json @@ -4,7 +4,7 @@ "Version": { "Major": "0", "Minor": "2", - "Micro": "1" + "Micro": "2" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", @@ -16,13 +16,13 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core>=2022.10.0", - "cpl-translation>=2022.10.0", - "cpl-query>=2022.10.0", - "cpl-discord>=2022.10.0" + "cpl-core==2022.10.0.post3", + "cpl-translation==2022.10.0", + "cpl-query==2022.10.0", + "cpl-discord==2022.10.0.post5" ], "DevDependencies": [ - "cpl-cli>=2022.10.0" + "cpl-cli==2022.10.0" ], "PythonVersion": ">=3.10.4", "PythonPath": { diff --git a/src/bot_core/__init__.py b/src/bot_core/__init__.py index e323ced5..6497b6fa 100644 --- a/src/bot_core/__init__.py +++ b/src/bot_core/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/abc/__init__.py b/src/bot_core/abc/__init__.py index d95795f2..8879e593 100644 --- a/src/bot_core/abc/__init__.py +++ b/src/bot_core/abc/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.abc' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/configuration/__init__.py b/src/bot_core/configuration/__init__.py index 1aa8c319..ef1850dd 100644 --- a/src/bot_core/configuration/__init__.py +++ b/src/bot_core/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/core_extension/__init__.py b/src/bot_core/core_extension/__init__.py index fe0762bd..3a970237 100644 --- a/src/bot_core/core_extension/__init__.py +++ b/src/bot_core/core_extension/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.core_extension' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/events/__init__.py b/src/bot_core/events/__init__.py index 7cf3da4a..1971b90e 100644 --- a/src/bot_core/events/__init__.py +++ b/src/bot_core/events/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.events' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/helper/__init__.py b/src/bot_core/helper/__init__.py index 85871fad..441021cb 100644 --- a/src/bot_core/helper/__init__.py +++ b/src/bot_core/helper/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.helper' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/logging/__init__.py b/src/bot_core/logging/__init__.py index 55630861..1e602041 100644 --- a/src/bot_core/logging/__init__.py +++ b/src/bot_core/logging/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.logging' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/pipes/__init__.py b/src/bot_core/pipes/__init__.py index 77bb114e..95982d42 100644 --- a/src/bot_core/pipes/__init__.py +++ b/src/bot_core/pipes/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.pipes' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_core/service/__init__.py b/src/bot_core/service/__init__.py index 280d2658..ca8b5388 100644 --- a/src/bot_core/service/__init__.py +++ b/src/bot_core/service/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_core.service' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_data/__init__.py b/src/bot_data/__init__.py index 354d9d03..a372c3aa 100644 --- a/src/bot_data/__init__.py +++ b/src/bot_data/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_data' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_data/abc/__init__.py b/src/bot_data/abc/__init__.py index d475ce09..c358dc45 100644 --- a/src/bot_data/abc/__init__.py +++ b/src/bot_data/abc/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_data.abc' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_data/migration/__init__.py b/src/bot_data/migration/__init__.py index cb7f0409..2e2443a7 100644 --- a/src/bot_data/migration/__init__.py +++ b/src/bot_data/migration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_data.migration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_data/model/__init__.py b/src/bot_data/model/__init__.py index 45bf4225..5e9029e8 100644 --- a/src/bot_data/model/__init__.py +++ b/src/bot_data/model/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_data.model' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/bot_data/service/__init__.py b/src/bot_data/service/__init__.py index 0224b585..6ac138f2 100644 --- a/src/bot_data/service/__init__.py +++ b/src/bot_data/service/__init__.py @@ -15,7 +15,7 @@ __title__ = 'bot_data.service' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/admin/__init__.py b/src/modules/admin/__init__.py index fc1dc7f9..281d4e5c 100644 --- a/src/modules/admin/__init__.py +++ b/src/modules/admin/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.admin' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/admin/command/__init__.py b/src/modules/admin/command/__init__.py index 81fe7473..2eb5b36f 100644 --- a/src/modules/admin/command/__init__.py +++ b/src/modules/admin/command/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.admin.command' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/__init__.py b/src/modules/base/__init__.py index 9c96d601..127341b8 100644 --- a/src/modules/base/__init__.py +++ b/src/modules/base/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/abc/__init__.py b/src/modules/base/abc/__init__.py index f2360753..76e2a071 100644 --- a/src/modules/base/abc/__init__.py +++ b/src/modules/base/abc/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base.abc' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/command/__init__.py b/src/modules/base/command/__init__.py index 12373318..6e111178 100644 --- a/src/modules/base/command/__init__.py +++ b/src/modules/base/command/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base.command' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/configuration/__init__.py b/src/modules/base/configuration/__init__.py index f45bccda..aab13767 100644 --- a/src/modules/base/configuration/__init__.py +++ b/src/modules/base/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/events/__init__.py b/src/modules/base/events/__init__.py index 260ba442..a0ba5b1a 100644 --- a/src/modules/base/events/__init__.py +++ b/src/modules/base/events/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base.events' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/base/service/__init__.py b/src/modules/base/service/__init__.py index c1e5ee4b..c3aa8958 100644 --- a/src/modules/base/service/__init__.py +++ b/src/modules/base/service/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.base.service' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/boot_log/__init__.py b/src/modules/boot_log/__init__.py index 51d3bcd6..822e029f 100644 --- a/src/modules/boot_log/__init__.py +++ b/src/modules/boot_log/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.boot_log' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/boot_log/configuration/__init__.py b/src/modules/boot_log/configuration/__init__.py index 272c31fe..bd45bb7a 100644 --- a/src/modules/boot_log/configuration/__init__.py +++ b/src/modules/boot_log/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.boot_log.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/database/__init__.py b/src/modules/database/__init__.py index 60ccad62..77c51380 100644 --- a/src/modules/database/__init__.py +++ b/src/modules/database/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.database' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/moderator/__init__.py b/src/modules/moderator/__init__.py index e87f4143..2cddb526 100644 --- a/src/modules/moderator/__init__.py +++ b/src/modules/moderator/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.moderator' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/moderator/command/__init__.py b/src/modules/moderator/command/__init__.py index eab82f4c..22b49279 100644 --- a/src/modules/moderator/command/__init__.py +++ b/src/modules/moderator/command/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.moderator.command' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/permission/__init__.py b/src/modules/permission/__init__.py index cf6a319d..be1d38f4 100644 --- a/src/modules/permission/__init__.py +++ b/src/modules/permission/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.permission' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/permission/abc/__init__.py b/src/modules/permission/abc/__init__.py index 60564ef0..8cb90065 100644 --- a/src/modules/permission/abc/__init__.py +++ b/src/modules/permission/abc/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.permission.abc' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/permission/configuration/__init__.py b/src/modules/permission/configuration/__init__.py index 38130ee2..725da88a 100644 --- a/src/modules/permission/configuration/__init__.py +++ b/src/modules/permission/configuration/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.permission.configuration' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/permission/events/__init__.py b/src/modules/permission/events/__init__.py index 787bc5d4..861d4ec6 100644 --- a/src/modules/permission/events/__init__.py +++ b/src/modules/permission/events/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.permission.events' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/permission/service/__init__.py b/src/modules/permission/service/__init__.py index d01084e6..138a6b6b 100644 --- a/src/modules/permission/service/__init__.py +++ b/src/modules/permission/service/__init__.py @@ -15,7 +15,7 @@ __title__ = 'modules.permission.service' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') From 8966bded81335162736cc53745f472f392b892fd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Oct 2022 16:43:24 +0200 Subject: [PATCH 16/20] Added channel param to auto-role add & fixed on reaction add event stuff #54 --- .../config/appsettings.edrafts-pc-ubuntu.json | 6 +++--- src/bot/translation/de.json | 4 ++-- .../auto_role/command/auto_role_group.py | 19 ++++++++++++++----- .../auto_role/helper/reaction_handler.py | 14 +++++++++++--- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/bot/config/appsettings.edrafts-pc-ubuntu.json b/src/bot/config/appsettings.edrafts-pc-ubuntu.json index a05aac5f..6de95194 100644 --- a/src/bot/config/appsettings.edrafts-pc-ubuntu.json +++ b/src/bot/config/appsettings.edrafts-pc-ubuntu.json @@ -9,19 +9,19 @@ "Command": { "Path": "logs/", "Filename": "commands.log", - "ConsoleLogLevel": "DEBUG", + "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" }, "Database": { "Path": "logs/", "Filename": "database.log", - "ConsoleLogLevel": "DEBUG", + "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" }, "Message": { "Path": "logs/", "Filename": "message.log", - "ConsoleLogLevel": "DEBUG", + "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" } }, diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 927c6b57..9e228815 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -63,7 +63,7 @@ "add": { "success": "auto-role für die Nachricht {} wurde hinzugefügt :D", "error": { - "not_found": "auto-role für die Nachricht {} nicht gefunden!", + "not_found": "Nachricht {} nicht in {} nicht gefunden!", "already_exists": "auto-role für die Nachricht {} existiert bereits!" } }, @@ -88,7 +88,7 @@ "success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D", "error": { "not_found": "Regel für auto-role {} nicht gefunden!", - "emoji_not_found": "Eomji {} für auto-role Regel {} nicht gefunden!", + "emoji_not_found": "Emoji {} für auto-role Regel {} nicht gefunden!", "rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!", "already_exists": "Regel für auto-role {} existiert bereits!" } diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index c26210db..3ef985b0 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -90,7 +90,7 @@ class AutoRoleGroup(DiscordCommandABC): @auto_role.command() @commands.guild_only() - async def add(self, ctx: Context, message_id: str): + 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 @@ -100,10 +100,10 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.trace(__name__, f'Finished command auto-role add') return - message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() + 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: - self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}') - await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id)) + self._logger.debug(__name__, f'Message with id {message_id} not found in {channel.name}') + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id, channel.name)) self._logger.trace(__name__, f'Finished command auto-role add') return @@ -123,6 +123,14 @@ class AutoRoleGroup(DiscordCommandABC): @add.autocomplete('message_id') async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id) + try: + channel_from_data = interaction.data['options'][0]['options'][0]['value'] + found_channel = discord.utils.get(interaction.guild.text_channels, id=int(channel_from_data)) + if found_channel is not None: + channel = found_channel + finally: + pass + messages = [message async for message in channel.history(limit=10)] return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)] @@ -292,4 +300,5 @@ class AutoRoleGroup(DiscordCommandABC): @remove.autocomplete('auto_role_rule') async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: rules = self._auto_roles.get_auto_role_rules() - return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in rules] + return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in + rules] diff --git a/src/modules/auto_role/helper/reaction_handler.py b/src/modules/auto_role/helper/reaction_handler.py index 383044cb..4be20493 100644 --- a/src/modules/auto_role/helper/reaction_handler.py +++ b/src/modules/auto_role/helper/reaction_handler.py @@ -28,20 +28,27 @@ class ReactionHandler: self._roles = self._auto_roles.get_auto_roles() async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: + self._logger.trace(__name__, f'Handle reaction {payload} {r_type}') if payload.message_id not in self._message_ids: + self._logger.debug(__name__, f'Message not in auto-roles - skipping') return guild = self._bot.get_guild(payload.guild_id) user = await guild.fetch_member(payload.user_id) if user.bot: + self._logger.debug(__name__, f'User is bot - skipping') return emoji = payload.emoji.name - auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id).first_or_default() + auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id == payload.message_id).single_or_default() if auto_role is None: + self._logger.debug(__name__, f'auto-role for message not found - skipping') return rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id) + if rules.count() == 0: + self._logger.debug(__name__, f'auto-role rules not found - skipping') + return for rule in rules: if emoji != rule.emoji_name: @@ -51,8 +58,9 @@ class ReactionHandler: role = guild.get_role(rule.role_id) self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') await user.add_roles(role) - - if r_type == 'remove': + elif r_type == 'remove': role = guild.get_role(rule.role_id) self._logger.debug(__name__, f'Remove role {role.name} to {user.name}') await user.remove_roles(role) + else: + self._logger.warn(__name__, f'Invalid reaction type {r_type}') From 36a8a5be3ced2ed1827d987bf4a1ed95815106dd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Oct 2022 16:52:10 +0200 Subject: [PATCH 17/20] Added project references #54 --- src/bot/bot.json | 8 ++++--- src/modules/auto_role/__init__.py | 6 +++--- src/modules/auto_role/command/__init__.py | 26 +++++++++++++++++++++++ src/modules/auto_role/events/__init__.py | 26 +++++++++++++++++++++++ src/modules/auto_role/helper/__init__.py | 26 +++++++++++++++++++++++ 5 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/bot/bot.json b/src/bot/bot.json index 206ef9d4..07958cbe 100644 --- a/src/bot/bot.json +++ b/src/bot/bot.json @@ -48,11 +48,13 @@ "../bot_core/bot-core.json", "../bot_data/bot-data.json", "../modules/base/base.json", + "../modules/admin/admin.json", + "../modules/auto_role/auto-role.json", + "../modules/base/base.json", "../modules/boot_log/boot-log.json", "../modules/database/database.json", - "../modules/permission/permission.json", - "../modules/admin/admin.json", - "../modules/moderator/moderator.json" + "../modules/moderator/moderator.json", + "../modules/permission/permission.json" ] } } \ No newline at end of file diff --git a/src/modules/auto_role/__init__.py b/src/modules/auto_role/__init__.py index fc1dc7f9..86936814 100644 --- a/src/modules/auto_role/__init__.py +++ b/src/modules/auto_role/__init__.py @@ -11,11 +11,11 @@ Discord bot for the Keksdose discord Server """ -__title__ = 'modules.admin' +__title__ = 'modules.auto_role' __author__ = 'Sven Heidemann' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2022 sh-edraft.de' -__version__ = '0.2.1' +__version__ = '0.2.2' from collections import namedtuple @@ -23,4 +23,4 @@ from collections import namedtuple # imports: VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='0', minor='2', micro='1') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/auto_role/command/__init__.py b/src/modules/auto_role/command/__init__.py index e69de29b..f0f2c9bc 100644 --- a/src/modules/auto_role/command/__init__.py +++ b/src/modules/auto_role/command/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.auto_role.command' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.2' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/auto_role/events/__init__.py b/src/modules/auto_role/events/__init__.py index e69de29b..2ab0e8e9 100644 --- a/src/modules/auto_role/events/__init__.py +++ b/src/modules/auto_role/events/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.auto_role.events' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.2' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='2') diff --git a/src/modules/auto_role/helper/__init__.py b/src/modules/auto_role/helper/__init__.py index e69de29b..01dd5eb6 100644 --- a/src/modules/auto_role/helper/__init__.py +++ b/src/modules/auto_role/helper/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.auto_role.helper' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.2.2' + +from collections import namedtuple + + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='2', micro='2') From 869439d3b8e88683c067e4184b02fdd81ed776b8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Oct 2022 17:38:21 +0200 Subject: [PATCH 18/20] Improved translation #54 --- src/bot/translation/de.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 9e228815..910de717 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -63,7 +63,7 @@ "add": { "success": "auto-role für die Nachricht {} wurde hinzugefügt :D", "error": { - "not_found": "Nachricht {} nicht in {} nicht gefunden!", + "not_found": "Nachricht {} in {} nicht gefunden!", "already_exists": "auto-role für die Nachricht {} existiert bereits!" } }, From 2df4f9080265e95d35283a8d8746cbacc675d130 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Oct 2022 18:09:27 +0200 Subject: [PATCH 19/20] Removed redundant set from sql queries #54 --- src/bot_data/model/auto_role.py | 2 +- src/bot_data/model/auto_role_rule.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bot_data/model/auto_role.py b/src/bot_data/model/auto_role.py index 26d5dba2..dbf73529 100644 --- a/src/bot_data/model/auto_role.py +++ b/src/bot_data/model/auto_role.py @@ -72,7 +72,7 @@ class AutoRole(TableABC): return str(f""" UPDATE `AutoRoles` SET `ServerId` = {self._server_id}, - SET `DiscordMessageId` = {self._discord_message_id}, + `DiscordMessageId` = {self._discord_message_id}, `LastModifiedAt` = '{self._modified_at}' WHERE `AutoRoleId` = {self._auto_role_id}; """) diff --git a/src/bot_data/model/auto_role_rule.py b/src/bot_data/model/auto_role_rule.py index 45f43c1b..38f40b3c 100644 --- a/src/bot_data/model/auto_role_rule.py +++ b/src/bot_data/model/auto_role_rule.py @@ -71,8 +71,8 @@ class AutoRoleRule(TableABC): return str(f""" UPDATE `AutoRoleRules` SET `AutoRoleId` = {self._auto_role_id}, - SET `DiscordEmojiName` = {self._discord_emoji_name}, - SET `DiscordRoleId` = {self._discord_role_id}, + `DiscordEmojiName` = {self._discord_emoji_name}, + `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' WHERE `AutoRoleRuleId` = {self._auto_role_rule_id}; """) From f2e6be530b69ee7b4a4078e9c447ad8fa4b69484 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Oct 2022 19:04:58 +0200 Subject: [PATCH 20/20] Renamed some functions & fixed list return context #54 --- .../config/appsettings.edrafts-pc-ubuntu.json | 6 +-- src/bot_data/abc/auto_role_repository_abc.py | 7 +--- .../service/auto_role_repository_service.py | 40 +++++++------------ .../auto_role/command/auto_role_group.py | 2 +- .../auto_role_on_raw_reaction_remove.py | 1 - 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/bot/config/appsettings.edrafts-pc-ubuntu.json b/src/bot/config/appsettings.edrafts-pc-ubuntu.json index 6de95194..a05aac5f 100644 --- a/src/bot/config/appsettings.edrafts-pc-ubuntu.json +++ b/src/bot/config/appsettings.edrafts-pc-ubuntu.json @@ -9,19 +9,19 @@ "Command": { "Path": "logs/", "Filename": "commands.log", - "ConsoleLogLevel": "TRACE", + "ConsoleLogLevel": "DEBUG", "FileLogLevel": "TRACE" }, "Database": { "Path": "logs/", "Filename": "database.log", - "ConsoleLogLevel": "TRACE", + "ConsoleLogLevel": "DEBUG", "FileLogLevel": "TRACE" }, "Message": { "Path": "logs/", "Filename": "message.log", - "ConsoleLogLevel": "TRACE", + "ConsoleLogLevel": "DEBUG", "FileLogLevel": "TRACE" } }, diff --git a/src/bot_data/abc/auto_role_repository_abc.py b/src/bot_data/abc/auto_role_repository_abc.py index 14422fc5..899cd486 100644 --- a/src/bot_data/abc/auto_role_repository_abc.py +++ b/src/bot_data/abc/auto_role_repository_abc.py @@ -25,13 +25,10 @@ class AutoRoleRepositoryABC(ABC): def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass @abstractmethod - def find_auto_roles_by_server_id(self, id: int) -> Optional[AutoRole]: pass + def get_auto_role_by_message_id(self, id: int) -> AutoRole: pass @abstractmethod - def get_auto_roles_by_message_id(self, id: int) -> AutoRole: pass - - @abstractmethod - def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass + def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]: pass @abstractmethod def add_auto_role(self, auto_role: AutoRole): pass diff --git a/src/bot_data/service/auto_role_repository_service.py b/src/bot_data/service/auto_role_repository_service.py index 9801a17d..291f0a3f 100644 --- a/src/bot_data/service/auto_role_repository_service.py +++ b/src/bot_data/service/auto_role_repository_service.py @@ -59,34 +59,22 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): id=result[0] ) - def get_auto_roles_by_server_id(self, id: int) -> AutoRole: + def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]: self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') - result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0] - return AutoRole( - result[1], - result[2], - result[3], - result[4], - id=result[0] - ) + auto_roles = List(AutoRole) + results = self._context.select(AutoRole.get_select_by_server_id_string(id)) + for result in results: + auto_roles.append(AutoRole( + result[1], + result[2], + result[3], + result[4], + id=result[0] + )) - def find_auto_roles_by_server_id(self, id: int) -> Optional[AutoRole]: - self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}') - result = self._context.select(AutoRole.get_select_by_server_id_string(id)) - if result is None or len(result) == 0: - return None + return auto_roles - result = result[0] - - return AutoRole( - result[1], - result[2], - result[3], - result[4], - id=result[0] - ) - - def get_auto_roles_by_message_id(self, id: int) -> AutoRole: + def get_auto_role_by_message_id(self, id: int) -> AutoRole: self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}') result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0] return AutoRole( @@ -97,7 +85,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): id=result[0] ) - def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: + def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]: self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_message_id_string(id)}') result = self._context.select(AutoRole.get_select_by_message_id_string(id)) if result is None or len(result) == 0: diff --git a/src/modules/auto_role/command/auto_role_group.py b/src/modules/auto_role/command/auto_role_group.py index 3ef985b0..515d2fe4 100644 --- a/src/modules/auto_role/command/auto_role_group.py +++ b/src/modules/auto_role/command/auto_role_group.py @@ -107,7 +107,7 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.trace(__name__, f'Finished command auto-role add') return - if self._auto_roles.find_auto_roles_by_message_id(int(message_id)) is not None: + if self._auto_roles.find_auto_role_by_message_id(int(message_id)) is not None: self._logger.debug(__name__, f'auto-role for message {message_id} already exists') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id)) self._logger.trace(__name__, f'Finished command auto-role add') diff --git a/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py b/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py index ca0ab888..30936676 100644 --- a/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py +++ b/src/modules/auto_role/events/auto_role_on_raw_reaction_remove.py @@ -1,5 +1,4 @@ from cpl_core.logging import LoggerABC -from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC from cpl_discord.events.on_raw_reaction_remove_abc import OnRawReactionRemoveABC from cpl_discord.service import DiscordBotServiceABC from discord import RawReactionActionEvent