<commit_before>import re
from datetime import datetime

import discord
import peewee
from discord.http import Route

from bot import Command, utils, categories
from discord import Embed

from bot.libs.configuration import BaseModel
from bot.utils import deltatime_to_str

modlog_types = ['user_join', 'user_leave', 'message_delete', 'username', 'nick', 'invite_filter']


class ModLog(Command):
    __author__ = 'makzk'
    __version__ = '1.0.0'
    rx_channel = re.compile('^<#[0-9]+>$')
    chan_config_name = 'join_send_channel'

    def __init__(self, bot):
        super().__init__(bot)

    async def on_member_join(self, member):
        await self.bot.send_modlog(
            member.server, '$[modlog-new-user]',
            embed=ModLog.gen_embed(member, more=True), locales={'mid': member.id}, logtype='user_join')

    async def on_member_remove(self, member):
        dt = deltatime_to_str(datetime.now() - member.joined_at)
        await self.bot.send_modlog(member.server, '$[modlog-user-left]',
                                   locales={'mid': member.id, 'username': str(member), 'dt': dt}, logtype='user_leave')

    async def on_message_delete(self, message):
        if message.server is None or message.author.id == self.bot.user.id:
            return

        footer = '$[modlog-msg-sent]: ' + utils.format_date(message.timestamp)
        if message.edited_timestamp is not None:
            footer += ', $[modlog-msg-edited]: ' + utils.format_date(message.edited_timestamp)

        embed = Embed(description='($[modlog-no-text])' if message.content == '' else message.content)
        embed.set_footer(text=footer)
        if len(message.attachments) > 0:
            with_img = False
            if 'width' in message.attachments[0] is not None:
                fn_value = '[{}]({})'.format(message.attachments[0]['filename'], message.attachments[0]['url'])
                embed.set_image(url=message.attachments[0]['url'])
                embed.add_field(name='$[modlog-file-name]', value=fn_value)
                with_img = True

            if with_img and len(message.attachments) > 1 or not with_img:
                i = 1 if with_img else 0
                x = ['[{}]({})'.format(f['filename'], f['url']) for f in message.attachments[i:]]
                t = [
                        ['$[modlog-attatched]', '$[modlog-attached-other]'],
                        ['$[modlog-attached-single]', '$[modlog-attached-other-single]']
                    ][int(len(x) == 1)][i]
                embed.add_field(name=t, value=', '.join(x))

        msg = '$[modlog-user-deleted-msg]'
        locales = {'username': message.author.display_name, 'channel_name': message.channel.mention}
        if message.id in self.bot.deleted_messages:
            if message.id in self.bot.deleted_messages_nolog:
                self.bot.deleted_messages_nolog.remove(message.id)
                return
            else:
                msg = '$[modlog-bot-deleted-msg]'
        else:
            try:
                last = await self.get_last_alog(message.server.id)
                if last is not None and last['action_type'] == 72 and \
                        last['options']['channel_id'] == message.channel.id and last['target_id'] == message.author.id:
                    who = last['user_id']
                    if who == self.bot.user.id:
                        msg = '$[modlog-bot-deleted-msg]'
                    else:
                        u = message.server.get_member(who)
                        u = '<@' + who + '>' if u is None else u.display_name

                        locales['deleter_name'] = u
                        msg = '$[modlog-user-deleted-other]'
            except discord.Forbidden:
                msg = '$[modlog-somehow-deleted-msg]'

        await self.bot.send_modlog(message.server, msg, embed=embed, locales=locales, logtype='message_delete')

    async def on_member_update(self, before, after):
        server = after.server

        if before.name != after.name:
            if after.display_name != after.name:
                await self.bot.send_modlog(
                    server, '$[modlog-username-changed-nick]',
                    locales={'prev_name': before.name, 'new_name': after.name, 'nick': after.display_name},
                    logtype='username')
            else:
                await self.bot.send_modlog(
                    server, '$[modlog-username-changed]',
                    locales={'prev_name': before.name, 'new_name': after.name}, logtype='username')

        if (before.nick or after.nick) and before.nick != after.nick:
            try:
                alog = await self.get_last_alog(after.server.id)
            except discord.Forbidden:
                alog = None

            if alog['action_type'] == 24 and len(alog['changes']) and alog['changes'][0]['key'] == 'nick':
                if alog['user_id'] == str(after.id):
                    by = after
                else:
                    by = after.server.get_member(alog['user_id'])
            else:
                by = None

            prev_nick = before.nick or '$[modlog-nick-none]'
            after_nick = after.nick or '$[modlog-nick-none]'
            locales = {'author': by.name, 'target': after.name, 'previous': prev_nick, 'after': after_nick}

            if by is None:
                # default entry, no author
                await self.bot.send_modlog(server, '$[modlog-nick-other]', logtype='nick', locales=locales)
            elif by.id == after.id:
                # self updated
                if by.id == self.bot.user.id:<commit_msg>Another fix for incorrectly 403 auditlog handle<commit_after>import re
from datetime import datetime

import discord
import peewee
from discord.http import Route

from bot import Command, utils, categories
from discord import Embed

from bot.libs.configuration import BaseModel
from bot.utils import deltatime_to_str

modlog_types = ['user_join', 'user_leave', 'message_delete', 'username', 'nick', 'invite_filter']


class ModLog(Command):
    __author__ = 'makzk'
    __version__ = '1.0.0'
    rx_channel = re.compile('^<#[0-9]+>$')
    chan_config_name = 'join_send_channel'

    def __init__(self, bot):
        super().__init__(bot)

    async def on_member_join(self, member):
        await self.bot.send_modlog(
            member.server, '$[modlog-new-user]',
            embed=ModLog.gen_embed(member, more=True), locales={'mid': member.id}, logtype='user_join')

    async def on_member_remove(self, member):
        dt = deltatime_to_str(datetime.now() - member.joined_at)
        await self.bot.send_modlog(member.server, '$[modlog-user-left]',
                                   locales={'mid': member.id, 'username': str(member), 'dt': dt}, logtype='user_leave')

    async def on_message_delete(self, message):
        if message.server is None or message.author.id == self.bot.user.id:
            return

        footer = '$[modlog-msg-sent]: ' + utils.format_date(message.timestamp)
        if message.edited_timestamp is not None:
            footer += ', $[modlog-msg-edited]: ' + utils.format_date(message.edited_timestamp)

        embed = Embed(description='($[modlog-no-text])' if message.content == '' else message.content)
        embed.set_footer(text=footer)
        if len(message.attachments) > 0:
            with_img = False
            if 'width' in message.attachments[0] is not None:
                fn_value = '[{}]({})'.format(message.attachments[0]['filename'], message.attachments[0]['url'])
                embed.set_image(url=message.attachments[0]['url'])
                embed.add_field(name='$[modlog-file-name]', value=fn_value)
                with_img = True

            if with_img and len(message.attachments) > 1 or not with_img:
                i = 1 if with_img else 0
                x = ['[{}]({})'.format(f['filename'], f['url']) for f in message.attachments[i:]]
                t = [
                        ['$[modlog-attatched]', '$[modlog-attached-other]'],
                        ['$[modlog-attached-single]', '$[modlog-attached-other-single]']
                    ][int(len(x) == 1)][i]
                embed.add_field(name=t, value=', '.join(x))

        msg = '$[modlog-user-deleted-msg]'
        locales = {'username': message.author.display_name, 'channel_name': message.channel.mention}
        if message.id in self.bot.deleted_messages:
            if message.id in self.bot.deleted_messages_nolog:
                self.bot.deleted_messages_nolog.remove(message.id)
                return
            else:
                msg = '$[modlog-bot-deleted-msg]'
        else:
            try:
                last = await self.get_last_alog(message.server.id)
                if last is not None and last['action_type'] == 72 and \
                        last['options']['channel_id'] == message.channel.id and last['target_id'] == message.author.id:
                    who = last['user_id']
                    if who == self.bot.user.id:
                        msg = '$[modlog-bot-deleted-msg]'
                    else:
                        u = message.server.get_member(who)
                        u = '<@' + who + '>' if u is None else u.display_name

                        locales['deleter_name'] = u
                        msg = '$[modlog-user-deleted-other]'
            except discord.Forbidden:
                msg = '$[modlog-somehow-deleted-msg]'

        await self.bot.send_modlog(message.server, msg, embed=embed, locales=locales, logtype='message_delete')

    async def on_member_update(self, before, after):
        server = after.server

        if before.name != after.name:
            if after.display_name != after.name:
                await self.bot.send_modlog(
                    server, '$[modlog-username-changed-nick]',
                    locales={'prev_name': before.name, 'new_name': after.name, 'nick': after.display_name},
                    logtype='username')
            else:
                await self.bot.send_modlog(
                    server, '$[modlog-username-changed]',
                    locales={'prev_name': before.name, 'new_name': after.name}, logtype='username')

        if (before.nick or after.nick) and before.nick != after.nick:
            try:
                alog = await self.get_last_alog(after.server.id)
            except discord.Forbidden:
                alog = None

            if alog is not None and alog['action_type'] == 24 and len(alog['changes']) \
                    and alog['changes'][0]['key'] == 'nick':
                if alog['user_id'] == str(after.id):
                    by = after
                else:
                    by = after.server.get_member(alog['user_id'])
            else:
                by = None

            prev_nick = before.nick or '$[modlog-nick-none]'
            after_nick = after.nick or '$[modlog-nick-none]'
            locales = {'author': by.name, 'target': after.name, 'previous': prev_nick, 'after': after_nick}

            if by is None:
                # default entry, no author
                await self.bot.send_modlog(server, '$[modlog-nick-other]', logtype='nick', locales=locales)
            elif by.id == after.id:
                # self updated
                if by.id == self.bot.user.id:
{'fraction_non_alphanumeric': 0.1344337540824433, 'fraction_numerical': 0.0036190308058963723, 'mean_word_length': 1.981578947368421, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1325771', 'n_tokens_mistral': 3429, 'n_tokens_neox': 3284, 'n_words': 692}