From 0b5acdea58b0882b275a896cb110ce8dd620a227 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 15:42:02 +0100 Subject: [PATCH 01/11] Added user joined game server model #181 --- .../src/bot/startup_migration_extension.py | 2 + .../user_joined_game_server_repository_abc.py | 52 ++++++ kdb-bot/src/bot_data/data_module.py | 3 + .../user_joined_game_server_migration.py | 37 ++++ .../bot_data/model/user_joined_game_server.py | 154 +++++++++++++++++ ...r_joined_game_server_repository_service.py | 160 ++++++++++++++++++ 6 files changed, 408 insertions(+) create mode 100644 kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py create mode 100644 kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py create mode 100644 kdb-bot/src/bot_data/model/user_joined_game_server.py create mode 100644 kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py diff --git a/kdb-bot/src/bot/startup_migration_extension.py b/kdb-bot/src/bot/startup_migration_extension.py index 415c8344..a6b13ddd 100644 --- a/kdb-bot/src/bot/startup_migration_extension.py +++ b/kdb-bot/src/bot/startup_migration_extension.py @@ -11,6 +11,7 @@ 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 from bot_data.migration.stats_migration import StatsMigration +from bot_data.migration.user_joined_game_server_migration import UserJoinedGameServerMigration from bot_data.migration.user_message_count_per_hour_migration import ( UserMessageCountPerHourMigration, ) @@ -34,3 +35,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, AutoRoleFix1Migration) # 30.12.2022 #151 - 0.3.0 services.add_transient(MigrationABC, UserMessageCountPerHourMigration) # 11.01.2023 #168 - 0.3.1 services.add_transient(MigrationABC, ApiKeyMigration) # 09.02.2023 #162 - 1.0.0 + services.add_transient(MigrationABC, UserJoinedGameServerMigration) # 12.02.2023 #181 - 1.0.0 diff --git a/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py new file mode 100644 index 00000000..97a0358b --- /dev/null +++ b/kdb-bot/src/bot_data/abc/user_joined_game_server_repository_abc.py @@ -0,0 +1,52 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from cpl_query.extension import List + +from bot_data.model.user_joined_game_server import UserJoinedGameServer + + +class UserJoinedGameServerRepositoryABC(ABC): + @abstractmethod + def __init__(self): + pass + + @abstractmethod + def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: + pass + + @abstractmethod + def get_user_joined_game_server_by_id(self, id: int) -> UserJoinedGameServer: + pass + + @abstractmethod + def get_user_joined_game_servers_by_user_id(self, user_id: int) -> List[UserJoinedGameServer]: + pass + + @abstractmethod + def get_active_user_joined_game_server_by_user_id(self, user_id: int) -> UserJoinedGameServer: + pass + + @abstractmethod + def find_active_user_joined_game_server_by_user_id(self, user_id: int) -> Optional[UserJoinedGameServer]: + pass + + @abstractmethod + def find_active_user_joined_game_servers_by_user_id(self, user_id: int) -> List[Optional[UserJoinedGameServer]]: + pass + + @abstractmethod + def add_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + pass + + @abstractmethod + def update_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + pass + + @abstractmethod + def delete_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + pass + + @abstractmethod + def delete_user_joined_game_server_by_user_id(self, user_id: int): + pass diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index 6e8c8000..16c751a9 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -13,6 +13,7 @@ from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.statistic_repository_abc import StatisticRepositoryABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import ( UserJoinedVoiceChannelRepositoryABC, @@ -30,6 +31,7 @@ from bot_data.service.level_repository_service import LevelRepositoryService from bot_data.service.seeder_service import SeederService from bot_data.service.server_repository_service import ServerRepositoryService from bot_data.service.statistic_repository_service import StatisticRepositoryService +from bot_data.service.user_joined_game_server_repository_service import UserJoinedGameServerRepositoryService from bot_data.service.user_joined_server_repository_service import ( UserJoinedServerRepositoryService, ) @@ -58,6 +60,7 @@ class DataModule(ModuleABC): services.add_transient(KnownUserRepositoryABC, KnownUserRepositoryService) services.add_transient(UserJoinedServerRepositoryABC, UserJoinedServerRepositoryService) services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService) + services.add_transient(UserJoinedGameServerRepositoryABC, UserJoinedGameServerRepositoryService) services.add_transient(AutoRoleRepositoryABC, AutoRoleRepositoryService) services.add_transient(LevelRepositoryABC, LevelRepositoryService) services.add_transient(StatisticRepositoryABC, StatisticRepositoryService) diff --git a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py new file mode 100644 index 00000000..1b5cfb85 --- /dev/null +++ b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py @@ -0,0 +1,37 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class UserJoinedGameServerMigration(MigrationABC): + name = "1.0_UserJoinedGameServerMigration" + + 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""" + CREATE TABLE IF NOT EXISTS `UserJoinedGameServer` ( + `Id` BIGINT NOT NULL AUTO_INCREMENT, + `UserId` BIGINT NOT NULL, + `GameServer` VARCHAR(255) NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6), + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`), + PRIMARY KEY(`Id`) + ); + """ + ) + ) + + def downgrade(self): + self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py new file mode 100644 index 00000000..77616b56 --- /dev/null +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -0,0 +1,154 @@ +from datetime import datetime + +from cpl_core.database import TableABC + +from bot_data.model.user import User + + +class UserJoinedGameServer(TableABC): + def __init__( + self, + user: User, + game_server: str, + joined_on: datetime, + leaved_on: datetime = None, + created_at: datetime = None, + modified_at: datetime = None, + id=0, + ): + self._join_id = id + self._game_server = game_server + self._user = user + self._joined_on = joined_on + self._leaved_on = leaved_on + + 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 join_id(self) -> int: + return self._join_id + + @property + def game_server(self) -> str: + return self._game_server + + @property + def user(self) -> User: + return self._user + + @property + def joined_on(self) -> datetime: + return self._joined_on + + @joined_on.setter + def joined_on(self, value: datetime): + self._modified_at = datetime.now() + self.joined_on = value + + @property + def leaved_on(self) -> datetime: + return self._leaved_on + + @leaved_on.setter + def leaved_on(self, value: datetime): + self._modified_at = datetime.now() + self._leaved_on = value + + @staticmethod + def get_select_all_string() -> str: + return str( + f""" + SELECT * FROM `UserJoinedGameServer`; + """ + ) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserJoinedGameServer` + WHERE `Id` = {id}; + """ + ) + + @staticmethod + def get_select_by_user_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserJoinedGameServer` + WHERE `UserId` = {id}; + """ + ) + + @staticmethod + def get_select_active_by_user_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserJoinedGameServer` + WHERE `UserId` = {id} + AND `LeavedOn` IS NULL; + """ + ) + + @property + def insert_string(self) -> str: + if self._leaved_on is not None: + return str( + f""" + INSERT INTO `UserJoinedGameServer` ( + `UserId`, `GameServer`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.user_id}, + {self._game_server}, + '{self._joined_on}', + '{self._leaved_on}', + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) + else: + return str( + f""" + INSERT INTO `UserJoinedGameServer` ( + `UserId`, `GameServer`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.user_id}, + {self._game_server}, + '{self._joined_on}', + '{self._created_at}', + '{self._modified_at}' + ); + """ + ) + + @property + def udpate_string(self) -> str: + return str( + f""" + UPDATE `UserJoinedGameServer` + SET `LeavedOn` = '{self._leaved_on}', + `LastModifiedAt` = '{self._modified_at}' + WHERE `Id` = {self._join_id}; + """ + ) + + @property + def delete_string(self) -> str: + return str( + f""" + DELETE FROM `UserJoinedGameServer` + WHERE `Id` = {self._join_id}; + """ + ) + + @staticmethod + def delete_by_user_id_string(id: int) -> str: + return str( + f""" + DELETE FROM `UserJoinedGameServer` + WHERE `UserId` = {id} + """ + ) diff --git a/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py new file mode 100644 index 00000000..4d8099f6 --- /dev/null +++ b/kdb-bot/src/bot_data/service/user_joined_game_server_repository_service.py @@ -0,0 +1,160 @@ +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.user_joined_game_server_repository_abc import ( + UserJoinedGameServerRepositoryABC, +) +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user_joined_game_server import UserJoinedGameServer + + +class UserJoinedGameServerRepositoryService(UserJoinedGameServerRepositoryABC): + def __init__( + self, + logger: DatabaseLogger, + db_context: DatabaseContextABC, + users: UserRepositoryABC, + ): + self._logger = logger + self._context = db_context + + self._users = users + + UserJoinedGameServerRepositoryABC.__init__(self) + + def get_user_joined_game_servers(self) -> List[UserJoinedGameServer]: + joins = List(UserJoinedGameServer) + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_all_string()}", + ) + results = self._context.select(UserJoinedGameServer.get_select_all_string()) + for result in results: + self._logger.trace(__name__, f"Get user-joined-game-server with id {result[0]}") + joins.append( + UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + ) + + return joins + + def get_user_joined_game_server_by_id(self, id: int) -> UserJoinedGameServer: + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_by_id_string(id)}", + ) + result = self._context.select(UserJoinedGameServer.get_select_by_id_string(id))[0] + return UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + + def get_user_joined_game_servers_by_user_id(self, user_id: int) -> List[UserJoinedGameServer]: + joins = List(UserJoinedGameServer) + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_by_user_id_string(user_id)}", + ) + results = self._context.select(UserJoinedGameServer.get_select_by_user_id_string(user_id)) + for result in results: + joins.append( + UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + ) + + return joins + + def get_active_user_joined_game_server_by_user_id(self, user_id: int) -> UserJoinedGameServer: + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_by_user_id_string(user_id)}", + ) + result = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id))[0] + return UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + + def find_active_user_joined_game_server_by_user_id(self, user_id: int) -> Optional[UserJoinedGameServer]: + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_by_user_id_string(user_id)}", + ) + result = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return UserJoinedGameServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + + def find_active_user_joined_game_servers_by_user_id(self, user_id: int) -> List[Optional[UserJoinedGameServer]]: + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.get_select_active_by_user_id_string(user_id)}", + ) + result = List(UserJoinedGameServer) + db_results = self._context.select(UserJoinedGameServer.get_select_active_by_user_id_string(user_id)) + + for db_result in db_results: + result.append( + UserJoinedGameServer( + self._users.get_user_by_id(db_result[1]), + db_result[2], + db_result[3], + db_result[4], + db_result[5], + id=db_result[0], + ) + ) + + return result + + def add_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + self._logger.trace(__name__, f"Send SQL command: {user_joined_game_server.insert_string}") + self._context.cursor.execute(user_joined_game_server.insert_string) + + def update_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + self._logger.trace(__name__, f"Send SQL command: {user_joined_game_server.udpate_string}") + self._context.cursor.execute(user_joined_game_server.udpate_string) + + def delete_user_joined_game_server(self, user_joined_game_server: UserJoinedGameServer): + self._logger.trace(__name__, f"Send SQL command: {user_joined_game_server.delete_string}") + self._context.cursor.execute(user_joined_game_server.delete_string) + + def delete_user_joined_game_server_by_user_id(self, user_id: int): + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedGameServer.delete_by_user_id_string}", + ) + self._context.cursor.execute(UserJoinedGameServer.delete_by_user_id_string(user_id)) -- 2.45.2 From 1e74584e71a398b9729289ed9b9374d520c3a899 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 16:16:17 +0100 Subject: [PATCH 02/11] Added user joined game server gql query #181 --- .../bot_data/model/user_joined_game_server.py | 20 +++--- kdb-bot/src/bot_graphql/abc/query_abc.py | 8 +++ .../filter/user_joined_game_server_filter.py | 62 +++++++++++++++++++ kdb-bot/src/bot_graphql/graphql_module.py | 4 ++ kdb-bot/src/bot_graphql/model/query.gql | 7 ++- kdb-bot/src/bot_graphql/model/user.gql | 3 + .../model/userJoinedGameServer.gql | 18 ++++++ .../queries/user_joined_game_server_query.py | 37 +++++++++++ kdb-bot/src/bot_graphql/queries/user_query.py | 9 +++ kdb-bot/src/bot_graphql/query.py | 9 +++ 10 files changed, 165 insertions(+), 12 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py create mode 100644 kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql create mode 100644 kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index 77616b56..4044321d 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -16,9 +16,9 @@ class UserJoinedGameServer(TableABC): modified_at: datetime = None, id=0, ): - self._join_id = id - self._game_server = game_server + self._id = id self._user = user + self._game_server = game_server self._joined_on = joined_on self._leaved_on = leaved_on @@ -27,17 +27,17 @@ class UserJoinedGameServer(TableABC): self._modified_at = modified_at if modified_at is not None else self._modified_at @property - def join_id(self) -> int: - return self._join_id - - @property - def game_server(self) -> str: - return self._game_server + def id(self) -> int: + return self._id @property def user(self) -> User: return self._user + @property + def game_server(self) -> str: + return self._game_server + @property def joined_on(self) -> datetime: return self._joined_on @@ -131,7 +131,7 @@ class UserJoinedGameServer(TableABC): UPDATE `UserJoinedGameServer` SET `LeavedOn` = '{self._leaved_on}', `LastModifiedAt` = '{self._modified_at}' - WHERE `Id` = {self._join_id}; + WHERE `Id` = {self._id}; """ ) @@ -140,7 +140,7 @@ class UserJoinedGameServer(TableABC): return str( f""" DELETE FROM `UserJoinedGameServer` - WHERE `Id` = {self._join_id}; + WHERE `Id` = {self._id}; """ ) diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index fd652ec0..09cc3414 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -17,6 +17,7 @@ from bot_data.model.known_user import KnownUser from bot_data.model.level import Level from bot_data.model.server import Server from bot_data.model.user import User +from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_data.model.user_joined_server import UserJoinedServer from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from bot_data.model.user_role_enum import UserRoleEnum @@ -141,6 +142,13 @@ class QueryABC(ObjectType): access = True break + elif type(element) == UserJoinedGameServer: + for u in user.users: + u: User = u + if u.user_id == element.user.user_id: + access = True + break + return access @ServiceProviderABC.inject diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py new file mode 100644 index 00000000..2af843a6 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py @@ -0,0 +1,62 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_data.model.user_joined_game_server import UserJoinedGameServer +from bot_graphql.abc.filter_abc import FilterABC + + +class UserJoinedGameServerFilter(FilterABC): + def __init__( + self, + services: ServiceProviderABC, + bot: DiscordBotServiceABC, + ): + FilterABC.__init__(self) + + self._services = services + self._bot = bot + + self._id = None + self._game_server = None + self._user = None + self._joined_on = None + self._leaved_on = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = int(values["id"]) + + if "gameServer" in values: + self._game_server = values["gameServer"] + + if "user" in values: + from bot_graphql.filter.user_filter import UserFilter + + self._user: UserFilter = self._services.get_service(UserFilter) + self._user.from_dict(values["user"]) + + if "joinedOn" in values: + self._joined_on = values["joinedOn"] + + if "leavedOn" in values: + self._leaved_on = values["leavedOn"] + + def filter(self, query: List[UserJoinedGameServer]) -> List[UserJoinedGameServer]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._game_server is not None: + query = query.where(lambda x: x.game_server == self._game_server) + + if self._user is not None: + users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id) + query = query.where(lambda x: x.user.user_id in users) + + if self._joined_on is not None: + query = query.where(lambda x: x.joined_on == self._joined_on or self._joined_on in x.joined_on) + + if self._leaved_on is not None: + query = query.where(lambda x: x.leaved_on == self._leaved_on or self._leaved_on in x.leaved_on) + + return query diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 43da66e0..91ad4d20 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -14,6 +14,7 @@ from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.user_filter import UserFilter +from bot_graphql.filter.user_joined_game_server_filter import UserJoinedGameServerFilter from bot_graphql.filter.user_joined_server_filter import UserJoinedServerFilter from bot_graphql.filter.user_joined_voice_channel_filter import UserJoinedVoiceChannelFilter from bot_graphql.graphql_service import GraphQLService @@ -28,6 +29,7 @@ from bot_graphql.queries.client_query import ClientQuery from bot_graphql.queries.known_user_query import KnownUserQuery from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery +from bot_graphql.queries.user_joined_game_server_query import UserJoinedGameServerQuery from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery from bot_graphql.queries.user_joined_voice_channel_query import UserJoinedVoiceChannelQuery from bot_graphql.queries.user_query import UserQuery @@ -58,6 +60,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserQuery) services.add_transient(QueryABC, UserJoinedServerQuery) services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) + services.add_transient(QueryABC, UserJoinedGameServerQuery) # filters services.add_singleton(FilterABC, AutoRoleFilter) @@ -68,6 +71,7 @@ class GraphQLModule(ModuleABC): services.add_singleton(FilterABC, UserFilter) services.add_singleton(FilterABC, UserJoinedServerFilter) services.add_singleton(FilterABC, UserJoinedVoiceChannelFilter) + services.add_singleton(FilterABC, UserJoinedGameServerFilter) # mutations services.add_transient(QueryABC, AutoRoleMutation) diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index 46f80381..2f597800 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -18,10 +18,13 @@ type Query { servers(filter: ServerFilter, page: Page, sort: Sort): [Server] userJoinedServerCount: Int - userJoinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] + userJoinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [UserJoinedServer] userJoinedVoiceChannelCount: Int - userJoinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] + userJoinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [UserJoinedVoiceChannel] + + userJoinedGameServerCount: Int + userJoinedGameServers(filter: UserJoinedGameServerFilter, page: Page, sort: Sort): [UserJoinedGameServer] userCount: Int users(filter: UserFilter, page: Page, sort: Sort): [User] diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 07f7acc4..b7cf5fdf 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -12,6 +12,9 @@ type User implements TableQuery { joinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [UserJoinedVoiceChannel] joinedVoiceChannelCount: Int + userJoinedGameServerCount: Int + userJoinedGameServers(filter: UserJoinedGameServerFilter, page: Page, sort: Sort): [UserJoinedGameServer] + server: Server createdAt: String diff --git a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql new file mode 100644 index 00000000..02a716eb --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql @@ -0,0 +1,18 @@ +type UserJoinedGameServer implements TableQuery { + id: ID + gameServer: String + user: User + joinedOn: String + leavedOn: String + + createdAt: String + modifiedAt: String +} + +input UserJoinedGameServerFilter { + id: ID + gameServer: String + user: UserFilter + joinedOn: String + leavedOn: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py new file mode 100644 index 00000000..877fa8b7 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/user_joined_game_server_query.py @@ -0,0 +1,37 @@ +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.model.user_joined_game_server import UserJoinedGameServer +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class UserJoinedGameServerQuery(DataQueryABC): + def __init__(self, bot: DiscordBotServiceABC): + DataQueryABC.__init__(self, "UserJoinedGameServer") + + self._bot = bot + + self.set_field("id", self.resolve_id) + self.set_field("gameServer", self.resolve_game_server) + self.set_field("user", self.resolve_user) + self.set_field("joinedOn", self.resolve_joined_on) + self.set_field("leavedOn", self.resolve_leaved_on) + + @staticmethod + def resolve_id(x: UserJoinedGameServer, *_): + return x.id + + @staticmethod + def resolve_game_server(x: UserJoinedGameServer, *_): + return x.game_server + + @staticmethod + def resolve_user(x: UserJoinedGameServer, *_): + return x.user + + @staticmethod + def resolve_joined_on(x: UserJoinedGameServer, *_): + return x.joined_on + + @staticmethod + def resolve_leaved_on(x: UserJoinedGameServer, *_): + return x.leaved_on diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index eca30928..1716d488 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -1,10 +1,12 @@ from cpl_discord.service import DiscordBotServiceABC from bot_core.abc.client_utils_abc import ClientUtilsABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.model.user import User from bot_graphql.abc.data_query_abc import DataQueryABC +from bot_graphql.filter.user_joined_game_server_filter import UserJoinedGameServerFilter from bot_graphql.filter.user_joined_server_filter import UserJoinedServerFilter from bot_graphql.filter.user_joined_voice_channel_filter import UserJoinedVoiceChannelFilter from modules.level.service.level_service import LevelService @@ -18,12 +20,14 @@ class UserQuery(DataQueryABC): client_utils: ClientUtilsABC, ujs: UserJoinedServerRepositoryABC, ujvs: UserJoinedVoiceChannelRepositoryABC, + user_joined_game_server: UserJoinedGameServerRepositoryABC, ): DataQueryABC.__init__(self, "User") self._bot = bot self._levels = levels self._client_utils = client_utils + self._user_joined_game_server = user_joined_game_server self._ujs = ujs self._ujvs = ujvs @@ -43,6 +47,11 @@ class UserQuery(DataQueryABC): lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id), UserJoinedVoiceChannelFilter, ) + self.add_collection( + "userJoinedGameServer", + lambda user, *_: self._user_joined_game_server.get_user_joined_game_servers_by_user_id(user.user_id), + UserJoinedGameServerFilter, + ) self.set_field("server", self.resolve_server) @staticmethod diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index aa5948d9..252a416b 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -3,6 +3,7 @@ from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC @@ -13,6 +14,7 @@ from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.user_filter import UserFilter +from bot_graphql.filter.user_joined_game_server_filter import UserJoinedGameServerFilter from bot_graphql.filter.user_joined_server_filter import UserJoinedServerFilter from bot_graphql.filter.user_joined_voice_channel_filter import UserJoinedVoiceChannelFilter @@ -27,6 +29,7 @@ class Query(QueryABC): servers: ServerRepositoryABC, user_joined_servers: UserJoinedServerRepositoryABC, user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, + user_joined_game_server: UserJoinedGameServerRepositoryABC, users: UserRepositoryABC, ): QueryABC.__init__(self, "Query") @@ -37,6 +40,7 @@ class Query(QueryABC): self._servers = servers self._user_joined_servers = user_joined_servers self._user_joined_voice_channels = user_joined_voice_channel + self._user_joined_game_server = user_joined_game_server self._users = users self.add_collection("autoRole", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) @@ -53,4 +57,9 @@ class Query(QueryABC): lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels(), UserJoinedVoiceChannelFilter, ) + self.add_collection( + "userJoinedGameServer", + lambda *_: self._user_joined_game_server.get_user_joined_game_servers(), + UserJoinedGameServerFilter, + ) self.add_collection("user", lambda *_: self._users.get_users(), UserFilter) -- 2.45.2 From bd4e08803234feb984ac8b60189854f04c965a9b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 16:39:26 +0100 Subject: [PATCH 03/11] Added mutations for user joined game server #181 --- .../bot_data/model/user_joined_game_server.py | 4 +- kdb-bot/src/bot_graphql/graphql_module.py | 2 + kdb-bot/src/bot_graphql/model/mutation.gql | 1 + .../model/userJoinedGameServer.gql | 12 ++++ kdb-bot/src/bot_graphql/mutation.py | 29 +++----- .../user_joined_game_server_mutation.py | 66 +++++++++++++++++++ 6 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py diff --git a/kdb-bot/src/bot_data/model/user_joined_game_server.py b/kdb-bot/src/bot_data/model/user_joined_game_server.py index 4044321d..d13beed0 100644 --- a/kdb-bot/src/bot_data/model/user_joined_game_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_game_server.py @@ -101,7 +101,7 @@ class UserJoinedGameServer(TableABC): `UserId`, `GameServer`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._user.user_id}, - {self._game_server}, + '{self._game_server}', '{self._joined_on}', '{self._leaved_on}', '{self._created_at}', @@ -116,7 +116,7 @@ class UserJoinedGameServer(TableABC): `UserId`, `GameServer`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._user.user_id}, - {self._game_server}, + '{self._game_server}', '{self._joined_on}', '{self._created_at}', '{self._modified_at}' diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 91ad4d20..14c7a841 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -22,6 +22,7 @@ from bot_graphql.mutation import Mutation from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation from bot_graphql.queries.auto_role_query import AutoRoleQuery from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery @@ -78,5 +79,6 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, AutoRoleRuleMutation) services.add_transient(QueryABC, LevelMutation) services.add_transient(QueryABC, UserMutation) + services.add_transient(QueryABC, UserJoinedGameServerMutation) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 5290dd1c..9631fd8d 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -3,4 +3,5 @@ type Mutation { autoRoleRule: AutoRoleRuleMutation level: LevelMutation user: UserMutation + userJoinedGameServer: UserJoinedGameServerMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql index 02a716eb..c86f53d9 100644 --- a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql +++ b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql @@ -15,4 +15,16 @@ input UserJoinedGameServerFilter { user: UserFilter joinedOn: String leavedOn: String +} + +type UserJoinedGameServerMutation { + userJoined(input: UserJoinedGameServerInput!): UserJoinedGameServer + userLeaved(input: UserJoinedGameServerInput!): UserJoinedGameServer +} + +input UserJoinedGameServerInput { + gameServer: String! + userId: ID! + joinedOn: String + leavedOn: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index 5b980c14..56bfa7c5 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -1,8 +1,10 @@ from ariadne import MutationType +from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -13,27 +15,12 @@ class Mutation(MutationType): auto_role_rule_mutation: AutoRoleRuleMutation, level_mutation: LevelMutation, user_mutation: UserMutation, + user_joined_game_server: UserJoinedGameServerMutation, ): MutationType.__init__(self) - self._auto_role_mutation = auto_role_mutation - self._auto_role_rule_mutation = auto_role_rule_mutation - self._level_mutation = level_mutation - self._user_mutation = user_mutation - - self.set_field("autoRole", self.resolve_auto_role) - self.set_field("autoRoleRule", self.resolve_auto_role_rule) - self.set_field("level", self.resolve_level) - self.set_field("user", self.resolve_user) - - def resolve_auto_role(self, *_): - return self._auto_role_mutation - - def resolve_auto_role_rule(self, *_): - return self._auto_role_rule_mutation - - def resolve_level(self, *_): - return self._level_mutation - - def resolve_user(self, *_): - return self._user_mutation + self.set_field("autoRole", lambda *_: auto_role_mutation) + self.set_field("autoRoleRule", lambda *_: auto_role_rule_mutation) + self.set_field("level", lambda *_: level_mutation) + self.set_field("user", lambda *_: user_mutation) + self.set_field("userJoinedGameServer", lambda *_: user_joined_game_server) diff --git a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py new file mode 100644 index 00000000..6e7ac328 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -0,0 +1,66 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user_joined_game_server import UserJoinedGameServer +from bot_data.model.user_role_enum import UserRoleEnum +from bot_graphql.abc.query_abc import QueryABC +from modules.permission.service.permission_service import PermissionService + + +class UserJoinedGameServerMutation(QueryABC): + def __init__( + self, + logger: LoggerABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + user_joined_game_servers: UserJoinedGameServerRepositoryABC, + bot: DiscordBotServiceABC, + db: DatabaseContextABC, + permissions: PermissionService, + ): + QueryABC.__init__(self, "UserJoinedGameServerMutation") + + self._logger = logger + self._servers = servers + self._users = users + self._user_joined_game_servers = user_joined_game_servers + self._bot = bot + self._db = db + self._permissions = permissions + + self.set_field("userJoined", self.resolve_user_joined) + self.set_field("userLeaved", self.resolve_user_leaved) + + def resolve_user_joined(self, *_, input: dict): + user = self._users.get_user_by_id(input["userId"]) + self._can_user_mutate_data(user.server, UserRoleEnum.admin) + + active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.user_id) + if active is not None: + self._logger.debug( + __name__, + f"Skip UserJoinedGameServer for user {user.user_id}. User already plays on {active.game_server}.", + ) + return + + new = UserJoinedGameServer(user, input["gameServer"], input["joinedOn"]) + self._user_joined_game_servers.add_user_joined_game_server(new) + self._db.save_changes() + + return self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id) + + def resolve_user_leaved(self, *_, input: dict): + user = self._users.get_user_by_id(input["userId"]) + self._can_user_mutate_data(user.server, UserRoleEnum.admin) + + active = self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id) + active.leaved_on = input["leavedOn"] + + self._user_joined_game_servers.update_user_joined_game_server(active) + self._db.save_changes() + + return active -- 2.45.2 From 2ef4d079ed9ef7c2e5cd818896c0673bb9c3710c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 16:55:35 +0100 Subject: [PATCH 04/11] Added minecraft id #181 --- .../user_joined_game_server_migration.py | 15 ++++++++ kdb-bot/src/bot_data/model/user.py | 10 +++++- .../service/user_repository_service.py | 35 ++++++++++++++----- kdb-bot/src/bot_graphql/filter/user_filter.py | 7 ++++ kdb-bot/src/bot_graphql/model/user.gql | 2 ++ kdb-bot/src/bot_graphql/queries/user_query.py | 5 +++ 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py index 1b5cfb85..e29854aa 100644 --- a/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py +++ b/kdb-bot/src/bot_data/migration/user_joined_game_server_migration.py @@ -33,5 +33,20 @@ class UserJoinedGameServerMigration(MigrationABC): ) ) + self._cursor.execute( + str( + f""" + ALTER TABLE Users ADD MinecraftId VARCHAR(255) NULL AFTER XP; + """ + ) + ) + def downgrade(self): self._cursor.execute("DROP TABLE `UserJoinedGameServer`;") + self._cursor.execute( + str( + f""" + ALTER TABLE Users DROP COLUMN MinecraftId; + """ + ) + ) diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index ab97ecd1..7674e78b 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -11,6 +11,7 @@ class User(TableABC): self, dc_id: int, xp: int, + minecraft_id: Optional[str], server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, @@ -19,6 +20,7 @@ class User(TableABC): self._user_id = id self._discord_id = dc_id self._xp = xp + self._minecraft_id = minecraft_id self._server = server TableABC.__init__(self) @@ -42,6 +44,10 @@ class User(TableABC): self._modified_at = datetime.now().isoformat() self._xp = value + @property + def minecraft_id(self) -> Optional[str]: + return self._minecraft_id + @property def server(self) -> Optional[Server]: return self._server @@ -96,10 +102,11 @@ class User(TableABC): return str( f""" INSERT INTO `Users` ( - `DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt` + `DiscordId`, `XP`, `MinecraftId`, `ServerId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._discord_id}, {self._xp}, + '{"NULL" if self._minecraft_id is None else self._minecraft_id}', {self._server.server_id}, '{self._created_at}', '{self._modified_at}' @@ -113,6 +120,7 @@ class User(TableABC): f""" UPDATE `Users` SET `XP` = {self._xp}, + `MinecraftId` = '{"NULL" if self._minecraft_id is None else self._minecraft_id}', `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user_id}; """ diff --git a/kdb-bot/src/bot_data/service/user_repository_service.py b/kdb-bot/src/bot_data/service/user_repository_service.py index 9d44c0ba..44207824 100644 --- a/kdb-bot/src/bot_data/service/user_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_repository_service.py @@ -33,7 +33,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -47,7 +50,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -62,7 +68,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -78,7 +87,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -97,7 +109,10 @@ class UserRepositoryService(UserRepositoryABC): User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) ) @@ -114,7 +129,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), + result[3], + self._servers.get_server_by_id(result[4]), + result[5], + result[6], id=result[0], ) @@ -132,9 +150,10 @@ class UserRepositoryService(UserRepositoryABC): return User( result[1], result[2], - self._servers.get_server_by_id(result[3]), - result[4], + result[3], + self._servers.get_server_by_id(result[4]), result[5], + result[6], id=result[0], ) diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index b0028674..8b9bdb74 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -30,6 +30,7 @@ class UserFilter(FilterABC): self._discord_id = None self._name = None self._xp = None + self._minecraft_id = None self._ontime = None self._level: Optional[LevelFilter] = None @@ -46,6 +47,9 @@ class UserFilter(FilterABC): if "xp" in values: self._xp = int(values["xp"]) + if "minecraftId" in values: + self._minecraft_id = values["minecraftId"] + if "ontime" in values: self._ontime = int(values["ontime"]) @@ -75,6 +79,9 @@ class UserFilter(FilterABC): if self._xp is not None: query = query.where(lambda x: x.xp == self._xp) + if self._minecraft_id is not None: + query = query.where(lambda x: x.minecraft_id == self._minecraft_id) + if self._ontime is not None: query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index b7cf5fdf..853b1ad4 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -3,6 +3,7 @@ type User implements TableQuery { discordId: String name: String xp: Int + minecraftId: String ontime: Float level: Level @@ -26,6 +27,7 @@ input UserFilter { discordId: String name: String xp: Int + minecraftId: String ontime: Float level: LevelFilter server: ServerFilter diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 1716d488..ddcda7b9 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -35,6 +35,7 @@ class UserQuery(DataQueryABC): self.set_field("discordId", self.resolve_discord_id) self.set_field("name", self.resolve_name) self.set_field("xp", self.resolve_xp) + self.set_field("minecraftId", self.resolve_minecraft_id) self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) self.add_collection( @@ -71,6 +72,10 @@ class UserQuery(DataQueryABC): def resolve_xp(user: User, *_): return user.xp + @staticmethod + def resolve_minecraft_id(user: User, *_): + return user.minecraft_id + def resolve_ontime(self, user: User, *_): return self._client_utils.get_ontime_for_user(user) -- 2.45.2 From aa16847fbfa6e65a9ecdb89e9234d9d1955d5ce6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 17:22:58 +0100 Subject: [PATCH 05/11] Added minecraft register commands #181 --- kdb-bot/src/bot/translation/de.json | 7 ++ kdb-bot/src/bot_data/model/user.py | 4 + kdb-bot/src/modules/base/base_module.py | 4 + .../modules/base/command/register_group.py | 85 +++++++++++++++++++ .../modules/base/command/unregister_group.py | 64 ++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 kdb-bot/src/modules/base/command/register_group.py create mode 100644 kdb-bot/src/modules/base/command/unregister_group.py diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 25c1301e..756ffb76 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -200,6 +200,13 @@ "error": { "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" } + }, + "register": { + "success": "Spieler wurde mit dem Mitglied verlinkt :D", + "not_found": "Benutzer nicht gefunden!" + }, + "unregister": { + "success": "Verlinkung wurde entfernt :D" } }, "boot_log": { diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index 7674e78b..a2f4de81 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -48,6 +48,10 @@ class User(TableABC): def minecraft_id(self) -> Optional[str]: return self._minecraft_id + @minecraft_id.setter + def minecraft_id(self, value: str): + self._minecraft_id = value + @property def server(self) -> Optional[Server]: return self._server diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index 9377c6e9..3c470bbb 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -14,6 +14,8 @@ from modules.base.command.mass_move_command import MassMoveCommand from modules.base.command.ping_command import PingCommand from modules.base.command.presence_command import PresenceCommand from modules.base.command.purge_command import PurgeCommand +from modules.base.command.register_group import RegisterGroup +from modules.base.command.unregister_group import UnregisterGroup from modules.base.command.user_group import UserGroup from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent from modules.base.events.base_on_command_event import BaseOnCommandEvent @@ -53,6 +55,8 @@ class BaseModule(ModuleABC): self._dc.add_command(PurgeCommand) self._dc.add_command(UserGroup) + self._dc.add_command(RegisterGroup) + self._dc.add_command(UnregisterGroup) # events self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) diff --git a/kdb-bot/src/modules/base/command/register_group.py b/kdb-bot/src/modules/base/command/register_group.py new file mode 100644 index 00000000..b47329cb --- /dev/null +++ b/kdb-bot/src/modules/base/command/register_group.py @@ -0,0 +1,85 @@ +import discord +import requests +from cpl_core.database.context import DatabaseContextABC +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_abc import ClientUtilsABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC + + +class RegisterGroup(DiscordCommandABC): + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + t: TranslatePipe, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._servers = servers + self._users = users + self._db = db + self._t = t + + self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") + + @commands.hybrid_group() + @commands.guild_only() + async def register(self, ctx: Context): + pass + + @register.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def minecraft(self, ctx: Context, member: discord.Member, name: str): + self._logger.debug(__name__, f"Received command register minecraft {ctx}") + + minecraft_id = None + try: + self._logger.debug(__name__, f"Try to get minecraft id for {name}") + response = requests.get(url=f"https://api.mojang.com/users/profiles/minecraft/{name}") + if len(response.content) == 0: + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.not_found") + ) + return + + minecraft_id = response.json()["id"] + except Exception as e: + self._logger.error(__name__, f"Get minecraft id for {name} failed", e) + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.not_found") + ) + + if minecraft_id is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + user.minecraft_id = minecraft_id + self._users.update_user(user) + self._db.save_changes() + + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.success") + ) + + self._logger.trace(__name__, f"Finished register minecraft command") diff --git a/kdb-bot/src/modules/base/command/unregister_group.py b/kdb-bot/src/modules/base/command/unregister_group.py new file mode 100644 index 00000000..c929f934 --- /dev/null +++ b/kdb-bot/src/modules/base/command/unregister_group.py @@ -0,0 +1,64 @@ +import discord +from cpl_core.database.context import DatabaseContextABC +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_abc import ClientUtilsABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC + + +class UnregisterGroup(DiscordCommandABC): + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + t: TranslatePipe, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._servers = servers + self._users = users + self._db = db + self._t = t + + self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") + + @commands.hybrid_group() + @commands.guild_only() + async def unregister(self, ctx: Context): + pass + + @unregister.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def minecraft(self, ctx: Context, member: discord.Member): + self._logger.debug(__name__, f"Received command register minecraft {ctx}") + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + user.minecraft_id = None + self._users.update_user(user) + self._db.save_changes() + + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.unregister.success") + ) + + self._logger.trace(__name__, f"Finished register minecraft command") -- 2.45.2 From 54cb6cf8a0a21c284dda2a638af6d72cb5203d85 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 17:42:59 +0100 Subject: [PATCH 06/11] Fixed user filter #181 --- kdb-bot/src/bot_graphql/filter/user_filter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 8b9bdb74..cf1a9bb6 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -33,6 +33,7 @@ class UserFilter(FilterABC): self._minecraft_id = None self._ontime = None self._level: Optional[LevelFilter] = None + self._server = None def from_dict(self, values: dict): if "id" in values: -- 2.45.2 From 667bb708d99892eaf20e6866b7ef058e5a563163 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 19:47:27 +0100 Subject: [PATCH 07/11] Improved user joined game server mutation #181 --- .../filter/user_joined_game_server_filter.py | 14 ------------- .../model/userJoinedGameServer.gql | 2 -- .../user_joined_game_server_mutation.py | 20 +++++++++++++++++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py index 2af843a6..1cdb4d4e 100644 --- a/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_joined_game_server_filter.py @@ -20,8 +20,6 @@ class UserJoinedGameServerFilter(FilterABC): self._id = None self._game_server = None self._user = None - self._joined_on = None - self._leaved_on = None def from_dict(self, values: dict): if "id" in values: @@ -36,12 +34,6 @@ class UserJoinedGameServerFilter(FilterABC): self._user: UserFilter = self._services.get_service(UserFilter) self._user.from_dict(values["user"]) - if "joinedOn" in values: - self._joined_on = values["joinedOn"] - - if "leavedOn" in values: - self._leaved_on = values["leavedOn"] - def filter(self, query: List[UserJoinedGameServer]) -> List[UserJoinedGameServer]: if self._id is not None: query = query.where(lambda x: x.id == self._id) @@ -53,10 +45,4 @@ class UserJoinedGameServerFilter(FilterABC): users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id) query = query.where(lambda x: x.user.user_id in users) - if self._joined_on is not None: - query = query.where(lambda x: x.joined_on == self._joined_on or self._joined_on in x.joined_on) - - if self._leaved_on is not None: - query = query.where(lambda x: x.leaved_on == self._leaved_on or self._leaved_on in x.leaved_on) - return query diff --git a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql index c86f53d9..63d66daa 100644 --- a/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql +++ b/kdb-bot/src/bot_graphql/model/userJoinedGameServer.gql @@ -25,6 +25,4 @@ type UserJoinedGameServerMutation { input UserJoinedGameServerInput { gameServer: String! userId: ID! - joinedOn: String - leavedOn: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py index 6e7ac328..ffdb2661 100644 --- a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -1,3 +1,5 @@ +from datetime import datetime + from cpl_core.database.context import DatabaseContextABC from cpl_core.logging import LoggerABC from cpl_discord.service import DiscordBotServiceABC @@ -8,6 +10,8 @@ from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC +from modules.base.abc.base_helper_abc import BaseHelperABC +from modules.base.configuration.base_server_settings import BaseServerSettings from modules.permission.service.permission_service import PermissionService @@ -15,6 +19,7 @@ class UserJoinedGameServerMutation(QueryABC): def __init__( self, logger: LoggerABC, + base_helper: BaseHelperABC, servers: ServerRepositoryABC, users: UserRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC, @@ -25,6 +30,7 @@ class UserJoinedGameServerMutation(QueryABC): QueryABC.__init__(self, "UserJoinedGameServerMutation") self._logger = logger + self._base_helper = base_helper self._servers = servers self._users = users self._user_joined_game_servers = user_joined_game_servers @@ -47,7 +53,7 @@ class UserJoinedGameServerMutation(QueryABC): ) return - new = UserJoinedGameServer(user, input["gameServer"], input["joinedOn"]) + new = UserJoinedGameServer(user, input["gameServer"], datetime.now()) self._user_joined_game_servers.add_user_joined_game_server(new) self._db.save_changes() @@ -58,9 +64,19 @@ class UserJoinedGameServerMutation(QueryABC): self._can_user_mutate_data(user.server, UserRoleEnum.admin) active = self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id) - active.leaved_on = input["leavedOn"] + active.leaved_on = datetime.now() + + settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_server_id) + + ontime = round((active.leaved_on - active.joined_on).total_seconds() / 3600, 2) + old_xp = user.xp + user.xp += round(ontime * settings.xp_per_ontime_hour) self._user_joined_game_servers.update_user_joined_game_server(active) self._db.save_changes() + self._logger.debug( + __name__, + f"User {user} leaved_on {active.leaved_on}. Ontime: {ontime}h | xp: from {old_xp} to {user.xp}", + ) return active -- 2.45.2 From 9a8f8a13f6d23d17c7d7a6c8c39ae373b7ab6d0c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 20:00:47 +0100 Subject: [PATCH 08/11] Improved user joined game server mutation #181 --- .../bot_graphql/mutations/user_joined_game_server_mutation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py index ffdb2661..d36487a9 100644 --- a/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -63,7 +63,9 @@ class UserJoinedGameServerMutation(QueryABC): user = self._users.get_user_by_id(input["userId"]) self._can_user_mutate_data(user.server, UserRoleEnum.admin) - active = self._user_joined_game_servers.get_active_user_joined_game_server_by_user_id(user.user_id) + active = self._user_joined_game_servers.find_active_user_joined_game_server_by_user_id(user.user_id) + if active is None: + return None active.leaved_on = datetime.now() settings: BaseServerSettings = self._base_helper.get_config(user.server.discord_server_id) -- 2.45.2 From ce1cdd4017d7bf5f0093f19cd440fe05051e457f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 20:30:42 +0100 Subject: [PATCH 09/11] Added user joined game server validation checks on start #181 --- kdb-bot/src/bot_graphql/abc/query_abc.py | 6 +-- .../database/database_on_ready_event.py | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index 09cc3414..b9e9f444 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -50,15 +50,15 @@ class QueryABC(ObjectType): sort.from_dict(kwargs["sort"]) kwargs["sort"] = sort - collection = get_collection(*args) + collection: List = get_collection(*args) user = Route.get_user() if user == "system" or user.auth_role == AuthRoleEnum.admin: return self._resolve_collection(collection, *args, **kwargs) - for x in collection: + for x in collection.to_list(): if not self._can_user_see_element(user, x): - return List() + collection.remove(x) return self._resolve_collection(collection, *args, **kwargs) diff --git a/kdb-bot/src/modules/database/database_on_ready_event.py b/kdb-bot/src/modules/database/database_on_ready_event.py index 08aa8177..72f35442 100644 --- a/kdb-bot/src/modules/database/database_on_ready_event.py +++ b/kdb-bot/src/modules/database/database_on_ready_event.py @@ -11,6 +11,7 @@ from bot_core.logging.database_logger import DatabaseLogger from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import ( UserJoinedVoiceChannelRepositoryABC, @@ -41,6 +42,7 @@ class DatabaseOnReadyEvent(OnReadyABC): known_users: KnownUserRepositoryABC, user_joins: UserJoinedServerRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC, + user_joined_gs: UserJoinedGameServerRepositoryABC, dtp: DateTimeOffsetPipe, ): self._config = config @@ -55,6 +57,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._known_users = known_users self._user_joins = user_joins self._user_joins_vc = user_joins_vc + self._user_joined_gs = user_joined_gs self._dtp = dtp OnReadyABC.__init__(self) @@ -327,6 +330,48 @@ class DatabaseOnReadyEvent(OnReadyABC): except Exception as e: self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) + def _check_user_joined_gs(self): + self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table") + for guild in self._bot.guilds: + guild: discord.Guild = guild + + server = self._servers.find_server_by_discord_id(guild.id) + if server is None: + self._logger.fatal(__name__, f"Server not found in database: {guild.id}") + + try: + for member in guild.members: + if member.bot: + self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot") + continue + + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + if user is None: + self._logger.fatal(__name__, f"User not found in database: {member.id}") + + joins = self._user_joined_gs.find_active_user_joined_game_servers_by_user_id(user.user_id) + if joins is None or len(joins) == 0: + continue + + for join in joins: + self._logger.warn( + __name__, + f"Active UserJoinedGameServer found in database: {guild.id}:{member.id}@{join.joined_on}", + ) + join.leaved_on = datetime.now() + settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}") + + if ( + (join.leaved_on - join.joined_on).total_seconds() / 60 / 60 + ) > settings.max_voice_state_hours: + join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours) + + self._user_joined_gs.update_user_joined_game_server(join) + # todo: maybe add XP + self._db_context.save_changes() + except Exception as e: + self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) + async def on_ready(self): self._logger.debug(__name__, f"Module {type(self)} started") @@ -338,6 +383,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._check_users() self._check_user_joins() self._check_user_joins_vc() + self._check_user_joined_gs() self._validate_init_time() self._logger.trace(__name__, f"Module {type(self)} stopped") -- 2.45.2 From 3844240930cfe24b5187725118cb41bb0b954f49 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 13 Feb 2023 22:20:53 +0100 Subject: [PATCH 10/11] Fixed filter error #181 --- kdb-bot/src/bot_data/model/user_role_enum.py | 1 - kdb-bot/src/bot_graphql/filter/user_filter.py | 2 +- kdb-bot/src/bot_graphql/graphql_module.py | 18 +++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/kdb-bot/src/bot_data/model/user_role_enum.py b/kdb-bot/src/bot_data/model/user_role_enum.py index edfaf85d..ad612e1d 100644 --- a/kdb-bot/src/bot_data/model/user_role_enum.py +++ b/kdb-bot/src/bot_data/model/user_role_enum.py @@ -2,7 +2,6 @@ from enum import Enum class UserRoleEnum(Enum): - member = 0 moderator = 1 admin = 2 diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index cf1a9bb6..d5122d87 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -66,7 +66,7 @@ class UserFilter(FilterABC): def filter(self, query: List[User]) -> List[User]: if self._id is not None: - query = query.where(lambda x: x.id == self._id) + query = query.where(lambda x: x.user_id == self._id) if self._discord_id is not None: query = query.where(lambda x: x.discord_id == self._discord_id) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 14c7a841..77b98a86 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -64,15 +64,15 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserJoinedGameServerQuery) # filters - services.add_singleton(FilterABC, AutoRoleFilter) - services.add_singleton(FilterABC, AutoRoleRuleFilter) - services.add_singleton(FilterABC, ClientFilter) - services.add_singleton(FilterABC, LevelFilter) - services.add_singleton(FilterABC, ServerFilter) - services.add_singleton(FilterABC, UserFilter) - services.add_singleton(FilterABC, UserJoinedServerFilter) - services.add_singleton(FilterABC, UserJoinedVoiceChannelFilter) - services.add_singleton(FilterABC, UserJoinedGameServerFilter) + services.add_transient(FilterABC, AutoRoleFilter) + services.add_transient(FilterABC, AutoRoleRuleFilter) + services.add_transient(FilterABC, ClientFilter) + services.add_transient(FilterABC, LevelFilter) + services.add_transient(FilterABC, ServerFilter) + services.add_transient(FilterABC, UserFilter) + services.add_transient(FilterABC, UserJoinedServerFilter) + services.add_transient(FilterABC, UserJoinedVoiceChannelFilter) + services.add_transient(FilterABC, UserJoinedGameServerFilter) # mutations services.add_transient(QueryABC, AutoRoleMutation) -- 2.45.2 From b9087dccb3583997a671da48b1196fa2654ce091 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 16 Feb 2023 21:43:47 +0100 Subject: [PATCH 11/11] Fixed copy&paste error #181 --- kdb-bot/src/modules/database/database_on_ready_event.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/modules/database/database_on_ready_event.py b/kdb-bot/src/modules/database/database_on_ready_event.py index 72f35442..af59a3b0 100644 --- a/kdb-bot/src/modules/database/database_on_ready_event.py +++ b/kdb-bot/src/modules/database/database_on_ready_event.py @@ -331,7 +331,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) def _check_user_joined_gs(self): - self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table") + self._logger.debug(__name__, f"Start checking UserJoinedGameServer table") for guild in self._bot.guilds: guild: discord.Guild = guild @@ -370,7 +370,7 @@ class DatabaseOnReadyEvent(OnReadyABC): # todo: maybe add XP self._db_context.save_changes() except Exception as e: - self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) + self._logger.error(__name__, f"Cannot get UserJoinedGameServer", e) async def on_ready(self): self._logger.debug(__name__, f"Module {type(self)} started") -- 2.45.2