Exemple #1
0
async def ask_attachements(
        bot,
        channel,
        user,
        timeout=Configuration.get_var("question_timeout_seconds"),
        max_files=Configuration.get_var('max_attachments')):

    def check(message):
        return user == message.author and message.channel == channel

    done = False

    def ready():
        nonlocal done
        done = True

    async def restart_attachments():
        nonlocal final_attachments
        final_attachments = []
        await ask(bot, channel, user, Lang.get_string("questions/attachments_restart"), [
            Option("YES", Lang.get_string('questions/restart_attachments_yes')),
            Option("NO", Lang.get_string('questions/restart_attachments_no'), handler=ready)
        ], show_embed=True)

    while not done:
        ask_again = True
        final_attachments = []
        count = 0

        def confirmed():
            nonlocal ask_again
            ask_again = False

        while ask_again:
            if not final_attachments:
                await channel.send(Lang.get_string("questions/attachment_prompt", max=max_files))
            elif len(final_attachments) < max_files - 1:
                await channel.send(
                    Lang.get_string("questions/attachment_prompt_continued", max=max_files - len(final_attachments))
                )
            elif len(final_attachments):
                await channel.send(Lang.get_string("questions/attachment_prompt_final"))

            done = False

            try:
                while True:
                    message = await bot.wait_for('message', timeout=timeout, check=check)
                    links = Utils.URL_MATCHER.findall(message.content)
                    attachment_links = [str(a.url) for a in message.attachments]
                    if len(links) is not 0 or len(message.attachments) is not 0:
                        if (len(links) + len(message.attachments)) > max_files:
                            await channel.send(Lang.get_string("questions/attachments_overflow", max=max_files))
                        else:
                            final_attachments += links + attachment_links
                            count += len(links) + len(attachment_links)
                            break
                    else:
                        await channel.send(Lang.get_string("questions/attachment_not_found"))
            except asyncio.TimeoutError as ex:
                await channel.send(
                    Lang.get_string("questions/error_reaction_timeout",
                                    error_emoji=Emoji.get_emoji("WARNING"),
                                    timeout=timeout_format(timeout))
                )
                raise ex
            else:
                if count < max_files:
                    await ask(bot, channel, user, Lang.get_string('questions/another_attachment'),
                              [Option("YES"), Option("NO", handler=confirmed)])
                else:
                    ask_again = False

        prompt_yes = Lang.get_string("questions/approve_attachments")
        if len(final_attachments) == 1:
            prompt_no = Lang.get_string('questions/restart_attachment_singular')
        else:
            prompt_no = Lang.get_string('questions/restart_attachment_plural')
        await ask(bot, channel, user, Lang.get_string('questions/confirm_attachments'), [
            Option("YES", prompt_yes, handler=ready),
            Option("NO", prompt_no, handler=restart_attachments)
        ], show_embed=True)

    return final_attachments