From ce0df405c179a599e42e49b1d6bed1ab40ebea67 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Tue, 14 Jul 2020 21:09:44 +0200 Subject: [PATCH] Creating the stop function --- config.py | 4 +++- helper.py | 27 +++++++++++++++++++++++++++ main.py | 35 +++++++++++++++++++++++++++++++++++ nemo.py | 2 +- 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index 225c318..7749be7 100644 --- a/config.py +++ b/config.py @@ -59,4 +59,6 @@ Ouvrir l'event a tous le monde: ```!open [message]``` Faire un event privé et inviter les personnes individuellement: ```!private``` -""" \ No newline at end of file +""" + +STOP_MSG = """Pour confirmer la fermeture de l'event, cliquez sur ✅.""" \ No newline at end of file diff --git a/helper.py b/helper.py index 1998fed..fd858ed 100644 --- a/helper.py +++ b/helper.py @@ -1,3 +1,7 @@ +import discord +import config + + def auto_delete(f): async def wrapper(*args, **kwargs): ret = await f(*args, **kwargs) @@ -6,3 +10,26 @@ def auto_delete(f): return wrapper + +def event_command(f): + async def wrapper(*args, **kwargs): + channel: discord.TextChannel = kwargs["message"].channel + if not channel.name[:1].isdigit(): + return None + return await f(*args, **kwargs, event=int(channel.name[:1])) + + return wrapper + + +def organizer_only(f): + async def wrapper(*args, **kwargs): + member: discord.Member = kwargs["member"] + channel: discord.TextChannel = kwargs["message"].channel + event: int = kwargs["event"] + if "administrator" in channel.permissions_for(member): + return await f(*args, **kwargs) + if any(x for x in member.roles if x.name == f"{config.ORGANIZER_PREFIX}{event}"): + return await f(*args, **kwargs) + return None + + return wrapper diff --git a/main.py b/main.py index 04b3b9b..f49837a 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,6 @@ #!/usr/bin/env python3 +import asyncio + import nemo import helper import config @@ -77,5 +79,38 @@ async def edit_event_status(index: int, text: str, list_msg: discord.Message): await list_msg.edit(content="\n".join(content)) +@nemo.command("!close") +@nemo.command("!stop") +@helper.event_command +@helper.organizer_only +@helper.auto_delete +async def stop(*, + channel: discord.TextChannel, + member: discord.Member, + message: discord.Message, + guild: discord.Guild, + event: int, + **_): + msg = await channel.send(config.STOP_MSG.replace("@User", f"<@{member.id}>")) + await msg.add_reaction("✅") + try: + await nemo.wait_for("reaction_add", timeout=60, check=lambda reaction, user: user == message.author) + except asyncio.TimeoutError: + await msg.delete() + return + await discord.utils.get(guild.roles, name=f"{config.ORGANIZER_PREFIX}{event}").delete() + await discord.utils.get(guild.roles, name=f"{config.PARTICIPANT_PREFIX}{event}").delete() + await channel.delete() + org_channel: discord.TextChannel = discord.utils.get(guild.channels, name=config.ORGANIZATION_NAME) + list_msg: discord.Message = [x async for x in org_channel.history() if config.LIST_KEY in x.content][0] + if f"**{event + 1}:**" in list_msg.content: + await edit_event_status(event, config.EMPTY_SLOT, list_msg) + elif event == 1: + await list_msg.edit(content=config.EVENT_LIST) + else: + await list_msg.edit(content='\n'.join(list_msg.content.split('\n')[:-1])) + await list_msg.clear_reaction(number_emojis[event - 1]) + + if __name__ == "__main__": nemo.run(config.TOKEN) diff --git a/nemo.py b/nemo.py index 0793280..fad9770 100644 --- a/nemo.py +++ b/nemo.py @@ -20,7 +20,7 @@ class Nemo(discord.Client): return print(f"{message.author.display_name} used the command: '{cmd[0]}'") try: - await f(cmd[1::], message=message, guild=message.guild) + await f(cmd[1::], message=message, guild=message.guild, channel=message.channel) except Exception: await message.channel.send(f"Fatal error: {traceback.format_exc()}") raise