moves a function from the reminder to the new page.py file for a more global use
This commit is contained in:
parent
c80eb540eb
commit
d2bfe8cbe3
3 changed files with 70 additions and 59 deletions
|
@ -1,7 +1,8 @@
|
|||
import discord
|
||||
from discord.ext import commands, tasks
|
||||
from discord_slash import cog_ext
|
||||
from utils.reminder import Reminder, embedListe, listReaction
|
||||
from utils.reminder import Reminder
|
||||
from utils.page import embedListeReminder, listReaction
|
||||
from utils.core import getURLsInString, getMentionInString, isSlash, mySendHidden
|
||||
from utils.core import addReaction, mentionToUser, cleanCodeStringWithMentionAndURLs
|
||||
from utils.time import stringTempsVersSecondes, nowUTC, intToDatetime, timedeltaToString
|
||||
|
@ -162,7 +163,7 @@ class ReminderDiscord(commands.Cog, name="Reminder"):
|
|||
if fromSlash != True:
|
||||
await addReaction(ctx.message, 0)
|
||||
|
||||
embed, pageMAX, refresh = await embedListe(utilisateur, ctx.guild.id, page)
|
||||
embed, pageMAX, refresh = await embedListeReminder(utilisateur, ctx.guild.id, page)
|
||||
message = await ctx.send(embed = embed)
|
||||
if refresh:
|
||||
await message.add_reaction("🔄")
|
||||
|
@ -180,14 +181,14 @@ class ReminderDiscord(commands.Cog, name="Reminder"):
|
|||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_add(self, payload):
|
||||
"""Triggered when a reaction is added"""
|
||||
message, embed = await listReaction(self.client, payload)
|
||||
message, embed = await listReaction(0, self.client, payload)
|
||||
if message:
|
||||
await message.edit(embed = embed)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_remove(self, payload):
|
||||
"""Triggered when a reaction is removed"""
|
||||
message, embed = await listReaction(self.client, payload)
|
||||
message, embed = await listReaction(0, self.client, payload)
|
||||
if message:
|
||||
await message.edit(embed = embed)
|
||||
|
||||
|
|
64
src/utils/page.py
Normal file
64
src/utils/page.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
from utils.reminder import embedListeReminder
|
||||
from utils.todo import embedListeToDo
|
||||
from re import findall
|
||||
|
||||
async def listReaction(type: int, client, payload):
|
||||
"""
|
||||
Gère le changement de page du reminderlist ou du todolist avec les réactions.
|
||||
Types: 0 -> reminder | 1 -> todo
|
||||
"""
|
||||
if payload.emoji.name in ["⬅️", "🔄", "➡️"]:
|
||||
if payload.event_type == "REACTION_ADD":
|
||||
if payload.member.bot == True: # vérifie que c'est pas un bot qui a réagit
|
||||
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 or 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?|To Do('s)?) 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])
|
||||
refresh_message = None
|
||||
if payload.emoji.name == "⬅️":
|
||||
if page > 1:
|
||||
page -= 1
|
||||
else:
|
||||
if int(infoDescription[2]) > 1:
|
||||
page = int(infoDescription[2])
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "➡️":
|
||||
if page + 1 <= int(infoDescription[2]):
|
||||
page += 1
|
||||
else:
|
||||
if page > 1:
|
||||
page = 1
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "🔄":
|
||||
refresh_message = message
|
||||
if type == 0:
|
||||
embed, _, refresh = await embedListeReminder(utilisateur, payload.guild_id, page, embed["color"], refresh_message)
|
||||
elif type == 1:
|
||||
embed, _, refresh = await embedListeToDo(utilisateur, page, embed["color"], refresh_message)
|
||||
else:
|
||||
raise Exception("Unknown type")
|
||||
if embed == False:
|
||||
return False, False
|
||||
if refresh == True:
|
||||
await message.add_reaction("🔄")
|
||||
await removeReactions(message, ["⬅️", "➡️"])
|
||||
else:
|
||||
await removeReactions(message, ["🔄"])
|
||||
return message, embed
|
||||
return False, False
|
||||
|
||||
async def removeReactions(message, reactions):
|
||||
for reaction in message.reactions:
|
||||
if str(reaction) in reactions:
|
||||
users = await reaction.users().flatten()
|
||||
for user in users:
|
||||
await message.remove_reaction(reaction, user)
|
|
@ -1,7 +1,6 @@
|
|||
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):
|
||||
|
@ -70,7 +69,7 @@ class Reminder(Database):
|
|||
"""
|
||||
return True if self.affichageResultat(self.requete(requete, [id, userID, guildID]))[0][0] == 1 else False
|
||||
|
||||
async def embedListe(utilisateur, guildID, page, color = None, refresh_message = None):
|
||||
async def embedListeReminder(utilisateur, guildID, page, color = None, refresh_message = 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)
|
||||
|
@ -111,56 +110,3 @@ async def embedListe(utilisateur, guildID, page, color = None, refresh_message =
|
|||
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(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: # vérifie que c'est pas un bot qui a réagit
|
||||
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 or 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])
|
||||
refresh_message = None
|
||||
if payload.emoji.name == "⬅️":
|
||||
if page > 1:
|
||||
page -= 1
|
||||
else:
|
||||
if int(infoDescription[2]) > 1:
|
||||
page = int(infoDescription[2])
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "➡️":
|
||||
if page + 1 <= int(infoDescription[2]):
|
||||
page += 1
|
||||
else:
|
||||
if page > 1:
|
||||
page = 1
|
||||
else:
|
||||
return False, False
|
||||
if payload.emoji.name == "🔄":
|
||||
refresh_message = message
|
||||
embed, _, refresh = await embedListe(utilisateur, payload.guild_id, page, embed["color"], refresh_message)
|
||||
if embed == False:
|
||||
return False, False
|
||||
if refresh == True:
|
||||
await message.add_reaction("🔄")
|
||||
await removeReactions(message, ["⬅️", "➡️"])
|
||||
else:
|
||||
await removeReactions(message, ["🔄"])
|
||||
return message, embed
|
||||
return False, False
|
||||
|
||||
async def removeReactions(message, reactions):
|
||||
for reaction in message.reactions:
|
||||
if str(reaction) in reactions:
|
||||
users = await reaction.users().flatten()
|
||||
for user in users:
|
||||
await message.remove_reaction(reaction, user)
|
Reference in a new issue