From f46208250d0351afcc063d50f501c58624d707e5 Mon Sep 17 00:00:00 2001 From: Hipstercat Date: Tue, 1 Sep 2020 16:12:08 +0200 Subject: [PATCH] update almost connected --- cogs/wynncraft/wynncraftcog.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/cogs/wynncraft/wynncraftcog.py b/cogs/wynncraft/wynncraftcog.py index 7d9311c..acdb6c5 100644 --- a/cogs/wynncraft/wynncraftcog.py +++ b/cogs/wynncraft/wynncraftcog.py @@ -7,7 +7,11 @@ from discord import TextChannel import discord import traceback import requests -import datetime +from datetime import datetime, timezone, timedelta + + +def utc_to_local(utc_dt): + return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None) class WynncraftCog(commands.Cog): @@ -160,6 +164,7 @@ class WynncraftCog(commands.Cog): roles_professions = await self.config.guild(guild).role_professions() guild_id = guild.id online_players = [] + almost_online_players = [] await self._log("Loop id %s" % guild_id) try: guild_name = await self.config.guild(guild).guild_name() @@ -200,7 +205,6 @@ class WynncraftCog(commands.Cog): # get max_combat_lvl_class if not max_combat_lvl_class or cl["professions"]["combat"]["level"] > max_combat_lvl_class["professions"]["combat"]["level"]: - # await self._log("cl: %s, max_combat_lvl_class: %s" % (cl, max_combat_lvl_class)) max_combat_lvl_class = cl # set max_lvl_professions @@ -212,7 +216,14 @@ class WynncraftCog(commands.Cog): await self._log("%s is online" % member_name) online_players.append(r) else: - await self._log("%s was offline: %s" % (member_name, r["data"][0]["meta"]["location"])) + last_join_dt_utc = datetime.strptime(r["data"][0]["meta"]["lastJoin"], "%Y-%m-%dT%H:%M:%S.%fZ") + last_join_dt_local = utc_to_local(last_join_dt_utc) + + if last_join_dt_local + timedelta(minutes=10) > datetime.now(): + await self._log("%s is ALMOST online" % member_name) + almost_online_players.append(r) + else: + await self._log("%s was offline: %s" % (member_name, r["data"][0]["meta"]["location"])) await self._log("Setting roles") # set max_combat_lvl_class role @@ -290,9 +301,15 @@ class WynncraftCog(commands.Cog): for online_player in online_players: s = ":green_circle: **%s** connecté sur %s" % (online_player["data"][0]["username"], online_player["data"][0]["meta"]["location"]["server"]) full_text += s + "\n" - if not online_players: + for almost_online_player in almost_online_players: + last_join_dt_utc = datetime.strptime(almost_online_player["data"][0]["meta"]["lastJoin"], "%Y-%m-%dT%H:%M:%S.%fZ") + last_join_dt_local = utc_to_local(last_join_dt_utc) + mins = (datetime.now() - last_join_dt_local).total_seconds() // 60 + s = ":grey_question: **%s** était connecté il y a %s minutes" % (almost_online_player["data"][0]["username"], mins) + full_text += s + "\n" + if not online_players and not almost_online_players: full_text = "Personne n'est connecté :disappointed_relieved:\n" - full_text += "\n*Dernière mise à jour: %s*\n*Les vraies informations peuvent prendre jusqu'à 5 minutes de plus*" % datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") + full_text += "\n*Dernière mise à jour: %s*\n*Les vraies informations peuvent prendre jusqu'à 5 minutes de plus*" % datetime.now().strftime("%d/%m/%Y %H:%M:%S") online_channel = self.bot.get_channel(750100968766701708) update_msg = await online_channel.fetch_message(await self.config.guild(guild).update_msg()) await update_msg.edit(content=full_text)