Exemplo n.º 1
0
async def get_inactive_members(context, progress_report=True):
    """Returns a list of inactive members."""
    senders = []
    inactive_members = []
    now = datetime.datetime.now()
    days_threshold = Settings.inactive_threshold(context.guild.id)
    time_boundary = now - datetime.timedelta(days=days_threshold)
    progress_msg = None
    include_reactions = Settings.include_reactions_inactivity(context.guild.id)

    included_channels = context.guild.text_channels
    channel_count = len(included_channels)

    if progress_report:
        progress_msg = await utils.say(context.channel, content=f"Scanning {channel_count} channels for inactive members.")
    
    for i, channel in enumerate(included_channels):
        try:
            async for m in channel.history(limit=None, after=(now - datetime.timedelta(days=days_threshold)), oldest_first=False):
                if m.author not in senders:
                    senders.append(m.author)

                if include_reactions:
                    for r in m.reactions:
                        async for u in r.users():
                            if u not in senders:
                                senders.append(u)

        except discord.errors.Forbidden:
            print(f"Can't access {channel.name}")
        else:
            if progress_msg:
                await progress_msg.edit(content=f"Scanned {i}/{channel_count} channels for inactive members.")
    
    if progress_msg:
        await progress_msg.edit(content=f"Scanned {channel_count} channels for inactive members.")
    
    results = [
        u for u in context.guild.members
        if u not in senders and u.joined_at < time_boundary and not u.bot
    ]
    db_inactive_members = [] # database.get_all_inactive_members(context.guild.id)

    for member in results:
        if member.id in db_inactive_members:
            m = db_inactive_members[member.id]
            m.user = member
            inactive_members.append(m)
        else:
            inactive_members.append(database.InactiveMember(context.guild.id, member.id, user=member))
    # update_inactive_members(db_inactive_members, {m.member_id: m for m in inactive_members})

    return inactive_members
Exemplo n.º 2
0
    async def inactivelist(self, context):
        cmd_settings = Settings.command_settings(context.command.name, context.guild.id)
        if not cmd_settings.get("enabled"):
            return

        inactive_members = await get_inactive_members(context)
        inactive_list = []
        for i in inactive_members:
            last_notified = i.last_notified.strftime(" (%b %d, %Y %Z)") if i.last_notified else ""
            entry = f"{'**EXEMPT** ' if i.is_exempt else ''}{i.user.mention} [{i.user.display_name}]{last_notified}"
            inactive_list.append(entry)

        days_threshold = Settings.inactive_threshold(context.guild.id)
        embeds = utils.split_embeds(
            title=f"Inactive Members ({days_threshold}+ days since last message)",
            description="\n".join(inactive_list)
        )

        for i, embed in enumerate(embeds):
            if i < len(embeds) - 1:
                await utils.say(context.channel, embed=embed)
            else:
                # Final message
                inactivity_message = Settings.inactive_message(context.guild.id)
                if inactivity_message:
                    embed.set_footer(text="React 📧 below to notify them")
                report = await utils.say(context.channel, content=f"{context.author.mention}", embed=embed)
        
                if inactivity_message:
                    await report.add_reaction("📧")
                    def check(reaction, user):
                        return reaction.message.id == report.id and user.id == context.message.author.id \
                        and str(reaction.emoji) == "📧"

                    try:
                        await self.bot.wait_for("reaction_add", timeout=600, check=check)
                    except asyncio.TimeoutError:
                        embed.set_footer(text=discord.Embed.Empty)
                        await report.edit(embed=embed)
                        await report.clear_reactions()
                    else:
                        await self.notify_inactive_members(context, inactive_members)
        
        return CommandStatus.COMPLETED