Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability graph image to voice-stats #13

Closed
levarar opened this issue Apr 15, 2021 · 2 comments · Fixed by #20
Closed

add ability graph image to voice-stats #13

levarar opened this issue Apr 15, 2021 · 2 comments · Fixed by #20

Comments

@levarar
Copy link

levarar commented Apr 15, 2021

Add graphics, example screen and some code
image

    let embed = new MessageEmbed()
        .setAuthor(message.author.username, message.author.avatarURL({ dynamic: true }))
        .setFooter(`${message.guild.name} root prod.`)
    let day = await tm.getDay(message.guild.id);
    embed.setDescription(`${message.guild.name}** день ${day}** .`);
    embed.setColor("2f3136");

    Stat.aggregate([
        {$project: {Message: 0}},
        { $sort: { AllVoice: -1 } }
    ]).limit(10).exec(async (err, docs) => {
        if (err) return message.reply("произошла ошибка.");
        let users = [], usersToEmbed = [];

        if (docs.length > 0) {
            for (let index = 0; index < docs.length; index++) {
                const doc = docs[index];
                let stat = doc;
                if (!stat) continue;

                if (stat.AllVoice <= 0) continue;

                if (stat.Voice) {
                    let days = Object.keys(stat.Voice);
                    let dataValues = new Array(day).fill(0);
                    days.forEach(_day => {
                        let sum = Object.values(stat.Voice[_day]).reduce((x, y) => x + y, 0);
                        dataValues[_day - 1] = (sum / (1000 * 60));
                    });
                    let user = (await Helper.GetUser(doc.Id));
                    usersToEmbed.push({
                        User: user,
                        Value: dataValues.reduce((x, y) => x + y, 0)
                    });
                    let borderColors = new Array(dataValues.length).fill(getColor(index, "0.8"));
                    let backgroundColors = new Array(dataValues.length).fill(getColor(index, "0.1"))
                    let pointBackgroundColors = new Array(dataValues.length).fill(getColor(index, "1"));
                    let data = {
                        label: `${user.username}`,
                        data: dataValues,
                        backgroundColor: backgroundColors,
                        borderColor: borderColors,
                        pointBackgroundColor: pointBackgroundColors,
                        borderWidth: 1.5,
                    };
                    users.push(data);
                }
            }

            let dataDate = [];
            for (let index = 0; index < day; index++) {
                let date = new Date(Date.now() - (1000 * 60 * 60 * 24 * (day - (index + 1))));
            }

            let buffer = await cm.ImageFromData({
                width: 600,
                height: 290,
                type: 'line',
                data: {
                    labels: [].concat(dataDate),
                    datasets: users
                },
                options: {
                    legend: {
                        labels: {
                            fontColor: '#ffffff',
                            fontSize: 20
                        }
                    }
                }
            });
            embed.addField(`Топ по голосовой активности`, usersToEmbed.map((val, index) => `\`${index + 1}.\` ${val.User}(${val.User.username}): \`${moment.duration(val.Value * (1000 * 60)).format("H [ч., ] m [мин.]")}\``).join("\n"))
            embed.setImage("attachment://Graph.png");
            let attachment = new MessageAttachment(buffer, "Graph.png");

            message.channel.csend({
                embeds: [embed],
                files: [attachment]
            });
        }
        else {
            embed.addField("Нет данных.", "** **");
            return message.csend(embed);
        }
    });
}

const colors = [
    'rgba(240, 255, 0, <f>)',
    'rgba(147, 255, 0, <f>)',
    'rgba(0, 255, 4, <f>)',
    'rgba(0, 255, 182, <f>)',
    'rgba(0, 240, 255, <f>)',
    'rgba(0, 124, 255, <f>)',
    'rgba(81, 0, 255, <f>)',
    'rgba(182, 0, 255, <f>)',
    'rgba(255, 0, 220, <f>)',
    'rgba(255, 0, 85, <f>)',
]
function getColor(index, x) {
    let color = colors[index].replace("<f>", x);
    return color;
}

@Lebyy
Copy link
Collaborator

Lebyy commented Apr 16, 2021

Interesting I will see what I can do! Thanks for providing the code it helps alot

@Lebyy Lebyy linked a pull request Jul 10, 2021 that will close this issue
9 tasks
@Lebyy
Copy link
Collaborator

Lebyy commented Jul 10, 2021

Might add this in the 2.0.0 release since it will have a system dedicated to levels / xp.

@Lebyy Lebyy closed this as completed in #20 Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants