Fixed voice state update (ontime) handling

This commit is contained in:
Sven Heidemann 2023-01-19 20:02:56 +01:00
parent 53cdaf3fa0
commit e1c89814da
5 changed files with 70 additions and 28 deletions

View File

@ -14,25 +14,32 @@
"permission": "src/modules/permission/permission.json", "permission": "src/modules/permission/permission.json",
"stats": "src/modules/stats/stats.json", "stats": "src/modules/stats/stats.json",
"technician": "src/modules/technician/technician.json", "technician": "src/modules/technician/technician.json",
"checks": "tools/checks/checks.json",
"get-version": "tools/get_version/get-version.json", "get-version": "tools/get_version/get-version.json",
"post-build": "tools/post_build/post-build.json", "post-build": "tools/post_build/post-build.json",
"set-version": "tools/set_version/set-version.json", "set-version": "tools/set_version/set-version.json"
"tools/checks": "tools/tools/checks/tools/checks.json"
}, },
"Scripts": { "Scripts": {
"format": "black ./", "format": "black ./",
"sv": "cpl set-version $ARGS", "sv": "cpl set-version $ARGS",
"set-version": "cpl run set-version $ARGS --dev; echo '';", "set-version": "cpl run set-version $ARGS --dev; echo '';",
"gv": "cpl get-version", "gv": "cpl get-version",
"get-version": "export VERSION=$(cpl run get-version --dev); echo $VERSION;", "get-version": "export VERSION=$(cpl run get-version --dev); echo $VERSION;",
"pre-build": "cpl set-version $ARGS; black ./;", "pre-build": "cpl set-version $ARGS; black ./;",
"post-build": "cpl run post-build --dev; black ./;", "post-build": "cpl run post-build --dev; black ./;",
"pre-prod": "cpl build", "pre-prod": "cpl build",
"prod": "export KDB_ENVIRONMENT=production; export KDB_NAME=KDB-Prod; cpl start;", "prod": "export KDB_ENVIRONMENT=production; export KDB_NAME=KDB-Prod; cpl start;",
"pre-stage": "cpl build", "pre-stage": "cpl build",
"stage": "export KDB_ENVIRONMENT=staging; export KDB_NAME=KDB-Stage; cpl start;", "stage": "export KDB_ENVIRONMENT=staging; export KDB_NAME=KDB-Stage; cpl start;",
"pre-dev": "cpl build", "pre-dev": "cpl build",
"dev": "export KDB_ENVIRONMENT=development; export KDB_NAME=KDB-Dev; cpl start;", "dev": "export KDB_ENVIRONMENT=development; export KDB_NAME=KDB-Dev; cpl start;",
"docker-build": "cpl build $ARGS; docker build -t kdb-bot/kdb-bot:$(cpl gv) .;", "docker-build": "cpl build $ARGS; docker build -t kdb-bot/kdb-bot:$(cpl gv) .;",
"dc-up": "docker-compose up -d", "dc-up": "docker-compose up -d",
"dc-down": "docker-compose down", "dc-down": "docker-compose down",

View File

@ -16,7 +16,7 @@
"LicenseName": "MIT", "LicenseName": "MIT",
"LicenseDescription": "MIT, see LICENSE for more details.", "LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [ "Dependencies": [
"cpl-core==2022.12.1.post2", "cpl-core==2022.12.1.post3",
"cpl-translation==2022.12.1", "cpl-translation==2022.12.1",
"cpl-query==2022.12.2.post1", "cpl-query==2022.12.2.post1",
"cpl-discord==2022.12.1.post2", "cpl-discord==2022.12.1.post2",
@ -31,7 +31,7 @@
"icmplib==3.0.3" "icmplib==3.0.3"
], ],
"DevDependencies": [ "DevDependencies": [
"cpl-cli==2022.12.1.post2" "cpl-cli==2022.12.1.post3"
], ],
"PythonVersion": ">=3.10.4", "PythonVersion": ">=3.10.4",
"PythonPath": {}, "PythonPath": {},

View File

@ -48,7 +48,7 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
self._logger.info(__name__, f"Module {type(self)} loaded") self._logger.info(__name__, f"Module {type(self)} loaded")
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server, switch=False): def _switch_voice_state(self, dc_user_id: int, before_dc_channel_id: int, after_dc_channel_id: int, server: Server):
user: Optional[User] = None user: Optional[User] = None
try: try:
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id) user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
@ -62,16 +62,50 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
try: try:
end_date = datetime.now() end_date = datetime.now()
if joined and not switch: join = UserJoinedVoiceChannel(user, before_dc_channel_id, end_date)
self._user_joins_vc.add_user_joined_voice_channel(join)
settings: BaseServerSettings = self._base_helper.get_config(server.discord_server_id)
join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id)
join.leaved_on = end_date
# ontime as hours
ontime = round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)
old_xp = user.xp
user.xp += round(ontime * settings.xp_per_ontime_hour)
self._user_joins_vc.update_user_joined_voice_channel(join)
self._users.update_user(user)
self._db.save_changes()
self._logger.debug(
__name__,
f"User {user} leaved_on {join.leaved_on}. Ontime: {ontime}h | xp: from {old_xp} to {user.xp}",
)
except Exception as e:
self._logger.error(__name__, f"Ontime validation failed", e)
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server):
user: Optional[User] = None
try:
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
except Exception as e:
self._logger.error(__name__, f"Cannot get user {dc_user_id}", e)
return
if user is None:
self._logger.error(__name__, f"User not found {dc_user_id}")
return
try:
end_date = datetime.now()
if joined:
join = UserJoinedVoiceChannel(user, dc_channel_id, end_date) join = UserJoinedVoiceChannel(user, dc_channel_id, end_date)
self._user_joins_vc.add_user_joined_voice_channel(join) self._user_joins_vc.add_user_joined_voice_channel(join)
self._db.save_changes() self._db.save_changes()
return return
elif joined and switch:
join = UserJoinedVoiceChannel(user, dc_channel_id, end_date)
self._user_joins_vc.add_user_joined_voice_channel(join)
settings: BaseServerSettings = self._base_helper.get_config(server.discord_server_id) settings: BaseServerSettings = self._base_helper.get_config(server.discord_server_id)
join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id) join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id)
@ -139,8 +173,11 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
self._logger.trace(__name__, f"User {member.id} left {before.channel}") self._logger.trace(__name__, f"User {member.id} left {before.channel}")
self._update_voice_state(False, member.id, before.channel.id, server) self._update_voice_state(False, member.id, before.channel.id, server)
else: elif (
before.channel.id not in settings.afk_channel_ids
and after.channel.id not in settings.afk_channel_ids
):
self._logger.trace(__name__, f"User {member.id} switched to {after.channel}") self._logger.trace(__name__, f"User {member.id} switched to {after.channel}")
self._update_voice_state(True, member.id, before.channel.id, server, True) self._switch_voice_state(member.id, before.channel.id, after.channel.id, server)
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot handle voice state for user {member.id}", e) self._logger.error(__name__, f"Cannot handle voice state for user {member.id}", e)

View File

@ -271,6 +271,7 @@ class DatabaseOnReadyEvent(OnReadyABC):
self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table") self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table")
for guild in self._bot.guilds: for guild in self._bot.guilds:
guild: discord.Guild = guild guild: discord.Guild = guild
settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}")
server = self._servers.find_server_by_discord_id(guild.id) server = self._servers.find_server_by_discord_id(guild.id)
if server is None: if server is None:
@ -296,7 +297,6 @@ class DatabaseOnReadyEvent(OnReadyABC):
f"Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}", f"Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}",
) )
join.leaved_on = datetime.now() join.leaved_on = datetime.now()
settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}")
if ( if (
(join.leaved_on - join.joined_on).total_seconds() / 60 / 60 (join.leaved_on - join.joined_on).total_seconds() / 60 / 60
@ -312,7 +312,7 @@ class DatabaseOnReadyEvent(OnReadyABC):
self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot") self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot")
continue continue
if member.voice is None: if member.voice is None or member.voice.channel.id in settings.afk_channel_ids:
continue continue
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)

View File

@ -6,25 +6,23 @@
"Minor": "0", "Minor": "0",
"Micro": "0" "Micro": "0"
}, },
"Author": "", "Author": "Sven Heidemann",
"AuthorEmail": "", "AuthorEmail": "sven.heidemann@sh-edraft.de",
"Description": "", "Description": "CPL internal tool to set version from branch name",
"LongDescription": "", "LongDescription": "CPL internal tool to set version from branch name",
"URL": "", "URL": "https://www.sh-edraft.de",
"CopyrightDate": "", "CopyrightDate": "2022",
"CopyrightName": "", "CopyrightName": "sh-edraft.de",
"LicenseName": "", "LicenseName": "MIT",
"LicenseDescription": "", "LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [ "Dependencies": [
"cpl-core>=2022.12.1.post3" "cpl-core==2022.12.0"
], ],
"DevDependencies": [ "DevDependencies": [
"cpl-cli>=2022.12.1.post3" "cpl-cli==2022.12.0"
], ],
"PythonVersion": ">=3.10.4", "PythonVersion": ">=3.10.4",
"PythonPath": { "PythonPath": {},
"linux": ""
},
"Classifiers": [] "Classifiers": []
}, },
"BuildSettings": { "BuildSettings": {