<commit_before>import Plugin from "./../Plugin";
import Util from "../Util";

export default class UserStats extends Plugin {
    static get plugin() {
        return {
            name: "UserStats",
            description: "Get user stats on message count.",
            help: "Enter /userstats to get statistics.",
            type: Plugin.Type.PROXY,
            needs: {
                database: true
            }
        };
    }

    proxy(eventName, message) {
        if (!message.from.username) return;
        const chatId = message.chat.id;
        const userId = message.from.id;
        if (!this.db["stat" + chatId]) {
            this.db["stat" + chatId] = {};
            this.db["stat" + chatId].totalMessageCount = 0;
        }
        if (!this.db["stat" + chatId][userId]) {
            this.db["stat" + chatId][userId] = {
                userId,
                username: message.from.username,
                messageCount: 0
            };
        }
        this.db["stat" + chatId][userId].messageCount++;
        this.db["stat" + chatId].totalMessageCount++;
        return Promise.resolve(message);
    }

    onText(message, reply) {
        const parts = Util.parseCommand(message.text, "userstats");
        if (!parts) return;
        let text = `Total Messages:\n\n`;
        const statsObject = this.db["stat" + message.chat.id];
        const totalCount = statsObject.totalMessageCount;
        const userList = Object.keys(statsObject)
            .map(item => {
                return statsObject[item];
            }).filter(item => {
                return typeof item === "object";
            }).sort((a, b) => {
                return b.messageCount - a.messageCount;
            });
        for (let user of userList) {
            if (user) {
                const percentage = (user.messageCount / totalCount * 100).toFixed(4);
                text += `${user.username} [${user.userId}]: ${user.messageCount} (${percentage}%)\n`;
            }
        }
        reply({type: 'text', text});
    }
}<commit_msg>Applying fixes from second code review.<commit_after>import Plugin from "./../Plugin";
import Util from "../Util";

export default class UserStats extends Plugin {
    static get plugin() {
        return {
            name: "UserStats",
            description: "Get user stats on message count.",
            help: "Enter /userstats to get statistics.",
            type: Plugin.Type.PROXY,
            needs: {
                database: true
            }
        };
    }

    proxy(eventName, message) {
        if (!message.from.username) return;
        const chatId = message.chat.id;
        const userId = message.from.id;
        if (!this.db["stat" + chatId]) {
            this.db["stat" + chatId] = {};
            this.db["stat" + chatId].totalMessageCount = 0;
        }
        if (!this.db["stat" + chatId][userId]) {
            this.db["stat" + chatId][userId] = {
                userId,
                username: message.from.username,
                messageCount: 0
            };
        }
        this.db["stat" + chatId][userId].messageCount++;
        this.db["stat" + chatId].totalMessageCount++;
        return Promise.resolve(message);
    }

    onText(message, reply) {
        const parts = Util.parseCommand(message.text, "userstats");
        if (!parts) return;
        let text = `Total Messages:\n\n`;
        const statsObject = this.db["stat" + message.chat.id];
        const totalCount = statsObject.totalMessageCount;
        const userList = Object.keys(statsObject)
            .map(item => statsObject[item])
            .filter(item => typeof item === "object")
            .sort((a, b) => b.messageCount - a.messageCount);

        for (let user of userList) {
            const percentage = (user.messageCount / totalCount * 100).toFixed(4);
            text += `${user.username} [${user.userId}]: ${user.messageCount} (${percentage}%)\n`;
        }
        reply({type: 'text', text});
    }
}
{'fraction_non_alphanumeric': 0.14146463384153962, 'fraction_numerical': 0.0029992501874531365, 'mean_word_length': 1.7810979847116053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194046', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1102, 'n_words': 258}