From f1f5954dfc868d74df6573a8240d8678cfb9b0b3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 15 Dec 2023 13:42:49 +0100 Subject: [PATCH] Added reset member after rejoin setting to backend [untested] #460 --- bot/src/bot_data/model/server_config.py | 11 ++ bot/src/bot_data/model/user.py | 5 + .../1.2.4/1_ResetMemberAfterRejoin_down.sql | 7 ++ .../1.2.4/1_ResetMemberAfterRejoin_up.sql | 116 ++++++++++++++++++ .../base/events/base_on_member_join_event.py | 6 + 5 files changed, 145 insertions(+) create mode 100644 bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_down.sql create mode 100644 bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_up.sql diff --git a/bot/src/bot_data/model/server_config.py b/bot/src/bot_data/model/server_config.py index cf220355..d568f953 100644 --- a/bot/src/bot_data/model/server_config.py +++ b/bot/src/bot_data/model/server_config.py @@ -32,6 +32,7 @@ class ServerConfig(TableABC, ConfigurationModelABC): default_role_id: Optional[int], short_role_name_only_set_highest_role: bool, game_offer_notification_chat_id: int, + reset_member_after_rejoin: bool, feature_flags: dict[FeatureFlagsEnum], server: Server, afk_channel_ids: List[int], @@ -58,6 +59,7 @@ class ServerConfig(TableABC, ConfigurationModelABC): self._default_role_id = default_role_id self._short_role_name_only_set_highest_role = short_role_name_only_set_highest_role self._game_offer_notification_chat_id = game_offer_notification_chat_id + self._reset_member_after_rejoin = reset_member_after_rejoin self._feature_flags = feature_flags self._server = server @@ -88,6 +90,7 @@ class ServerConfig(TableABC, ConfigurationModelABC): None, False, guild.system_channel.id, + False, {}, server, List(int), @@ -234,6 +237,14 @@ class ServerConfig(TableABC, ConfigurationModelABC): def game_offer_notification_chat_id(self, value: int): self._game_offer_notification_chat_id = value + @property + def reset_member_after_rejoin(self) -> bool: + return self._reset_member_after_rejoin + + @reset_member_after_rejoin.setter + def reset_member_after_rejoin(self, value: bool): + self._reset_member_after_rejoin = value + @property def feature_flags(self) -> dict[FeatureFlagsEnum]: return self._feature_flags diff --git a/bot/src/bot_data/model/user.py b/bot/src/bot_data/model/user.py index bfc7be34..697511a2 100644 --- a/bot/src/bot_data/model/user.py +++ b/bot/src/bot_data/model/user.py @@ -38,6 +38,11 @@ class User(TableABC): 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 + def reset(self): + self._xp = 0 + self._message_count = 0 + self._reaction_count = 0 + @property def id(self) -> int: return self._user_id diff --git a/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_down.sql b/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_down.sql new file mode 100644 index 00000000..5b584865 --- /dev/null +++ b/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_down.sql @@ -0,0 +1,7 @@ +ALTER TABLE CFG_Server + DROP COLUMN ResetMemberAfterRejoin; + +ALTER TABLE CFG_ServerHistory + DROP COLUMN ResetMemberAfterRejoin; + + diff --git a/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_up.sql b/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_up.sql new file mode 100644 index 00000000..4595e89f --- /dev/null +++ b/bot/src/bot_data/scripts/1.2.4/1_ResetMemberAfterRejoin_up.sql @@ -0,0 +1,116 @@ +ALTER TABLE CFG_Server + ADD ResetMemberAfterRejoin BOOLEAN NOT NULL DEFAULT FALSE AFTER GameOfferNotificationChatId; + + + +ALTER TABLE CFG_ServerHistory + ADD ResetMemberAfterRejoin BOOLEAN NOT NULL DEFAULT FALSE AFTER GameOfferNotificationChatId; + +DROP TRIGGER IF EXISTS `TR_CFG_ServerUpdate`;; + +CREATE TRIGGER `TR_CFG_ServerUpdate` + AFTER UPDATE + ON `CFG_Server` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerHistory` (`Id`, + `MessageDeleteTimer`, + `NotificationChatId`, + `MaxVoiceStateHours`, + `XpPerMessage`, + `XpPerReaction`, + `MaxMessageXpPerHour`, + `XpPerOntimeHour`, + `XpPerEventParticipation`, + `XpPerAchievement`, + `AFKCommandChannelId`, + `HelpVoiceChannelId`, + `TeamChannelId`, + `LoginMessageChannelId`, + `DefaultRoleId`, + `ShortRoleNameSetOnlyHighest`, + `GameOfferNotificationChatId`, + `ResetMemberAfterRejoin`, + `FeatureFlags`, + `ServerId`, + `DateFrom`, + `DateTo`) + VALUES (OLD.Id, + OLD.MessageDeleteTimer, + OLD.NotificationChatId, + OLD.MaxVoiceStateHours, + OLD.XpPerMessage, + OLD.XpPerReaction, + OLD.MaxMessageXpPerHour, + OLD.XpPerOntimeHour, + OLD.XpPerEventParticipation, + OLD.XpPerAchievement, + OLD.AFKCommandChannelId, + OLD.HelpVoiceChannelId, + OLD.TeamChannelId, + OLD.LoginMessageChannelId, + OLD.DefaultRoleId, + OLD.ShortRoleNameSetOnlyHighest, + OLD.GameOfferNotificationChatId, + OLD.ResetMemberAfterRejoin, + OLD.FeatureFlags, + OLD.ServerId, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6)); +END;; + +DROP TRIGGER IF EXISTS `TR_CFG_ServerDelete`;; + +CREATE TRIGGER `TR_CFG_ServerDelete` + AFTER DELETE + ON `CFG_Server` + FOR EACH ROW +BEGIN + INSERT INTO `CFG_ServerHistory` (`Id`, + `MessageDeleteTimer`, + `NotificationChatId`, + `MaxVoiceStateHours`, + `XpPerMessage`, + `XpPerReaction`, + `MaxMessageXpPerHour`, + `XpPerOntimeHour`, + `XpPerEventParticipation`, + `XpPerAchievement`, + `AFKCommandChannelId`, + `HelpVoiceChannelId`, + `TeamChannelId`, + `LoginMessageChannelId`, + `DefaultRoleId`, + `ShortRoleNameSetOnlyHighest`, + `GameOfferNotificationChatId`, + `ResetMemberAfterRejoin`, + `ServerId`, + `FeatureFlags`, + `Deleted`, + `DateFrom`, + `DateTo`) + VALUES (OLD.Id, + OLD.MessageDeleteTimer, + OLD.NotificationChatId, + OLD.MaxVoiceStateHours, + OLD.XpPerMessage, + OLD.XpPerReaction, + OLD.MaxMessageXpPerHour, + OLD.XpPerOntimeHour, + OLD.XpPerEventParticipation, + OLD.XpPerAchievement, + OLD.AFKCommandChannelId, + OLD.HelpVoiceChannelId, + OLD.TeamChannelId, + OLD.LoginMessageChannelId, + OLD.DefaultRoleId, + OLD.ShortRoleNameSetOnlyHighest, + OLD.GameOfferNotificationChatId, + OLD.ResetMemberAfterRejoin, + OLD.FeatureFlags, + OLD.ServerId, + TRUE, + OLD.LastModifiedAt, + CURRENT_TIMESTAMP(6)); +END;; + diff --git a/bot/src/modules/base/events/base_on_member_join_event.py b/bot/src/modules/base/events/base_on_member_join_event.py index 4cd4f2e3..ef7da1a3 100644 --- a/bot/src/modules/base/events/base_on_member_join_event.py +++ b/bot/src/modules/base/events/base_on_member_join_event.py @@ -78,6 +78,12 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC): user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id) if user is not None: self._user_joins.add_user_joined_server(UserJoinedServer(user, datetime.now())) + + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}") + if server_config.reset_member_after_rejoin: + user.reset() + self._users.update_user(user) + self._db.save_changes() return