Fixed auto-role add #151

This commit is contained in:
Sven Heidemann 2022-12-30 13:05:49 +01:00
parent 33fb973f21
commit 844a818aa0
6 changed files with 52 additions and 4 deletions

View File

@ -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

View File

@ -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;
""")
)

View File

@ -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};

View File

@ -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]
)

View File

@ -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:

View File

@ -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)