From 38057e610a011773700f4af44c04e9372c694c7b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 3 Jun 2021 20:14:45 +0200 Subject: [PATCH] meilleur affichage du temps --- src/cogs/utils.py | 6 +++--- src/utils/core.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/cogs/utils.py b/src/cogs/utils.py index 1f90ed6..55b87ba 100644 --- a/src/cogs/utils.py +++ b/src/cogs/utils.py @@ -9,7 +9,7 @@ from pytz import timezone from discord_slash import cog_ext import shlex from utils.core import map_list_among_us, get_age, getURLsInString, getMentionInString, cleanCodeStringWithMentionAndURLs -from utils.core import cleanUser, userOrNick, ageLayout, stringTempsVersSecondes, nowTimestampCustom, intToTimestamp, nowTimestampUTC +from utils.core import cleanUser, userOrNick, ageLayout, stringTempsVersSecondes, timedeltaToString, intToTimestamp, nowTimestampUTC from utils.reminder import Reminder def setup(client): @@ -503,7 +503,7 @@ class Utils(commands.Cog): if fromSlash != True: messageID = ctx.message.id Reminder().ajoutReminder(messageID, ctx.channel.id, mention, reminder, now, now + seconds, ctx.author.id) - return await ctx.send(f"Ok, je t'en parles dans {time} !") + return await ctx.send(f"Ok, je t'en parles dans {timedeltaToString(seconds)} !") await ctx.send(embed = embed) @cog_ext.cog_slash(name="reminder", description = "Met en place un rappel.") async def __reminder(self, ctx, time, reminder = None): @@ -529,7 +529,7 @@ class Utils(commands.Cog): except: pass finalEmbed = discord.Embed(description = cleanCodeStringWithMentionAndURLs(reminder), timestamp = intToTimestamp(expired[3]), color = discord.Colour.random()) - finalEmbed.set_footer(text=f"Message d'il y a {str(timedelta(seconds = int(nowTimestampUTC()) - expired[3])).replace('days', 'jours')} secondes") + finalEmbed.set_footer(text=f"Message d'il y a {timedeltaToString(int(nowTimestampUTC()) - expired[3])}") links = "" findedURLs = getURLsInString(reminder) diff --git a/src/utils/core.py b/src/utils/core.py index f008b79..809804a 100644 --- a/src/utils/core.py +++ b/src/utils/core.py @@ -4,7 +4,7 @@ import requests import time import os from pytz import timezone -from datetime import datetime +from datetime import datetime, timedelta myTimezone = os.environ['TIMEZONE'] @@ -188,3 +188,19 @@ def nowTimestampUTC(): def intToTimestamp(int): return datetime.fromtimestamp(int) + +def timedeltaToString(time): + age = str(timedelta(seconds = time)).replace('days, ', 'jours, :').split(':') + if len(age) == 4: + a = [1, 1, 1, 1] # a pour affichage + if len(age) == 3: + a = [0, 1, 1, 1] + age.insert(0, None) + for i in range(1, 4): + if int(age[i]) == 0: + a[i] = 0 + age[0] = age[0] if a[0] == 1 else '' + age[1] = f"{age[1]}h " if a[1] == 1 else '' + age[2] = f"{age[2]}m " if a[2] == 1 else '' + age[3] = f"{age[3]}s" if a[3] == 1 else '' + return ''.join(age)