diff --git a/bot.py b/bot.py index 5905c24..baaaec1 100755 --- a/bot.py +++ b/bot.py @@ -257,6 +257,22 @@ async def remiser(ctx: commands.Context, type_defi: Literal['capture', 'competit await ctx.reply(f"Le défi de {type_defi} n°{id_defi} ({defi['nom']}) n'était déjà pas dans la défausse.") +class MainView(discord.ui.View): + def __init__(self, ctx: commands.Context, defis: list[dict], timeout: float | None = 180.0): + super().__init__(timeout=timeout) + + async def terminer_defi(self, id_defi: int, interaction: discord.Interaction): + await interaction.response.defer() + await terminer(ctx, id_defi) + + for id_defi in defis: + defi = next(defi for defi in DEFIS['capture'] if defi['id'] == id_defi) + button = discord.ui.Button(style=discord.ButtonStyle.success, label=f"Terminer {defi['nom']}") + button.callback = partial(terminer_defi, self, id_defi) + self.add_item(button) + + + @bot.command(name="main") async def afficher_main(ctx: commands.Context, mode: Literal['public', 'prive'] = "prive", author_id: int | None = None): author_id = author_id or ctx.author.id @@ -277,15 +293,16 @@ async def afficher_main(ctx: commands.Context, mode: Literal['public', 'prive'] if mode == "public": await ctx.send(f"Défis de l'équipe **{couleur}** :", embeds=embeds) else: - channel_dm = await bot.create_dm(ctx.author) - await channel_dm.send("Vos défis en main :", embeds=embeds) + channel_dm = await bot.create_dm(namedtuple('User', 'id')(author_id)) + await channel_dm.send("Vos défis en main :", embeds=embeds, view=MainView(ctx, main)) @bot.command() -async def terminer(ctx: commands.Context, id_defi: int): +async def terminer(ctx: commands.Context, id_defi: int, author_id: int | None = None): if all(id_defi != defi['id'] for defi in DEFIS['capture']): raise commands.BadArgument(f"Erreur : Le défi {id_defi_1} n'existe pas") - author_id = ctx.author.id + defi = next(defi for defi in DEFIS['capture'] if defi['id'] == id_defi) + author_id = author_id or ctx.author.id for equipe, membres_equipe in data['equipes'].items(): if author_id in membres_equipe: break @@ -303,6 +320,7 @@ async def terminer(ctx: commands.Context, id_defi: int): with DATA_FILE.open('w') as data_file: json.dump(data, data_file, indent=2) + await ctx.send(f"Défi n°{id_defi} **{defi['nom']}** terminé ! Il est retiré de votre main.") colour = discord.Color.red() if equipe == "rouge" else discord.Color.green() embed = discord.Embed(title=nouveau_defi['nom'], description=nouveau_defi['description'], colour=colour) embed.set_footer(text=f"Défi n°{nouveau_defi['id']}")