<commit_before>import asyncio
from plugin_manager import BasePlugin
from plugins.channel_manager import ChannelNotFoundError
from utils import split_message, Command, respond


class DiscordLogger(BasePlugin):
    name = "logger"
    default_config = {
        "default": {
            "log_events": [
            ]
        }
    }

    async def activate(self):
        self.log_items = {}
        self.active = True
        asyncio.ensure_future(self._dump_logs())

    async def deactivate(self):
        self.active = False

    async def _dump_logs(self):
        while self.active:
            await asyncio.sleep(1)
            if not self.active:
                return
            for guild in self.client.guilds:
                gid = str(guild.id)
                try:
                    logchan = self.plugins.channel_manager.get_channel(guild, "logs")
                except ChannelNotFoundError:
                    continue
                except AttributeError:
                    self.logger.error("Failed to get channel.")
                    return
                if gid in self.log_items and self.log_items[gid]:
                    logs = "\n".join(self.log_items[gid])
                    for msg in split_message(logs, splitter="\n"):
                        await logchan.send(msg)
                    self.log_items[gid] = []

    async def on_message_delete(self, msg):
        gid = str(msg.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "message_delete" not in self.plugin_config[gid].log_events and msg.author != self.client.user:
            uname = str(msg.author)
            contents = msg.clean_content
            msgtime = msg.created_at.strftime("%Y-%m-%d @ %H:%M:%S")
            attaches = ""
            links = ""
            if msg.attachments:
                links = ", ".join([x.url for x in msg.attachments])
                attaches = f"\n**Attachments:** `{links}`"
            self.logger.debug(f"User {uname}'s message at {msgtime} in {msg.channel.name} was deleted.\n"
                              f"Contents: {contents}\nAttachments: {links}")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname}'s message at `{msgtime}` in {msg.channel.mention} was"
                                       f" deleted. ANALYSIS: Contents:**\n{contents}{attaches}")

    async def on_message_edit(self, before, after):
        gid = str(after.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "message_edit" not in self.plugin_config[gid].log_events and after.author != self.client.user:
            uname = str(after.author)
            old_contents = before.clean_content
            contents = after.clean_content
            msgtime = after.created_at.strftime("%Y-%m-%d @ %H:%M:%S")
            if old_contents == contents:
                return
            self.logger.debug(f"User {uname} edited their message at {msgtime} in {after.channel.name}. \n"
                              f"Old contents: {old_contents}\nNew contents: {contents}")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname} edited their message at `{msgtime}` in "
                                       f"{after.channel.mention}. ANALYSIS:**\n"
                                       f"**Old contents:** {old_contents}\n**New contents:** {contents}")

    async def on_member_join(self, member):
        gid = str(member.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_join" not in self.plugin_config[gid].log_events:
            uname = str(member)
            self.logger.debug(f"User {uname} joined {member.guild.name}.")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**NEW USER DETECTED: {uname}.**")

    async def on_member_remove(self, member):
        gid = str(member.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_remove" not in self.plugin_config[gid].log_events:
            uname = str(member)
            self.logger.debug(f"User {uname} left {member.guild.name}.")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname} has left the server.**")

    async def on_member_update(self, before, after):
        gid = str(after.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_update" not in self.plugin_config[gid].log_events:
            uname = str(after)
            t_str = ""
            if before.avatar != after.avatar:
                t_str = f"{t_str}`New avatar : `{after.avatar_url}\n"
            if before.nick != after.nick:
                t_str = f"{t_str}`Old nick: `{before.nick}\n`New nick : `{after.nick}\n"
            if before.roles != after.roles:
                o_role = ", ".join([str(x) for x in before.roles])
                n_role = ", ".join([str(x) for x in after.roles])
                t_str = f"{t_str}**Old roles :**```[ {o_role.replace('@','')} ]```\n" \
                        f"**New roles :**```[ {n_role.replace('@','')} ]```\n"
            if t_str == "":
                return
            self.logger.debug(f"User {uname} was modified:\n{t_str}".replace("```", " ").replace("**", ""))<commit_msg>on_member_update now logs username changes<commit_after>import asyncio
from plugin_manager import BasePlugin
from plugins.channel_manager import ChannelNotFoundError
from utils import split_message, Command, respond


class DiscordLogger(BasePlugin):
    name = "logger"
    default_config = {
        "default": {
            "log_events": [
            ]
        }
    }

    async def activate(self):
        self.log_items = {}
        self.active = True
        asyncio.ensure_future(self._dump_logs())

    async def deactivate(self):
        self.active = False

    async def _dump_logs(self):
        while self.active:
            await asyncio.sleep(1)
            if not self.active:
                return
            for guild in self.client.guilds:
                gid = str(guild.id)
                try:
                    logchan = self.plugins.channel_manager.get_channel(guild, "logs")
                except ChannelNotFoundError:
                    continue
                except AttributeError:
                    self.logger.error("Failed to get channel.")
                    return
                if gid in self.log_items and self.log_items[gid]:
                    logs = "\n".join(self.log_items[gid])
                    for msg in split_message(logs, splitter="\n"):
                        await logchan.send(msg)
                    self.log_items[gid] = []

    async def on_message_delete(self, msg):
        gid = str(msg.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "message_delete" not in self.plugin_config[gid].log_events and msg.author != self.client.user:
            uname = str(msg.author)
            contents = msg.clean_content
            msgtime = msg.created_at.strftime("%Y-%m-%d @ %H:%M:%S")
            attaches = ""
            links = ""
            if msg.attachments:
                links = ", ".join([x.url for x in msg.attachments])
                attaches = f"\n**Attachments:** `{links}`"
            self.logger.debug(f"User {uname}'s message at {msgtime} in {msg.channel.name} was deleted.\n"
                              f"Contents: {contents}\nAttachments: {links}")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname}'s message at `{msgtime}` in {msg.channel.mention} was"
                                       f" deleted. ANALYSIS: Contents:**\n{contents}{attaches}")

    async def on_message_edit(self, before, after):
        gid = str(after.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "message_edit" not in self.plugin_config[gid].log_events and after.author != self.client.user:
            uname = str(after.author)
            old_contents = before.clean_content
            contents = after.clean_content
            msgtime = after.created_at.strftime("%Y-%m-%d @ %H:%M:%S")
            if old_contents == contents:
                return
            self.logger.debug(f"User {uname} edited their message at {msgtime} in {after.channel.name}. \n"
                              f"Old contents: {old_contents}\nNew contents: {contents}")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname} edited their message at `{msgtime}` in "
                                       f"{after.channel.mention}. ANALYSIS:**\n"
                                       f"**Old contents:** {old_contents}\n**New contents:** {contents}")

    async def on_member_join(self, member):
        gid = str(member.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_join" not in self.plugin_config[gid].log_events:
            uname = str(member)
            self.logger.debug(f"User {uname} joined {member.guild.name}.")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**NEW USER DETECTED: {uname}.**")

    async def on_member_remove(self, member):
        gid = str(member.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_remove" not in self.plugin_config[gid].log_events:
            uname = str(member)
            self.logger.debug(f"User {uname} left {member.guild.name}.")
            if gid not in self.log_items:
                self.log_items[gid] = []
            self.log_items[gid].append(f"**WARNING: User {uname} has left the server.**")

    async def on_member_update(self, before, after):
        gid = str(after.guild.id)
        if gid not in self.plugin_config:
            self.plugin_config[gid] = self.plugin_config["default"]
        if "member_update" not in self.plugin_config[gid].log_events:
            uname = str(after)
            t_str = ""
            if before.name != after.name or before.discriminator != after.discriminator:
                t_str = f"{t_str}`Old username : `{str(before)}\n`New username : `{uname}\n"
            if before.avatar != after.avatar:
                t_str = f"{t_str}`New avatar : `{after.avatar_url}\n"
            if before.nick != after.nick:
                t_str = f"{t_str}`Old nick: `{before.nick}\n`New nick : `{after.nick}\n"
            if before.roles != after.roles:
                o_role = ", ".join([str(x) for x in before.roles])
                n_role = ", ".join([str(x) for x in after.roles])
                t_str = f"{t_str}**Old roles :**```[ {o_role.replace('@','')} ]```\n" \
                        f"**New roles :**```[ {n_role.replace('@','')} ]```\n"
            if t_str == "":
                return
            self.logger.debug(f"User {uname} was modified:\n{t_str}".replace("```", " ").replace("**", ""))
{'fraction_non_alphanumeric': 0.128840098715003, 'fraction_numerical': 0.00017019828099736192, 'mean_word_length': 1.8832188420019627, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575331', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3462, 'n_words': 750}