Beispiel #1
0
    async def on_member_unban(self, guild: discord.Guild, user: discord.User):
        """Called when a member is unbanned from a guild"""
        now = dt.datetime.utcnow()
        log_message = "{1.name}#{1.discriminator} (ID:{1.id}) was unbanned from {0.name}".format(
            guild, user)
        bot_member = guild.me  # type: discord.Member

        embed = discord.Embed(description="Unbanned")
        icon_url = get_user_avatar(user)
        embed.set_author(name="{0.name}#{0.discriminator}".format(user),
                         icon_url=icon_url)
        embed.timestamp = now
        embed.colour = discord.Colour(0xff9000)

        if bot_member.guild_permissions.view_audit_log:
            async for entry in guild.audit_logs(
                    limit=10,
                    reverse=False,
                    action=discord.AuditLogAction.unban,
                    after=now -
                    dt.timedelta(0, 5)):  # type: discord.AuditLogEntry
                if abs((entry.created_at - now).total_seconds()) >= 5:
                    # After is broken in the API, so we must check if entry is too old.
                    break
                if entry.target.id == user.id:
                    icon_url = get_user_avatar(entry.user)
                    embed.set_footer(text="{0.name}#{0.discriminator}".format(
                        entry.user),
                                     icon_url=icon_url)
                    log_message += f"by {entry.user.name}"
                    break
        log.warning(log_message)
        await self.send_log_message(guild, embed=embed)
Beispiel #2
0
    async def on_member_remove(self, member: discord.Member):
        """Called when a member leaves or is kicked from a guild."""
        now = dt.datetime.utcnow()
        self.members[member.id].remove(member.guild.id)
        bot_member = member.guild.me  # type: discord.Member

        embed = discord.Embed(description="Left the server or was kicked")
        icon_url = get_user_avatar(member)
        embed.set_author(name="{0.name}#{0.discriminator}".format(member),
                         icon_url=icon_url)
        embed.timestamp = now
        embed.colour = discord.Colour(0xffff00)

        # If bot can see audit log, he can see if it was a kick or member left on it's own
        if bot_member.guild_permissions.view_audit_log:
            async for entry in member.guild.audit_logs(
                    limit=20,
                    reverse=False,
                    action=discord.AuditLogAction.kick,
                    after=now -
                    dt.timedelta(0, 5)):  # type: discord.AuditLogEntry
                if abs((entry.created_at - now).total_seconds()) >= 5:
                    # After is broken in the API, so we must check if entry is too old.
                    break
                if entry.target.id == member.id:
                    embed.description = "Kicked"
                    icon_url = get_user_avatar(entry.user)
                    embed.set_footer(text="{0.name}#{0.discriminator}".format(
                        entry.user),
                                     icon_url=icon_url)
                    embed.timestamp = entry.created_at
                    embed.colour = discord.Colour(0xff0000)
                    if entry.reason:
                        embed.description += f"\n**Reason:** {entry.reason}"
                    log.info(
                        "{0.display_name} (ID:{0.id}) was kicked from {0.guild.name} by {1.display_name}"
                        .format(member, entry.user))
                    await self.send_log_message(member.guild, embed=embed)
                    return
            embed.description = "Left the server"
            log.info("{0.display_name} (ID:{0.id}) left {0.guild.name}".format(
                member))
            await self.send_log_message(member.guild, embed=embed)
            return
        # Otherwise, we are not certain
        log.info(
            "{0.display_name} (ID:{0.id}) left or was kicked from {0.guild.name}"
            .format(member))
        await self.send_log_message(member.guild, embed=embed)
Beispiel #3
0
    async def on_member_update(self, before: discord.Member,
                               after: discord.Member):
        """Called every time a member is updated"""
        now = dt.datetime.utcnow()
        guild = after.guild
        bot_member = guild.me

        embed = discord.Embed(description=f"{after.mention}: ")
        embed.set_author(name=f"{after.name}#{after.discriminator}",
                         icon_url=get_user_avatar(after))
        embed.timestamp = now
        embed.colour = discord.Colour.blue()
        changes = True
        if f"{before.name}#{before.discriminator}" != f"{after.name}#{after.discriminator}":
            embed.description += "Name changed from **{0.name}#{0.discriminator}** to **{1.name}#{1.discriminator}**."\
                .format(before, after)
        elif before.nick != after.nick:
            if before.nick is None:
                embed.description += f"Nickname set to **{after.nick}**"
            elif after.nick is None:
                embed.description += f"Nickname **{before.nick}** deleted"
            else:
                embed.description += f"Nickname changed from **{before.nick}** to **{after.nick}**"
            if bot_member.guild_permissions.view_audit_log:
                async for entry in guild.audit_logs(
                        limit=10,
                        reverse=False,
                        action=discord.AuditLogAction.member_update,
                        after=now -
                        dt.timedelta(0, 5)):  # type: discord.AuditLogEntry
                    if abs((entry.created_at - now).total_seconds()) >= 5:
                        # After is broken in the API, so we must check if entry is too old.
                        break
                    if entry.target.id == after.id:
                        # If the user changed their own nickname, no need to specify
                        if entry.user.id == after.id:
                            break
                        icon_url = get_user_avatar(entry.user)
                        embed.set_footer(
                            text="{0.name}#{0.discriminator}".format(
                                entry.user),
                            icon_url=icon_url)
                        break
        else:
            changes = False
        if changes:
            await self.send_log_message(after.guild, embed=embed)
        return
Beispiel #4
0
    async def on_member_join(self, member: discord.Member):
        """ Called when a member joins a guild (server) the bot is in."""
        log.info("{0.display_name} (ID: {0.id}) joined {0.guild.name}".format(
            member))
        # Updating member list
        if member.id in self.members:
            self.members[member.id].append(member.guild.id)
        else:
            self.members[member.id] = [member.guild.id]

        # No welcome message for lite servers and servers not tracking worlds
        if member.guild.id in config.lite_servers or tracked_worlds.get(
                member.guild.id) is None:
            return

        server_welcome = get_server_property("welcome", member.guild.id, "")
        pm = (config.welcome_pm + "\n" + server_welcome).format(
            user=member,
            server=member.guild,
            bot=self.user,
            owner=member.guild.owner)

        embed = discord.Embed(description="{0.mention} joined.".format(member))
        icon_url = get_user_avatar(member)
        embed.colour = discord.Colour.green()
        embed.set_author(name="{0.name}#{0.discriminator}".format(member),
                         icon_url=icon_url)
        embed.timestamp = dt.datetime.utcnow()

        # Check if user already has characters registered and announce them on log_channel
        # This could be because he rejoined the server or is in another server tracking the same worlds
        world = tracked_worlds.get(member.guild.id)
        if world is not None:
            c = userDatabase.cursor()
            try:
                c.execute(
                    "SELECT name, vocation, ABS(level) as level, guild "
                    "FROM chars WHERE user_id = ? and world = ?", (
                        member.id,
                        world,
                    ))
                results = c.fetchall()
                if len(results) > 0:
                    pm += "\nYou already have these characters in {0} registered to you: {1}"\
                        .format(world, join_list([r["name"] for r in results], ", ", " and "))
                    characters = [
                        "\u2023 {name} - Level {level} {voc} - **{guild}**".
                        format(**c, voc=get_voc_abb_and_emoji(c["vocation"]))
                        for c in results
                    ]
                    embed.add_field(name="Registered characters",
                                    value="\n".join(characters))
            finally:
                c.close()

        await self.send_log_message(member.guild, embed=embed)
        await member.send(pm)
Beispiel #5
0
    async def on_guild_update(self, before: discord.Guild,
                              after: discord.Guild):
        """Called every time a guild is updated"""
        now = dt.datetime.utcnow()
        guild = after
        bot_member = guild.me

        embed = discord.Embed()
        embed.set_author(name=after.name, icon_url=after.icon_url)
        embed.timestamp = now
        embed.colour = discord.Colour(value=0x9b3ee8)

        changes = True
        if before.name != after.name:
            embed.description = "Name changed from **{0.name}** to **{1.name}**".format(
                before, after)
        elif before.region != after.region:
            embed.description = "Region changed from **{0}** to **{1}**".format(
                get_region_string(before.region),
                get_region_string(after.region))
        elif before.icon_url != after.icon_url:
            embed.description = "Icon changed"
            embed.set_thumbnail(url=after.icon_url)
        elif before.owner_id != after.owner_id:
            embed.description = f"Ownership transferred to {after.owner.mention}"
        else:
            changes = False
        if changes:
            if bot_member.guild_permissions.view_audit_log:
                async for entry in guild.audit_logs(
                        limit=1,
                        reverse=False,
                        action=discord.AuditLogAction.guild_update,
                        after=now -
                        dt.timedelta(0, 5)):  # type: discord.AuditLogEntry:
                    icon_url = get_user_avatar(entry.user)
                    embed.set_footer(text="{0.name}#{0.discriminator}".format(
                        entry.user),
                                     icon_url=icon_url)
                    break
            await self.send_log_message(after, embed=embed)