adding refresh button
This commit is contained in:
parent
69ea7bf5db
commit
6f6bd3ac11
2 changed files with 84 additions and 66 deletions
|
@ -1,7 +1,7 @@
|
|||
import discord
|
||||
from discord.ext import commands, tasks
|
||||
from discord_slash import cog_ext
|
||||
from utils.reminder import Reminder
|
||||
from utils.reminder import Reminder, embedListe, listReaction
|
||||
from utils.core import getURLsInString, getMentionInString, isSlash, mySendHidden, mentionToUser, cleanCodeStringWithMentionAndURLs
|
||||
from utils.time import stringTempsVersSecondes, nowUTC, intToDatetime, timedeltaToString
|
||||
|
||||
|
@ -150,9 +150,11 @@ class ReminderDiscord(commands.Cog):
|
|||
if fromSlash != True:
|
||||
await ctx.message.add_reaction(emoji = '✅')
|
||||
|
||||
embed, pageMAX = Reminder().embedListe(utilisateur, ctx.guild.id, page)
|
||||
embed, pageMAX, refresh = embedListe(utilisateur, ctx.guild.id, page)
|
||||
message = await ctx.send(embed = embed)
|
||||
if pageMAX > 1:
|
||||
if refresh:
|
||||
await message.add_reaction("🔄")
|
||||
elif pageMAX > 1:
|
||||
for emoji in ["⬅️", "➡️"]:
|
||||
await message.add_reaction(emoji)
|
||||
@cog_ext.cog_slash(name="reminderlist", description = "Affiche la liste des rappels d'un utilisateur.")
|
||||
|
@ -164,13 +166,13 @@ class ReminderDiscord(commands.Cog):
|
|||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_add(self, payload):
|
||||
message, embed = await Reminder().listReaction(self.client, payload)
|
||||
message, embed = await listReaction(self.client, payload)
|
||||
if message:
|
||||
await message.edit(embed = embed)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_remove(self, payload):
|
||||
message, embed = await Reminder().listReaction(self.client, payload)
|
||||
message, embed = await listReaction(self.client, payload)
|
||||
if message:
|
||||
await message.edit(embed = embed)
|
||||
|
||||
|
|
|
@ -70,69 +70,85 @@ class Reminder(Database):
|
|||
"""
|
||||
return True if self.affichageResultat(self.requete(requete, [id, userID, guildID]))[0][0] == 1 else False
|
||||
|
||||
def embedListe(self, utilisateur, guildID, page, color = None):
|
||||
"""Fais l'embed d'une page pour l'affichage de la liste des reminders d'un utilisateur"""
|
||||
reminders = self.listeReminder(utilisateur.id, guildID)
|
||||
def embedListe(utilisateur, guildID, page, color = None):
|
||||
"""Fais l'embed d'une page pour l'affichage de la liste des reminders d'un utilisateur."""
|
||||
reminders = Reminder().listeReminder(utilisateur.id, guildID)
|
||||
|
||||
pageMAX = -(-len(reminders) // 5)
|
||||
if color == None:
|
||||
color = Colour.random()
|
||||
embed = Embed(description = f"**Rappel{'s' if len(reminders) > 1 else ''} de {utilisateur.mention}** • Page {page}/{pageMAX}", color = color)
|
||||
embed.set_thumbnail(url = utilisateur.avatar_url_as(size = 64))
|
||||
limit = 5 * page
|
||||
if len(reminders) > 0 and page <= pageMAX:
|
||||
curseur = limit - 4
|
||||
for reminder in reminders[limit - 5:]:
|
||||
if curseur <= limit:
|
||||
texte = reminder[0]
|
||||
if len(texte) > 1024:
|
||||
texte = f"{texte[:1021]}..."
|
||||
expiration = reminder[2] - int(nowUTC())
|
||||
if expiration > 0:
|
||||
expiration = f"Expire dans {timedeltaToString(expiration)}"
|
||||
else:
|
||||
expiration = f"A déjà expiré."
|
||||
embed.add_field(value = texte, name = f"#{reminder[3]} • Fais le {timestampScreen(intToDatetime(reminder[1]))}\n{expiration}", inline = False)
|
||||
curseur += 1
|
||||
else:
|
||||
embed.add_field(name = "\u200b", value = f"L'utilisateur n'a aucun rappel en attente ou page n°{page} vide !")
|
||||
embed.set_footer(text = "Les rappels qui ont déjà expirés vont apparaître dans quelques instants.\nIls peuvent avoir jusqu'à 1 minute de retard maximum.")
|
||||
return (embed, pageMAX)
|
||||
pageMAX = -(-len(reminders) // 5)
|
||||
if pageMAX > 1:
|
||||
refresh = False
|
||||
else:
|
||||
refresh = True
|
||||
if color == None:
|
||||
color = Colour.random()
|
||||
embed = Embed(description = f"**Rappel{'s' if len(reminders) > 1 else ''} de {utilisateur.mention}** • Page {page}/{pageMAX}", color = color)
|
||||
embed.set_thumbnail(url = utilisateur.avatar_url_as(size = 64))
|
||||
limit = 5 * page
|
||||
if len(reminders) > 0 and page <= pageMAX:
|
||||
curseur = limit - 4
|
||||
for reminder in reminders[limit - 5:]:
|
||||
if curseur <= limit:
|
||||
texte = reminder[0]
|
||||
if len(texte) > 1024:
|
||||
texte = f"{texte[:1021]}..."
|
||||
expiration = reminder[2] - int(nowUTC())
|
||||
if expiration > 0:
|
||||
expiration = f"Expire dans {timedeltaToString(expiration)}"
|
||||
else:
|
||||
expiration = f"A déjà expiré."
|
||||
embed.add_field(value = texte, name = f"#{reminder[3]} • Fais le {timestampScreen(intToDatetime(reminder[1]))}\n{expiration}", inline = False)
|
||||
curseur += 1
|
||||
else:
|
||||
embed.add_field(name = "\u200b", value = f"L'utilisateur n'a aucun rappel en attente ou page n°{page} vide !")
|
||||
embed.set_footer(text = "Les rappels qui ont déjà expirés vont apparaître dans quelques instants.\nIls peuvent avoir jusqu'à 1 minute de retard maximum.")
|
||||
return (embed, pageMAX, refresh)
|
||||
|
||||
async def listReaction(self, client, payload):
|
||||
if payload.emoji.name in ["⬅️", "➡️"]:
|
||||
if payload.event_type == "REACTION_ADD":
|
||||
if payload.member.bot == True:
|
||||
return False, False
|
||||
channel = client.get_channel(payload.channel_id)
|
||||
message = await channel.fetch_message(payload.message_id)
|
||||
if message.author.id != client.user.id and len(message.embeds) == 0: # vérification message du bot + au moins 1 embed
|
||||
async def listReaction(client, payload):
|
||||
"""Gère le changement de page du reminderlist avec les réactions."""
|
||||
if payload.emoji.name in ["⬅️", "🔄", "➡️"]:
|
||||
if payload.event_type == "REACTION_ADD":
|
||||
if payload.member.bot == True:
|
||||
return False, False
|
||||
embed = message.embeds[0].to_dict()
|
||||
if "description" in embed: # si c'est un embed avec une description
|
||||
if len(findall(r"\*\*Rappels? de <@\!?\d+>\*\* • Page \d+\/\d+", embed["description"])) == 1: # si c'est le bon embed
|
||||
infoDescription = findall(r"\*\*Rappels? de <@\!?(\d+)>\*\* • Page (\d+)\/(\d+)", embed["description"])[0]
|
||||
utilisateur = client.get_user(int(infoDescription[0]))
|
||||
page = int(infoDescription[1])
|
||||
if payload.emoji.name == "⬅️":
|
||||
if page > 1:
|
||||
page -= 1
|
||||
else:
|
||||
if int(infoDescription[2]) > 1:
|
||||
page += 1
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "➡️":
|
||||
if page + 1 <= int(infoDescription[2]):
|
||||
channel = client.get_channel(payload.channel_id)
|
||||
message = await channel.fetch_message(payload.message_id)
|
||||
if message.author.id != client.user.id and len(message.embeds) == 0: # vérification message du bot + au moins 1 embed
|
||||
return False, False
|
||||
embed = message.embeds[0].to_dict()
|
||||
if "description" in embed: # si c'est un embed avec une description
|
||||
if len(findall(r"\*\*Rappels? de <@\!?\d+>\*\* • Page \d+\/\d+", embed["description"])) == 1: # si c'est le bon embed
|
||||
infoDescription = findall(r"\*\*Rappels? de <@\!?(\d+)>\*\* • Page (\d+)\/(\d+)", embed["description"])[0]
|
||||
utilisateur = client.get_user(int(infoDescription[0]))
|
||||
page = int(infoDescription[1])
|
||||
if payload.emoji.name == "⬅️":
|
||||
if page > 1:
|
||||
page -= 1
|
||||
else:
|
||||
if int(infoDescription[2]) > 1:
|
||||
page += 1
|
||||
else:
|
||||
if len(findall(r"L'utilisateur n'a aucun rappel en attente ou page n°\d+ vide !", embed["fields"][0]["value"])) == 1:
|
||||
if len(self.listeReminder(utilisateur.id, payload.guild_id)) > 0:
|
||||
page += 1
|
||||
else:
|
||||
return False, False
|
||||
else:
|
||||
return False, False
|
||||
embed, _ = Reminder().embedListe(utilisateur, payload.guild_id, page, embed["color"])
|
||||
return message, embed
|
||||
return False, False
|
||||
return False, False
|
||||
if payload.emoji.name == "➡️":
|
||||
if page + 1 <= int(infoDescription[2]):
|
||||
page += 1
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "🔄":
|
||||
reminders = Reminder().listeReminder(utilisateur.id, payload.guild_id)
|
||||
if len(reminders) > 0:
|
||||
page = 1
|
||||
if -(-len(reminders) // 5) > 1 and message.reactions[0] != "⬅️":
|
||||
for emoji in ["⬅️", "➡️"]:
|
||||
await message.add_reaction(emoji)
|
||||
else:
|
||||
return False, False
|
||||
embed, _, refresh = embedListe(utilisateur, payload.guild_id, page, embed["color"])
|
||||
if refresh:
|
||||
await message.add_reaction("🔄")
|
||||
else:
|
||||
for reaction in message.reactions:
|
||||
if str(reaction) == "🔄":
|
||||
users = await reaction.users().flatten()
|
||||
for user in users:
|
||||
await message.remove_reaction("🔄", user)
|
||||
return message, embed
|
||||
return False, False
|
||||
|
|
Reference in a new issue