adding custom timezone everywhere with one unique call per file

This commit is contained in:
Mylloon 2021-05-13 08:36:42 +02:00
parent 1b94dd973d
commit f96d395777
2 changed files with 9 additions and 7 deletions

View file

@ -13,6 +13,7 @@ class Utils(commands.Cog):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
self.customTimezone = os.environ['TIMEZONE']
@commands.command(name='ping') @commands.command(name='ping')
@ -143,7 +144,7 @@ class Utils(commands.Cog):
await ctx.message.delete() await ctx.message.delete()
embed = discord.Embed(description = text, color = discord.Colour.random()) embed = discord.Embed(description = text, color = discord.Colour.random())
embed.set_author(name = f"Mémo noté depuis {ctx.guild.name}", icon_url = ctx.author.avatar_url) embed.set_author(name = f"Mémo noté depuis {ctx.guild.name}", icon_url = ctx.author.avatar_url)
embed.set_footer(text = f'📝 le {datetime.now(pytz.timezone("Europe/Paris")).strftime("%d/%m/%Y à %H:%M:%S")}') embed.set_footer(text = f'📝 le {datetime.now(pytz.timezone(self.customTimezone)).strftime("%d/%m/%Y à %H:%M:%S")}')
await ctx.author.send(embed = embed) await ctx.author.send(embed = embed)
return await ctx.send("Tu viens de recevoir ton mémo !", delete_after = 5) return await ctx.send("Tu viens de recevoir ton mémo !", delete_after = 5)
@_memo.error @_memo.error
@ -246,14 +247,14 @@ class Utils(commands.Cog):
embed.add_field(name = "ID", value = user[0].id) embed.add_field(name = "ID", value = user[0].id)
value = str(user[0].created_at.astimezone(timezone(os.environ['TIMEZONE'])))[:-13].replace('-', '/').split() value = str(user[0].created_at.astimezone(timezone(self.customTimezone)))[:-13].replace('-', '/').split()
embed.add_field(name = "Compte créé le", value = f"{value[0][8:]}/{value[0][5:-3]}/{value[0][:4]} à {value[1]}") embed.add_field(name = "Compte créé le", value = f"{value[0][8:]}/{value[0][5:-3]}/{value[0][:4]} à {value[1]}")
embed.add_field(name = "Âge du compte", value = self._ageLayout(self._get_age(user[0].created_at))) embed.add_field(name = "Âge du compte", value = self._ageLayout(self._get_age(user[0].created_at)))
embed.add_field(name = "Mention", value = user[0].mention) embed.add_field(name = "Mention", value = user[0].mention)
value = str(user[0].joined_at.astimezone(timezone(os.environ['TIMEZONE'])))[:-13].replace('-', '/').split() value = str(user[0].joined_at.astimezone(timezone(self.customTimezone)))[:-13].replace('-', '/').split()
embed.add_field(name = "Serveur rejoint le", value = f"{value[0][8:]}/{value[0][5:-3]}/{value[0][:4]} à {value[1]}") embed.add_field(name = "Serveur rejoint le", value = f"{value[0][8:]}/{value[0][5:-3]}/{value[0][:4]} à {value[1]}")
embed.add_field(name = "Est sur le serveur depuis", value = self._ageLayout(self._get_age(user[0].joined_at))) embed.add_field(name = "Est sur le serveur depuis", value = self._ageLayout(self._get_age(user[0].joined_at)))

View file

@ -6,6 +6,7 @@ from random import choice
from datetime import datetime from datetime import datetime
from pytz import timezone from pytz import timezone
customPrefix = os.environ['PREFIX'] customPrefix = os.environ['PREFIX']
customTimezone = os.environ['TIMEZONE']
client = commands.Bot(command_prefix = customPrefix, case_insensitive = True, intents = discord.Intents.all()) client = commands.Bot(command_prefix = customPrefix, case_insensitive = True, intents = discord.Intents.all())
@ -197,9 +198,9 @@ async def on_message_delete(message):
embed.set_author(name = user_or_nick(message.author), icon_url = message.author.avatar_url) embed.set_author(name = user_or_nick(message.author), icon_url = message.author.avatar_url)
if not user_suppressed: if not user_suppressed:
embed.set_footer(text = f"Channel: #{message.channel.name} | Date : {goodTimezone(message.created_at, 1)}\nSupprimé le {datetime.now(pytz.timezone(os.environ['TIMEZONE'])).strftime('%d/%m/%Y à %H:%M:%S')}") embed.set_footer(text = f"Channel: #{message.channel.name} | Date : {goodTimezone(message.created_at, 1)}\nSupprimé le {datetime.now(pytz.timezone(customTimezone)).strftime('%d/%m/%Y à %H:%M:%S')}")
else: else:
embed.set_footer(icon_url = user_suppressed.avatar_url, text = f"Channel: #{message.channel.name} | Date : {goodTimezone(message.created_at, 1)}\nSupprimé par {user_or_nick(user_suppressed)} le {datetime.now(pytz.timezone(os.environ['TIMEZONE'])).strftime('%d/%m/%Y à %H:%M:%S')}") embed.set_footer(icon_url = user_suppressed.avatar_url, text = f"Channel: #{message.channel.name} | Date : {goodTimezone(message.created_at, 1)}\nSupprimé par {user_or_nick(user_suppressed)} le {datetime.now(pytz.timezone(customTimezone)).strftime('%d/%m/%Y à %H:%M:%S')}")
await channel.send(embed = embed) await channel.send(embed = embed)
# ne fonctionne pas quand un message a été supprimé avant que le bot ai démarré # ne fonctionne pas quand un message a été supprimé avant que le bot ai démarré
@ -213,8 +214,8 @@ def user_or_nick(user):
def goodTimezone(date, type): def goodTimezone(date, type):
if type == 0: if type == 0:
return str(pytz.timezone(os.environ['TIMEZONE']).fromutc(date))[:-13].replace('-', '/').split() return str(pytz.timezone(customTimezone).fromutc(date))[:-13].replace('-', '/').split()
elif type == 1: elif type == 1:
return str(pytz.timezone(os.environ['TIMEZONE']).fromutc(date))[:-13].replace('-', '/').replace(' ', ' à ') return str(pytz.timezone(customTimezone).fromutc(date))[:-13].replace('-', '/').replace(' ', ' à ')
client.run(os.environ['TOKEN_DISCORD']) client.run(os.environ['TOKEN_DISCORD'])