Added birthday to wi #401
This commit is contained in:
@@ -9,11 +9,13 @@ from bot_data.migration.api_key_migration import ApiKeyMigration
|
||||
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.birthday_migration import BirthdayMigration
|
||||
from bot_data.migration.config_feature_flags_migration import ConfigFeatureFlagsMigration
|
||||
from bot_data.migration.config_migration import ConfigMigration
|
||||
from bot_data.migration.db_history_migration import DBHistoryMigration
|
||||
from bot_data.migration.default_role_migration import DefaultRoleMigration
|
||||
from bot_data.migration.fix_updates_migration import FixUpdatesMigration
|
||||
from bot_data.migration.fix_user_history_migration import FixUserHistoryMigration
|
||||
from bot_data.migration.initial_migration import InitialMigration
|
||||
from bot_data.migration.level_migration import LevelMigration
|
||||
from bot_data.migration.remove_stats_migration import RemoveStatsMigration
|
||||
@@ -56,3 +58,5 @@ class StartupMigrationExtension(StartupExtensionABC):
|
||||
services.add_transient(MigrationABC, ShortRoleNameMigration) # 28.09.2023 #378 - 1.1.7
|
||||
services.add_transient(MigrationABC, FixUpdatesMigration) # 28.09.2023 #378 - 1.1.7
|
||||
services.add_transient(MigrationABC, ShortRoleNameOnlyHighestMigration) # 02.10.2023 #391 - 1.1.9
|
||||
services.add_transient(MigrationABC, FixUserHistoryMigration) # 10.10.2023 #401 - 1.2.0
|
||||
services.add_transient(MigrationABC, BirthdayMigration) # 10.10.2023 #401 - 1.2.0
|
||||
|
84
kdb-bot/src/bot_data/migration/birthday_migration.py
Normal file
84
kdb-bot/src/bot_data/migration/birthday_migration.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from bot_core.logging.database_logger import DatabaseLogger
|
||||
from bot_data.abc.migration_abc import MigrationABC
|
||||
from bot_data.db_context import DBContext
|
||||
|
||||
|
||||
class BirthdayMigration(MigrationABC):
|
||||
name = "1.2.0_BirthdayMigration"
|
||||
|
||||
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 Users
|
||||
ADD Birthday DATE NULL AFTER MessageCount;
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE UsersHistory
|
||||
ADD Birthday DATE NULL AFTER MessageCount;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._exec(__file__, "users.sql")
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_Server
|
||||
ADD XpForBirthday BIGINT(20) NOT NULL DEFAULT 0 AFTER XpPerAchievement;
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_ServerHistory
|
||||
ADD XpForBirthday BIGINT(20) NOT NULL DEFAULT 0 AFTER XpPerAchievement;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._exec(__file__, "config/server.sql")
|
||||
|
||||
def downgrade(self):
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE Users DROP COLUMN Birthday;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE UsersHistory DROP COLUMN Birthday;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_Server DROP COLUMN XpForBirthday;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_ServerHistory DROP COLUMN XpForBirthday;
|
||||
"""
|
||||
)
|
||||
)
|
@@ -6,13 +6,16 @@ ALTER TABLE `Users`
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `UsersHistory`
|
||||
(
|
||||
`Id` BIGINT(20) NOT NULL,
|
||||
`DiscordId` BIGINT(20) NOT NULL,
|
||||
`XP` BIGINT(20) NOT NULL DEFAULT 0,
|
||||
`ServerId` BIGINT(20) DEFAULT NULL,
|
||||
`Deleted` BOOL DEFAULT FALSE,
|
||||
`DateFrom` DATETIME(6) NOT NULL,
|
||||
`DateTo` DATETIME(6) NOT NULL
|
||||
`Id` BIGINT(20) NOT NULL,
|
||||
`DiscordId` BIGINT(20) NOT NULL,
|
||||
`XP` BIGINT(20) NOT NULL DEFAULT 0,
|
||||
`ReactionCount` BIGINT(20) NOT NULL DEFAULT 0,
|
||||
`MessageCount` BIGINT(20) NOT NULL DEFAULT 0,
|
||||
`Birthday` DATE NULL,
|
||||
`ServerId` BIGINT(20) DEFAULT NULL,
|
||||
`Deleted` BOOL DEFAULT FALSE,
|
||||
`DateFrom` DATETIME(6) NOT NULL,
|
||||
`DateTo` DATETIME(6) NOT NULL
|
||||
);
|
||||
|
||||
DROP TRIGGER IF EXISTS `TR_UsersUpdate`;
|
||||
@@ -22,12 +25,10 @@ CREATE TRIGGER `TR_UsersUpdate`
|
||||
ON `Users`
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
INSERT INTO `UsersHistory` (
|
||||
`Id`, `DiscordId`, `XP`, `ServerId`, `DateFrom`, `DateTo`
|
||||
)
|
||||
VALUES (
|
||||
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
|
||||
);
|
||||
INSERT INTO `UsersHistory` (`Id`, `DiscordId`, `XP`, `ReactionCount`, `MessageCount`, `Birthday`, `ServerId`,
|
||||
`DateFrom`, `DateTo`)
|
||||
VALUES (OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ReactionCount, OLD.MessageCount, OLD.Birthday, OLD.ServerId,
|
||||
OLD.LastModifiedAt, CURRENT_TIMESTAMP(6));
|
||||
END;
|
||||
|
||||
DROP TRIGGER IF EXISTS `TR_UsersDelete`;
|
||||
@@ -37,10 +38,8 @@ CREATE TRIGGER `TR_UsersDelete`
|
||||
ON `Users`
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
INSERT INTO `UsersHistory` (
|
||||
`Id`, `DiscordId`, `XP`, `ServerId`, `Deleted`, `DateFrom`, `DateTo`
|
||||
)
|
||||
VALUES (
|
||||
OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ServerId, TRUE, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)
|
||||
);
|
||||
INSERT INTO `UsersHistory` (`Id`, `DiscordId`, `XP`, `ReactionCount`, `MessageCount`, `Birthday`, `ServerId`,
|
||||
`Deleted`, `DateFrom`, `DateTo`)
|
||||
VALUES (OLD.UserId, OLD.DiscordId, OLD.XP, OLD.ReactionCount, OLD.MessageCount, OLD.Birthday, OLD.ServerId, TRUE,
|
||||
OLD.LastModifiedAt, CURRENT_TIMESTAMP(6));
|
||||
END;
|
45
kdb-bot/src/bot_data/migration/fix_user_history_migration.py
Normal file
45
kdb-bot/src/bot_data/migration/fix_user_history_migration.py
Normal file
@@ -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 FixUserHistoryMigration(MigrationABC):
|
||||
name = "1.2.0_FixUserHistoryMigration"
|
||||
|
||||
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")
|
||||
|
||||
# fix 1.1.0_AchievementsMigration
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""ALTER TABLE UsersHistory ADD COLUMN IF NOT EXISTS ReactionCount BIGINT NOT NULL DEFAULT 0 AFTER XP;"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""ALTER TABLE UsersHistory ADD COLUMN IF NOT EXISTS MessageCount BIGINT NOT NULL DEFAULT 0 AFTER ReactionCount;"""
|
||||
)
|
||||
)
|
||||
self._exec(__file__, "users.sql")
|
||||
|
||||
def downgrade(self):
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE UsersHistory DROP COLUMN MessageCount;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE UsersHistory DROP COLUMN ReactionCount;
|
||||
"""
|
||||
)
|
||||
)
|
@@ -24,6 +24,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
xp_per_ontime_hour: int,
|
||||
xp_per_event_participation: int,
|
||||
xp_per_achievement: int,
|
||||
xp_for_birthday: int,
|
||||
afk_command_channel_id: int,
|
||||
help_voice_channel_id: int,
|
||||
team_channel_id: int,
|
||||
@@ -48,6 +49,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
self._xp_per_ontime_hour = xp_per_ontime_hour
|
||||
self._xp_per_event_participation = xp_per_event_participation
|
||||
self._xp_per_achievement = xp_per_achievement
|
||||
self._xp_for_birthday = xp_for_birthday
|
||||
self._afk_command_channel_id = afk_command_channel_id
|
||||
self._help_voice_channel_id = help_voice_channel_id
|
||||
self._team_channel_id = team_channel_id
|
||||
@@ -76,6 +78,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
10,
|
||||
guild.system_channel.id,
|
||||
guild.system_channel.id,
|
||||
guild.system_channel.id,
|
||||
@@ -164,6 +167,14 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
def xp_per_achievement(self, value: int):
|
||||
self._xp_per_achievement = value
|
||||
|
||||
@property
|
||||
def xp_for_birthday(self) -> int:
|
||||
return self._xp_for_birthday
|
||||
|
||||
@xp_for_birthday.setter
|
||||
def xp_for_birthday(self, value: int):
|
||||
self._xp_for_birthday = value
|
||||
|
||||
@property
|
||||
def afk_command_channel_id(self) -> int:
|
||||
return self._afk_command_channel_id
|
||||
@@ -280,6 +291,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
`XpPerOntimeHour`,
|
||||
`XpPerEventParticipation`,
|
||||
`XpPerAchievement`,
|
||||
`XpForBirthday`,
|
||||
`AFKCommandChannelId`,
|
||||
`HelpVoiceChannelId`,
|
||||
`TeamChannelId`,
|
||||
@@ -298,6 +310,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
{self._xp_per_ontime_hour},
|
||||
{self._xp_per_event_participation},
|
||||
{self._xp_per_achievement},
|
||||
'{self._xp_for_birthday}',
|
||||
{self._afk_command_channel_id},
|
||||
{self._help_voice_channel_id},
|
||||
{self._team_channel_id},
|
||||
@@ -324,6 +337,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
`XpPerOntimeHour` = {self._xp_per_ontime_hour},
|
||||
`XpPerEventParticipation` = {self._xp_per_event_participation},
|
||||
`XpPerAchievement` = {self._xp_per_achievement},
|
||||
`XpForBirthday` = {self._xp_for_birthday},
|
||||
`AFKCommandChannelId` = {self._afk_command_channel_id},
|
||||
`HelpVoiceChannelId` = {self._help_voice_channel_id},
|
||||
`TeamChannelId` = {self._team_channel_id},
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, date
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.database import TableABC
|
||||
@@ -17,6 +17,7 @@ class User(TableABC):
|
||||
xp: int,
|
||||
reaction_count: int,
|
||||
message_count: int,
|
||||
birthday: Optional[date],
|
||||
server: Optional[Server],
|
||||
created_at: datetime = None,
|
||||
modified_at: datetime = None,
|
||||
@@ -27,6 +28,7 @@ class User(TableABC):
|
||||
self._xp = xp
|
||||
self._reaction_count = reaction_count
|
||||
self._message_count = message_count
|
||||
self._birthday = birthday
|
||||
self._server = server
|
||||
|
||||
TableABC.__init__(self)
|
||||
@@ -79,6 +81,14 @@ class User(TableABC):
|
||||
def reaction_count(self, value: int):
|
||||
self._reaction_count = value
|
||||
|
||||
@property
|
||||
def birthday(self) -> Optional[datetime]:
|
||||
return self._birthday
|
||||
|
||||
@birthday.setter
|
||||
def birthday(self, value: Optional[datetime]):
|
||||
self._birthday = value
|
||||
|
||||
@property
|
||||
@ServiceProviderABC.inject
|
||||
def ontime(self, services: ServiceProviderABC) -> float:
|
||||
@@ -171,12 +181,13 @@ class User(TableABC):
|
||||
return str(
|
||||
f"""
|
||||
INSERT INTO `Users` (
|
||||
`DiscordId`, `XP`, `MessageCount`, `ReactionCount`, `ServerId`
|
||||
`DiscordId`, `XP`, `MessageCount`, `ReactionCount`, `Birthday`, `ServerId`
|
||||
) VALUES (
|
||||
{self._discord_id},
|
||||
{self._xp},
|
||||
{self._message_count},
|
||||
{self._reaction_count},
|
||||
'{self._birthday}',
|
||||
{self._server.id}
|
||||
);
|
||||
"""
|
||||
@@ -189,7 +200,8 @@ class User(TableABC):
|
||||
UPDATE `Users`
|
||||
SET `XP` = {self._xp},
|
||||
`MessageCount` = {self._message_count},
|
||||
`ReactionCount` = {self._reaction_count}
|
||||
`ReactionCount` = {self._reaction_count},
|
||||
`Birthday` = '{self._birthday}'
|
||||
WHERE `UserId` = {self._user_id};
|
||||
"""
|
||||
)
|
||||
|
@@ -66,12 +66,13 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC):
|
||||
result[13],
|
||||
result[14],
|
||||
result[15],
|
||||
json.loads(result[16]),
|
||||
self._servers.get_server_by_id(result[17]),
|
||||
self._get_afk_channel_ids(result[17]),
|
||||
self._get_team_role_ids(result[17]),
|
||||
result[18],
|
||||
result[16],
|
||||
json.loads(result[17]),
|
||||
self._servers.get_server_by_id(result[18]),
|
||||
self._get_afk_channel_ids(result[18]),
|
||||
self._get_team_role_ids(result[18]),
|
||||
result[19],
|
||||
result[20],
|
||||
id=result[0],
|
||||
)
|
||||
|
||||
|
@@ -29,9 +29,10 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
result[2],
|
||||
result[3],
|
||||
result[4],
|
||||
self._servers.get_server_by_id(result[5]),
|
||||
result[6],
|
||||
result[5].strftime("%d.%m.%Y") if result[5] is not None else None,
|
||||
self._servers.get_server_by_id(result[6]),
|
||||
result[7],
|
||||
result[8],
|
||||
id=result[0],
|
||||
)
|
||||
|
||||
|
@@ -9,6 +9,7 @@ type ServerConfig implements TableWithHistoryQuery {
|
||||
xpPerOntimeHour: Int
|
||||
xpPerEventParticipation: Int
|
||||
xpPerAchievement: Int
|
||||
xpForBirthday: Int
|
||||
afkCommandChannelId: String
|
||||
helpVoiceChannelId: String
|
||||
teamChannelId: String
|
||||
@@ -41,6 +42,7 @@ type ServerConfigHistory implements HistoryTableQuery {
|
||||
xpPerOntimeHour: Int
|
||||
xpPerEventParticipation: Int
|
||||
xpPerAchievement: Int
|
||||
xpForBirthday: Int
|
||||
afkCommandChannelId: String
|
||||
helpVoiceChannelId: String
|
||||
teamChannelId: String
|
||||
@@ -91,6 +93,7 @@ input ServerConfigInput {
|
||||
xpPerOntimeHour: Int
|
||||
xpPerEventParticipation: Int
|
||||
xpPerAchievement: Int
|
||||
xpForBirthday: Int
|
||||
afkCommandChannelId: String
|
||||
helpVoiceChannelId: String
|
||||
teamChannelId: String
|
||||
|
@@ -5,6 +5,7 @@ type User implements TableWithHistoryQuery {
|
||||
xp: Int
|
||||
messageCount: Int
|
||||
reactionCount: Int
|
||||
birthday: String
|
||||
ontime: Float
|
||||
level: Level
|
||||
|
||||
@@ -62,6 +63,7 @@ type UserMutation {
|
||||
input UserInput {
|
||||
id: ID
|
||||
xp: Int
|
||||
birthday: String
|
||||
levelId: ID
|
||||
userWarnings: [UserWarningInput]
|
||||
}
|
@@ -1,6 +1,9 @@
|
||||
from datetime import datetime
|
||||
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
|
||||
from bot_api.route.route import Route
|
||||
from bot_data.abc.level_repository_abc import LevelRepositoryABC
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
||||
@@ -42,18 +45,26 @@ class UserMutation(QueryABC):
|
||||
|
||||
def resolve_update_user(self, *_, input: dict):
|
||||
user = self._users.get_user_by_id(input["id"])
|
||||
self._can_user_mutate_data(user.server, UserRoleEnum.moderator)
|
||||
|
||||
new_xp = None
|
||||
if "levelId" in input:
|
||||
level = self._levels.get_level_by_id(input["levelId"])
|
||||
if user.level.id != level.id:
|
||||
new_xp = level.min_xp
|
||||
auth_user = Route.get_user()
|
||||
member = self._bot.get_guild(user.server.discord_id).get_member(
|
||||
auth_user.users.where(lambda x: x.server.id == user.server.id).single().discord_id
|
||||
)
|
||||
if member.id != user.discord_id:
|
||||
self._can_user_mutate_data(user.server, UserRoleEnum.moderator)
|
||||
|
||||
user.xp = new_xp if new_xp is not None else input["xp"] if "xp" in input else user.xp
|
||||
new_xp = None
|
||||
if "levelId" in input:
|
||||
level = self._levels.get_level_by_id(input["levelId"])
|
||||
if user.level.id != level.id:
|
||||
new_xp = level.min_xp
|
||||
|
||||
if "userWarnings" in input:
|
||||
self._update_user_warning(user, input["userWarnings"])
|
||||
if "userWarnings" in input:
|
||||
self._update_user_warning(user, input["userWarnings"])
|
||||
|
||||
user.xp = new_xp if new_xp is not None else input["xp"] if "xp" in input else user.xp
|
||||
|
||||
user.birthday = datetime.strptime(input["birthday"], "%d.%m.%Y") if "birthday" in input else user.birthday
|
||||
|
||||
self._users.update_user(user)
|
||||
self._db.save_changes()
|
||||
|
@@ -20,6 +20,7 @@ class ServerConfigQuery(DataQueryWithHistoryABC):
|
||||
self.set_field("xpPerOntimeHour", lambda config, *_: config.xp_per_ontime_hour)
|
||||
self.set_field("xpPerEventParticipation", lambda config, *_: config.xp_per_event_participation)
|
||||
self.set_field("xpPerAchievement", lambda config, *_: config.xp_per_achievement)
|
||||
self.set_field("xpForBirthday", lambda config, *_: config.xp_for_birthday)
|
||||
self.set_field("afkCommandChannelId", lambda config, *_: config.afk_command_channel_id)
|
||||
self.set_field("helpVoiceChannelId", lambda config, *_: config.help_voice_channel_id)
|
||||
self.set_field("teamChannelId", lambda config, *_: config.team_channel_id)
|
||||
|
@@ -50,6 +50,7 @@ class UserQuery(DataQueryWithHistoryABC):
|
||||
self.set_field("xp", self.resolve_xp)
|
||||
self.set_field("messageCount", lambda x, *_: x.message_count)
|
||||
self.set_field("reactionCount", lambda x, *_: x.reaction_count)
|
||||
self.set_field("birthday", lambda x, *_: x.birthday)
|
||||
self.set_field("ontime", self.resolve_ontime)
|
||||
self.set_field("level", self.resolve_level)
|
||||
self.add_collection(
|
||||
|
Reference in New Issue
Block a user