From 7b6dbbfe3c8f4a3fc702162e93bf71af7a536caa Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Tue, 14 Jul 2020 03:55:18 +0200 Subject: [PATCH] Handling the event creation --- config.py | 24 +++++++++++++++++++++++- main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++---- nemo.py | 23 +++++++++++++++++++++++ run.sh | 1 + 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 05d2638..e569361 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,6 @@ -TOKEN = "ENTER YOUR DISCORD TOKEN HERE" +import os + +TOKEN = os.environ["NEMO_TOKEN"] CATEGORY_NAME = "EVENTS" ORGANIZATION_NAME = "organization" @@ -34,7 +36,27 @@ EVENT_LIST = """**Liste des events ouverts:** **Aucun event ouvert.** """ +LIST_KEY = "Liste des events ouverts" +EMPTY_KEY = "Aucun event ouvert" +EMPTY_SLOT = "Aucun event sur ce slot." CREATE_MSG = """**Créer un event: ** Pour créer un event, click sur ✅ """ +CREATE_KEY = "Créer un event" + +CONFIGURING_EVENT = "@User configure l'event." + + +EVENT_SUFFIX = "-event" +ORGANIZER_PREFIX = "Organisateur-" +PARTICIPANT_PREFIX = "Event-" + + +SETUP_MSG = """**Pour finaliser la création de l'event, faite l'une des commandes si dessous:** + +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 diff --git a/main.py b/main.py index 5a94f1f..9303314 100644 --- a/main.py +++ b/main.py @@ -5,12 +5,14 @@ import config import discord from discord.ext.commands import has_permissions +number_emojis = ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣"] + nemo = nemo.Nemo() @nemo.command("!") @has_permissions(administrator=True) -async def setup(_, message: discord.Message, guild: discord.Guild, **kwargs): +async def setup(*, message: discord.Message, guild: discord.Guild, **_): if any(x.name == config.CATEGORY_NAME and x.type == discord.ChannelType.category for x in guild.channels): return category = await guild.create_category(config.CATEGORY_NAME) @@ -25,16 +27,55 @@ async def setup(_, message: discord.Message, guild: discord.Guild, **kwargs): @nemo.command("!help") @has_permissions(administrator=True) @helper.auto_delete -async def help_command(_, message: discord.Message, **kwargs): +async def help_command(*, message: discord.Message, **_): await help_msg(message.channel) async def help_msg(channel: discord.TextChannel): await channel.send(config.HELP_MSG.replace("@Me", f"<@{nemo.user.id}>")) - msg = await channel.send(config.EVENT_LIST) - await msg.add_reaction("🔢") + await channel.send(config.EVENT_LIST) msg = await channel.send(config.CREATE_MSG) await msg.add_reaction("✅") + +@nemo.reaction(lambda reaction, member: reaction.message.author.bot and config.CREATE_KEY in reaction.message.content) +async def create_event(reaction: discord.Reaction, message: discord.Message, member: discord.Member, guild: discord.Guild, **_): + list_msg: discord.Message = next(x async for x in message.channel.history() if config.LIST_KEY in x.content) + index = get_new_event_index(list_msg) + await edit_event_status(index, config.CONFIGURING_EVENT, list_msg) + await reaction.remove(member) + + org_role = await guild.create_role(name=f"{config.ORGANIZER_PREFIX}{index}", color=discord.Color.purple) + await member.add_roles(org_role) + user_role = await guild.create_role(name=f"{config.PARTICIPANT_PREFIX}{index}") + await member.add_roles(user_role) + channel = await guild.create_text_channel(f"{index}{config.EVENT_SUFFIX}", overwrites={ + guild.default_role: discord.PermissionOverwrite(read_messages=False), + guild.me: discord.PermissionOverwrite(read_messages=True), + org_role: discord.PermissionOverwrite(read_messages=True, manage_messages=True), + user_role: discord.PermissionOverwrite(read_messages=True) + }, category=next(x.name == config.CATEGORY_NAME and x.type == discord.ChannelType.category for x in guild.channels)) + await list_msg.add_reaction(number_emojis[index - 1]) + await channel.send(config.SETUP_MSG) + + +def get_new_event_index(msg: discord.Message): + for x in msg.content.split('\n'): + if config.EMPTY_KEY in x or config.EMPTY_SLOT in x: + return int(x[2:x.index(':')]) + last_slot = msg.content.split('\n')[-1] + slot = int(last_slot[2:last_slot.index(':')]) + 1 + return slot if slot < 10 else -1 + + +async def edit_event_status(index: int, text: str, list_msg: discord.Message): + content = list_msg.content.split('\n') + try: + content[index + 1] = f"**{index}:** {text}" + except IndexError: + content.append(f"**{index}:** {text}") + await list_msg.edit(content="\n".join(content)) + + if __name__ == "__main__": nemo.run(config.TOKEN) diff --git a/nemo.py b/nemo.py index 73be741..e023e57 100644 --- a/nemo.py +++ b/nemo.py @@ -1,4 +1,6 @@ import traceback +from typing import Callable + import discord @@ -6,6 +8,7 @@ class Nemo(discord.Client): def __init__(self, **options): super().__init__(**options) self.commands = {} + self.reactions = [] async def on_ready(self): print(f"Logged in as {self.user}.") @@ -26,4 +29,24 @@ class Nemo(discord.Client): def wrapper(f): self.commands[cmd] = f return f + + return wrapper + + async def on_reaction_add(self, reaction: discord.Reaction, member: discord.Member): + if member.bot: + return + handler = next((x for x in self.reactions if x[0](reaction, member)), None) + if handler is None: + return + try: + await handler[1](reaction=reaction, member=member, message=reaction.message) + except Exception: + await reaction.message.channel.send(f"Fatal error: {traceback.format_exc()}") + raise + + def reaction(self, predicate: Callable[[discord.Reaction, discord.Member], bool]): + def wrapper(f): + self.reactions.append((predicate, f)) + return f + return wrapper diff --git a/run.sh b/run.sh index 4c48c48..d681aee 100755 --- a/run.sh +++ b/run.sh @@ -2,4 +2,5 @@ . venv/bin/activate pip install -U discord.py +export NEMO_TOKEN="INSERT YOUR TOKEN HERE" python3 main.py \ No newline at end of file