Fixed loop and model handling
This commit is contained in:
parent
22b0c3ca6a
commit
d5de1991eb
@ -1,13 +1,17 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
from gismo_data.model.server import Server
|
||||
|
||||
|
||||
class User(TableABC):
|
||||
|
||||
def __init__(self, dc_id: int, xp: int, created_at: datetime = None, modified_at: datetime = None, id=0):
|
||||
def __init__(self, dc_id: int, xp: int, server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, id=0):
|
||||
self._user_id = id
|
||||
self._discord_id = dc_id
|
||||
self._xp = xp
|
||||
self._server = server
|
||||
|
||||
TableABC.__init__(self)
|
||||
self._created_at = created_at if created_at is not None else self._created_at
|
||||
@ -30,6 +34,10 @@ class User(TableABC):
|
||||
self._modified_at = datetime.now().isoformat()
|
||||
self._xp = value
|
||||
|
||||
@property
|
||||
def server(self) -> Optional[Server]:
|
||||
return self._server
|
||||
|
||||
@staticmethod
|
||||
def get_create_string() -> str:
|
||||
return str(f"""
|
||||
@ -37,8 +45,10 @@ class User(TableABC):
|
||||
`UserId` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`DiscordId` BIGINT NOT NULL,
|
||||
`XP` BIGINT NOT NULL DEFAULT 0,
|
||||
`ServerId` BIGINT,
|
||||
`CreatedAt` DATETIME(6),
|
||||
`LastModifiedAt` DATETIME(6),
|
||||
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
|
||||
PRIMARY KEY(`UserId`)
|
||||
);
|
||||
""")
|
||||
@ -67,10 +77,11 @@ class User(TableABC):
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
INSERT INTO `Users` (
|
||||
`DiscordId`, `XP`, `CreatedAt`, `LastModifiedAt`
|
||||
`DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt`
|
||||
) VALUES (
|
||||
{self._discord_id},
|
||||
{self._xp},
|
||||
{self._server.server_id},
|
||||
'{self._created_at}',
|
||||
'{self._modified_at}'
|
||||
);
|
||||
|
@ -9,10 +9,9 @@ from gismo_data.model.server import Server
|
||||
|
||||
class UserJoinedServer(TableABC):
|
||||
|
||||
def __init__(self, user: User, server: Server, joined_on: datetime, leaved_on: datetime=None, created_at: datetime=None, modified_at: datetime=None, id=0):
|
||||
def __init__(self, user: User, joined_on: datetime, leaved_on: datetime=None, created_at: datetime=None, modified_at: datetime=None, id=0):
|
||||
self._join_id = id
|
||||
self._user = user
|
||||
self._server = server
|
||||
self._joined_on = joined_on
|
||||
self._leaved_on = leaved_on
|
||||
|
||||
@ -28,10 +27,6 @@ class UserJoinedServer(TableABC):
|
||||
def user(self) -> User:
|
||||
return self._user
|
||||
|
||||
@property
|
||||
def server(self) -> Server:
|
||||
return self._server
|
||||
|
||||
@property
|
||||
def joined_on(self) -> datetime:
|
||||
return self._joined_on
|
||||
@ -46,14 +41,12 @@ class UserJoinedServer(TableABC):
|
||||
CREATE TABLE IF NOT EXISTS `UserJoinedServers` (
|
||||
`JoinId` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`UserId` BIGINT NOT NULL,
|
||||
`ServerId` BIGINT NOT NULL,
|
||||
`JoinedOn` DATETIME(6) NOT NULL,
|
||||
`LeavedOn` DATETIME(6),
|
||||
`CreatedAt` DATETIME(6),
|
||||
`LastModifiedAt` DATETIME(6),
|
||||
FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`),
|
||||
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
|
||||
PRIMARY KEY(`ServerId`)
|
||||
PRIMARY KEY(`JoinId`)
|
||||
);
|
||||
""")
|
||||
|
||||
@ -89,24 +82,25 @@ class UserJoinedServer(TableABC):
|
||||
def get_select_by_server_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `UserJoinedServers`
|
||||
WHERE `ServerId` = {id};
|
||||
JOIN `Users` On `UserJoinedServers`.`UserId` = `Users`.`UserId`
|
||||
WHERE `Users`.`ServerId` = {id};
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_active_by_server_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `UserJoinedServers`
|
||||
WHERE `ServerId` = {id}
|
||||
AND `LeavedOn` IS NULL;
|
||||
JOIN `Users` On `UserJoinedServers`.`UserId` = `Users`.`UserId`
|
||||
WHERE `Users`.`ServerId` = {id}
|
||||
AND `UserJoinedServers`.`LeavedOn` IS NULL;
|
||||
""")
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
INSERT INTO `UserJoinedServers` (
|
||||
`UserId`, `ServerId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt`
|
||||
`UserId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt`
|
||||
) VALUES (
|
||||
{self._user.user_id},
|
||||
{self._server.server_id},
|
||||
{self._joined_on},
|
||||
{self._leaved_on},
|
||||
'{self._created_at}',
|
||||
|
@ -64,17 +64,17 @@ class Database(ModuleABC, OnReadyABC):
|
||||
return
|
||||
|
||||
def _check_known_users(self):
|
||||
self._logger.debug(__name__, f'Start checking KnownUsers table')
|
||||
self._logger.debug(__name__, f'Start checking KnownUsers table, {len(self._bot.users)}')
|
||||
for u in self._bot.users:
|
||||
u: discord.User = u
|
||||
try:
|
||||
if u.bot:
|
||||
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
|
||||
break
|
||||
continue
|
||||
|
||||
user = self._known_users.find_user_by_discord_id(u.id)
|
||||
if user is not None:
|
||||
break
|
||||
continue
|
||||
|
||||
self._logger.warn(__name__, f'Unknown user: {u.id}')
|
||||
self._logger.debug(__name__, f'Add user: {u.id}')
|
||||
@ -89,10 +89,6 @@ class Database(ModuleABC, OnReadyABC):
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot get user', e)
|
||||
|
||||
results = self._servers.get_servers()
|
||||
if results is None or len(results) == 0:
|
||||
self._logger.error(__name__, f'Table Servers is empty!')
|
||||
|
||||
def _check_servers(self):
|
||||
self._logger.debug(__name__, f'Start checking Servers table')
|
||||
for g in self._bot.guilds:
|
||||
@ -100,7 +96,7 @@ class Database(ModuleABC, OnReadyABC):
|
||||
try:
|
||||
server = self._servers.find_server_by_discord_id(g.id)
|
||||
if server is not None:
|
||||
break
|
||||
continue
|
||||
|
||||
self._logger.warn(__name__, f'Server not found in database: {g.id}')
|
||||
self._logger.debug(__name__, f'Add server: {g.id}')
|
||||
@ -130,7 +126,7 @@ class Database(ModuleABC, OnReadyABC):
|
||||
|
||||
client = self._clients.find_client_by_server_id(server.server_id)
|
||||
if client is not None:
|
||||
break
|
||||
continue
|
||||
|
||||
self._logger.warn(__name__, f'Client for server {g.id} not found in database: {self._bot.user.id}')
|
||||
self._logger.debug(__name__, f'Add client: {self._bot.user.id}')
|
||||
@ -163,11 +159,11 @@ class Database(ModuleABC, OnReadyABC):
|
||||
u: discord.Member = u
|
||||
if u.bot:
|
||||
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
|
||||
break
|
||||
continue
|
||||
|
||||
user = self._users.find_user_by_discord_id(u.id)
|
||||
if user is not None:
|
||||
break
|
||||
continue
|
||||
|
||||
self._logger.warn(__name__, f'User not found in database: {u.id}')
|
||||
self._logger.debug(__name__, f'Add user: {u.id}')
|
||||
|
Reference in New Issue
Block a user