Add reaction for changing pages in reminderlist
This commit is contained in:
parent
2b9edd0f4e
commit
1f3dbcb6b6
2 changed files with 83 additions and 28 deletions
|
@ -3,7 +3,7 @@ from discord.ext import commands, tasks
|
|||
from discord_slash import cog_ext
|
||||
from utils.reminder import Reminder
|
||||
from utils.core import getURLsInString, getMentionInString, isSlash, mySendHidden, mentionToUser, cleanCodeStringWithMentionAndURLs
|
||||
from utils.time import stringTempsVersSecondes, nowUTC, intToDatetime, timedeltaToString, timestampScreen
|
||||
from utils.time import stringTempsVersSecondes, nowUTC, intToDatetime, timedeltaToString
|
||||
|
||||
def setup(client):
|
||||
client.add_cog(ReminderDiscord(client))
|
||||
|
@ -24,14 +24,14 @@ class ReminderDiscord(commands.Cog):
|
|||
channel = self.client.get_channel(expired[0]) # salon du message
|
||||
finalEmbed = discord.Embed(description = cleanCodeStringWithMentionAndURLs(reminder), timestamp = intToDatetime(expired[3]), color = discord.Colour.random())
|
||||
if expired[1] == 2: # s'il faut envoyer en DM le message
|
||||
user = self.client.get_user(userID)
|
||||
user = self.client.get_user(userID)
|
||||
if user == None: # si l'utilisateur n'est pas trouvé
|
||||
return Reminder().suppressionReminder(expired[5]) # suppression du rappel
|
||||
channel = await user.create_dm() # envoie en DM
|
||||
userID = None # plus de mention
|
||||
sourceMessage = None # plus de message source
|
||||
elif channel == None: # si le salon n'existe plus
|
||||
user = self.client.get_user(userID)
|
||||
user = self.client.get_user(userID)
|
||||
if user == None: # si l'utilisateur n'est pas trouvé
|
||||
return Reminder().suppressionReminder(expired[5]) # suppression du rappel
|
||||
channel = await user.create_dm() # envoie en DM
|
||||
|
@ -132,13 +132,13 @@ class ReminderDiscord(commands.Cog):
|
|||
async def _reminderlist(self, ctx, *arg):
|
||||
"""Affiche la liste des rappels d'un utilisateur.\n ➡ Syntaxe: {PREFIX}reminderlist/rl/remindlist/rappeliste [utilisateur]"""
|
||||
_, fromSlash, arg = isSlash(arg)
|
||||
utilisateur = ctx.author.id
|
||||
utilisateur = ctx.author
|
||||
page = 1
|
||||
erreur = False
|
||||
if len(arg) > 0:
|
||||
for i in range(0, len(arg)):
|
||||
try:
|
||||
utilisateur = mentionToUser(getMentionInString(arg[i])[0])
|
||||
utilisateur = self.client.get_user(mentionToUser(getMentionInString(arg[i])[0]))
|
||||
except:
|
||||
try:
|
||||
page = int(arg[i])
|
||||
|
@ -147,32 +147,14 @@ class ReminderDiscord(commands.Cog):
|
|||
if erreur == True:
|
||||
return await mySendHidden(ctx, fromSlash, "Veuillez renseigné un utilisateur ou un numéro de page valide.")
|
||||
|
||||
reminders = Reminder().listeReminder(utilisateur, ctx.guild.id)
|
||||
if fromSlash != True:
|
||||
await ctx.message.add_reaction(emoji = '✅')
|
||||
|
||||
pageMAX = -(-len(reminders) // 5)
|
||||
embed = discord.Embed(description = f"**Rappel{'s' if len(reminders) > 1 else ''} de <@{utilisateur}>** • Page {page}/{pageMAX}", color = discord.Colour.random())
|
||||
embed.set_thumbnail(url = self.client.get_user(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.")
|
||||
await ctx.send(embed = embed)
|
||||
embed, pageMAX = Reminder().embedListe(utilisateur, ctx.guild.id, page)
|
||||
message = await ctx.send(embed = embed)
|
||||
if 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.")
|
||||
async def __reminderlist(self, ctx, userorpage = None):
|
||||
if userorpage == None:
|
||||
|
@ -180,6 +162,18 @@ class ReminderDiscord(commands.Cog):
|
|||
else:
|
||||
return await self._reminderlist(ctx, userorpage, True)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_add(self, payload):
|
||||
message, embed = await Reminder().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)
|
||||
if message:
|
||||
await message.edit(embed = embed)
|
||||
|
||||
@commands.command(name='reminderdelete', aliases=["reminddelete", "rd"])
|
||||
async def _reminderdelete(self, ctx, *id):
|
||||
"""Suppprime un rappel.\n ➡ Syntaxe: {PREFIX}reminderdelete/rd <id>"""
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
from utils.db import Database
|
||||
from discord import Embed, Colour
|
||||
from utils.time import nowUTC, intToDatetime, timedeltaToString, timestampScreen
|
||||
from re import findall
|
||||
|
||||
class Reminder(Database):
|
||||
def __init__(self):
|
||||
|
@ -66,3 +69,61 @@ 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)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
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:
|
||||
return False, False
|
||||
if payload.emoji.name == "➡️":
|
||||
if page + 1 <= int(infoDescription[2]):
|
||||
page += 1
|
||||
else:
|
||||
return False, False
|
||||
embed, _ = Reminder().embedListe(utilisateur, payload.guild_id, page, embed["color"])
|
||||
return message, embed
|
||||
return False, False
|
||||
|
|
Reference in a new issue