From 844a818aa077848eed941960c147b9d937aeef23 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 30 Dec 2022 13:05:49 +0100 Subject: [PATCH] Fixed auto-role add #151 --- .../src/bot/startup_migration_extension.py | 2 ++ .../migration/auto_role_fix1_migration.py | 29 +++++++++++++++++++ kdb-bot/src/bot_data/model/auto_role.py | 11 +++++-- .../service/auto_role_repository_service.py | 6 ++++ .../auto_role/command/auto_role_group.py | 5 ++-- .../base/helper/base_reaction_handler.py | 3 ++ 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 kdb-bot/src/bot_data/migration/auto_role_fix1_migration.py diff --git a/kdb-bot/src/bot/startup_migration_extension.py b/kdb-bot/src/bot/startup_migration_extension.py index 724db39a..a4bd452c 100644 --- a/kdb-bot/src/bot/startup_migration_extension.py +++ b/kdb-bot/src/bot/startup_migration_extension.py @@ -5,6 +5,7 @@ from cpl_core.environment import ApplicationEnvironmentABC from bot_data.abc.migration_abc import MigrationABC from bot_data.migration.api_migration import ApiMigration +from bot_data.migration.auto_role_fix1_migration import AutoRoleFix1Migration from bot_data.migration.auto_role_migration import AutoRoleMigration from bot_data.migration.initial_migration import InitialMigration from bot_data.migration.level_migration import LevelMigration @@ -27,3 +28,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, ApiMigration) # 15.10.2022 #70 - 0.3.0 services.add_transient(MigrationABC, LevelMigration) # 06.11.2022 #25 - 0.3.0 services.add_transient(MigrationABC, StatsMigration) # 09.11.2022 #46 - 0.3.0 + services.add_transient(MigrationABC, AutoRoleFix1Migration) # 30.12.2022 #151 - 0.3.0 diff --git a/kdb-bot/src/bot_data/migration/auto_role_fix1_migration.py b/kdb-bot/src/bot_data/migration/auto_role_fix1_migration.py new file mode 100644 index 00000000..fd03325e --- /dev/null +++ b/kdb-bot/src/bot_data/migration/auto_role_fix1_migration.py @@ -0,0 +1,29 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class AutoRoleFix1Migration(MigrationABC): + name = '0.3.0_AutoRoleMigration' + + def __init__(self, logger: DatabaseLogger, db: DBContext): + MigrationABC.__init__(self) + self._logger = logger + self._db = db + self._cursor = db.cursor + + def upgrade(self): + self._logger.debug(__name__, 'Running upgrade') + + self._cursor.execute( + str(f""" + ALTER TABLE AutoRoles ADD DiscordChannelId BIGINT NOT NULL AFTER ServerId; + """) + ) + + def downgrade(self): + self._cursor.execute( + str(f""" + ALTER TABLE AutoRoles DROP COLUMN DiscordChannelId; + """) + ) diff --git a/kdb-bot/src/bot_data/model/auto_role.py b/kdb-bot/src/bot_data/model/auto_role.py index dbf73529..54d108c5 100644 --- a/kdb-bot/src/bot_data/model/auto_role.py +++ b/kdb-bot/src/bot_data/model/auto_role.py @@ -6,9 +6,10 @@ 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): + def __init__(self, server_id: int, dc_channel_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_channel_id = dc_channel_id self._discord_message_id = dc_message_id TableABC.__init__(self) @@ -23,6 +24,10 @@ class AutoRole(TableABC): def server_id(self) -> int: return self._server_id + @property + def discord_channel_id(self) -> int: + return self._discord_channel_id + @property def discord_message_id(self) -> int: return self._discord_message_id @@ -58,9 +63,10 @@ class AutoRole(TableABC): def insert_string(self) -> str: return str(f""" INSERT INTO `AutoRoles` ( - `ServerId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt` + `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._server_id}, + {self._discord_channel_id}, {self._discord_message_id}, '{self._created_at}', '{self._modified_at}' @@ -72,6 +78,7 @@ class AutoRole(TableABC): return str(f""" UPDATE `AutoRoles` SET `ServerId` = {self._server_id}, + `DiscordChannelId` = {self._discord_channel_id}, `DiscordMessageId` = {self._discord_message_id}, `LastModifiedAt` = '{self._modified_at}' WHERE `AutoRoleId` = {self._auto_role_id}; diff --git a/kdb-bot/src/bot_data/service/auto_role_repository_service.py b/kdb-bot/src/bot_data/service/auto_role_repository_service.py index 291f0a3f..dd554772 100644 --- a/kdb-bot/src/bot_data/service/auto_role_repository_service.py +++ b/kdb-bot/src/bot_data/service/auto_role_repository_service.py @@ -27,6 +27,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] )) @@ -40,6 +41,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] ) @@ -56,6 +58,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] ) @@ -69,6 +72,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] )) @@ -82,6 +86,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] ) @@ -98,6 +103,7 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result[2], result[3], result[4], + result[5], id=result[0] ) diff --git a/kdb-bot/src/modules/auto_role/command/auto_role_group.py b/kdb-bot/src/modules/auto_role/command/auto_role_group.py index 83a60bff..fd636bf3 100644 --- a/kdb-bot/src/modules/auto_role/command/auto_role_group.py +++ b/kdb-bot/src/modules/auto_role/command/auto_role_group.py @@ -114,7 +114,7 @@ class AutoRoleGroup(DiscordCommandABC): 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._auto_roles.add_auto_role(AutoRole(server_id, int(channel.id), int(message_id))) self._db_context.save_changes() 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)) @@ -239,8 +239,9 @@ class AutoRoleGroup(DiscordCommandABC): self._db_context.save_changes() rule = 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).single() try: - message = await ctx.fetch_message(auto_role_from_db.discord_message_id) guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single() + channel = guild.get_channel(auto_role_from_db.discord_channel_id) + message = await channel.fetch_message(auto_role_from_db.discord_message_id) emoji = List(discord.Emoji, guild.emojis).where(lambda x: x.name == rule.emoji_name).single() if emoji is None: diff --git a/kdb-bot/src/modules/base/helper/base_reaction_handler.py b/kdb-bot/src/modules/base/helper/base_reaction_handler.py index 401f97b1..eb75a54a 100644 --- a/kdb-bot/src/modules/base/helper/base_reaction_handler.py +++ b/kdb-bot/src/modules/base/helper/base_reaction_handler.py @@ -36,6 +36,9 @@ class BaseReactionHandler: self._logger.warn(__name__, f'User {payload.user_id} in {guild.name} not found - skipping') return + if member.bot: + return + server = self._servers.get_server_by_discord_id(guild.id) user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) settings: BaseServerSettings = self._base_helper.get_config(guild.id) -- 2.45.2