Exemplo n.º 1
0
async def main(client):
    AVATARS_PATH.mkdir(exist_ok=True, parents=True)
    for path in AVATARS_PATH.glob('*.png'):
        path.unlink()

    with db:
        Member.drop_table()
        Member.create_table()

    members = [member async for member in client.juniorguru_guild.fetch_members(limit=None)
               if not member.bot]
    for member in members:
        log.info(f'Member {member.display_name} {member.id}')
        avatar_url = str(member.avatar_url)
        if is_default_avatar(avatar_url):
            avatar_path = None
        else:
            buffer = BytesIO()
            await member.avatar_url.save(buffer)
            image = Image.open(buffer)
            image = image.resize((SIZE_PX, SIZE_PX))
            image_path = AVATARS_PATH / f'{Path(urlparse(avatar_url).path).stem}.png'
            image.save(image_path, 'PNG')
            avatar_path = f'images/avatars/{image_path.name}'
        Member.create(id=member.id, avatar_path=avatar_path)
Exemplo n.º 2
0
def test_is_default_avatar_handles_non_string():
    class NonString:
        def __init__(self, url):
            self.url = url

        def __str__(self):
            return self.url

    assert club.is_default_avatar(
        NonString('https://cdn.discordapp.com/embed/avatars/4.png')) is True
Exemplo n.º 3
0
async def download_avatar(discord_member):
    avatar_url = str(discord_member.avatar_url)
    if is_default_avatar(avatar_url):
        return None
    else:
        buffer = BytesIO()
        await discord_member.avatar_url.save(buffer)
        image = Image.open(buffer)
        image = image.resize((AVATAR_SIZE_PX, AVATAR_SIZE_PX))
        image_path = AVATARS_PATH / f'{Path(urlparse(avatar_url).path).stem}.png'
        image.save(image_path, 'PNG')
        return f'images/avatars/{image_path.name}'
Exemplo n.º 4
0
async def main(client):
    # MESSAGES AND AUTHORS
    with db:
        db.drop_tables([Message, MessageAuthor])
        db.create_tables([Message, MessageAuthor])

    authors = {}
    relevant_channels = (channel for channel in client.juniorguru_guild.text_channels
                         if channel.permissions_for(client.juniorguru_guild.me).read_messages)
    for channel in relevant_channels:
        log.info(f'#{channel.name}')
        async for message in channel.history(limit=None, after=None):
            if message.author.id not in authors:
                # The message.author can be an instance of Member, but it can also be an instance of User,
                # if the author isn't a member of the Discord guild/server anymore. User instances don't
                # have certain properties, hence the getattr() calls below.
                with db:
                    author = MessageAuthor.create(id=message.author.id,
                                                  is_bot=message.author.bot,
                                                  is_member=bool(getattr(message.author, 'joined_at', False)),
                                                  has_avatar=not is_default_avatar(message.author.avatar_url),
                                                  display_name=message.author.display_name,
                                                  mention=message.author.mention,
                                                  joined_at=getattr(message.author, 'joined_at', None),
                                                  roles=get_roles(message.author))
                authors[message.author.id] = author
            with db:
                Message.create(id=message.id,
                               url=message.jump_url,
                               content=message.content,
                               upvotes=count_upvotes(message.reactions),
                               created_at=message.created_at,
                               edited_at=message.edited_at,
                               author=authors[message.author.id],
                               channel_id=channel.id,
                               channel_name=channel.name,
                               channel_mention=channel.mention,
                               type=message.type.name)

    with db:
        messages_count = Message.count()
    log.info(f'Saved {messages_count} messages from {len(authors)} authors')

    # RETURNING MEMBERS
    system_messages_channel = await client.fetch_channel(SYSTEM_MESSAGES_CHANNEL)
    for message in Message.channel_listing(SYSTEM_MESSAGES_CHANNEL):
        if message.type == 'new_member' and message.author.first_seen_at() < message.created_at.date():
            log.info(f'It looks like {message.author.display_name} has returned')
            discord_message = await system_messages_channel.fetch_message(message.id)
            if DISCORD_MUTATIONS_ENABLED:
                await discord_message.add_reaction('👋')
                await discord_message.add_reaction('🔄')
            else:
                log.warning("Skipping Discord mutations, DISCORD_MUTATIONS_ENABLED not set")

    # DIGEST
    week_ago_dt = datetime.utcnow() - timedelta(weeks=1)
    with db:
        last_digest_message = Message.last_bot_message(DIGEST_CHANNEL, '🔥')
    if last_digest_message:
        since_dt = last_digest_message.created_at
        log.info(f"Last digest on {since_dt}")
        if since_dt.date() > week_ago_dt.date():
            log.info(f"Aborting, {since_dt.date()} (last digest) > {week_ago_dt.date()} (week ago)")
            return  # abort
        else:
            log.info(f"About to create digest, {since_dt.date()} (last digest) <= {week_ago_dt.date()} (week ago)")
    else:
        since_dt = week_ago_dt
        log.info(f"Last digest not found, analyzing since {week_ago_dt}")

    channel = await client.fetch_channel(DIGEST_CHANNEL)
    with db:
        messages = Message.digest_listing(since_dt, limit=DIGEST_LIMIT)

    for n, message in enumerate(messages, start=1):
        log.info(f"Digest #{n}: {message.upvotes} votes for {message.author.display_name} in #{message.channel_name}, {message.url}")
    if DISCORD_MUTATIONS_ENABLED:
        content = [
            f"🔥 **{DIGEST_LIMIT} nej příspěvků za uplynulý týden (od {since_dt.day}.{since_dt.month}.)**",
            "",
            "Pokud je něco zajímavé nebo ti to pomohlo, dej tomu palec �, srdí�ko ��, o�i�ka 👀, apod. Oceníš autory a pomůžeš tomu, aby se příspěvek mohl objevit i tady. Někomu, kdo nemá �as procházet všechno, co se v klubu napíše, se může tento přehled hodit.",
        ]
        embed_description = []
        for message in messages:
            embed_description += [
                f"{message.upvotes}× láska pro {message.author.mention} v {message.channel_mention}:",
                f"> {textwrap.shorten(message.content, 200, placeholder='…')}",
                f"[Hop na příspěvek]({message.url})",
                "",
            ]
        await channel.send(content="\n".join(content),
                           embed=Embed(description="\n".join(embed_description)))
    else:
        log.warning("Skipping Discord mutations, DISCORD_MUTATIONS_ENABLED not set")
Exemplo n.º 5
0
def test_is_default_avatar(url, expected):
    assert club.is_default_avatar(url) is expected