CPU assign to Glitch

So, for some reason, the last few days when I was doing the command to check my bot’s statistics, I kept seeing this, which wasn’t really good for me: image
And my bot keeps getting offline for no reason and I’ve done a lot of things to keep the bot online 24/7.

From a Discord Bot Developer and Glitch User,
TehPig_YT

What you have provided is no where near useful. Could you provide us with a project link? Note make sure its public so we can see it, before doing that, make sure all sensitive things like bot tokens are in your ENV

1 Like

The only thing that I can provide is the whole command code, so here it is:

const Discord = require("discord.js");
const { version } = require("discord.js");
let cpuStat = require("cpu-stat");
const fs = require("fs");
const config = require("../botconfig.json");
const db = require("quick.db");

module.exports.run = async (bot, message, args, prefix, catchErr) => {
     
    let cmdz = await db.fetch(`cmds`);
  
    const os = require('os');
    const arch = os.arch()
    const used = process.memoryUsage().heapUsed / 1024 / 1024;

    let totalSeconds = process.uptime();
    let realTotalSecs = Math.floor(totalSeconds % 60);
    let days = Math.floor((totalSeconds % 31536000) / 86400);
    let hours = Math.floor((totalSeconds / 3600) % 24);
    let mins = Math.floor((totalSeconds / 60) % 60);
  
    cpuStat.usagePercent(function(err, percent, seconds) {
    if (err) {
      return catchErr(err, message);
    }
      
    fs.readdir("./commands/", (err, files) => {
  
    let jsfile = files.filter(f => f.split(".").pop() === "js");
    if(jsfile.length <= 0){
      return;
    }

    var ping = bot.ping
    let embed = new Discord.RichEmbed()
    .setAuthor(`${bot.user.username}'s Bot Stats and Info`, bot.user.avatarURL)
    .setThumbnail(bot.user.avatarURL)
    .setColor("RANDOM")
    .addField("Uptime", `Days: ${days} | Hours: ${hours} | Minutes: ${mins} | Seconds: ${realTotalSecs}`, true)
    .addField("CPU Usage", `${percent.toFixed(2)}%`, true)
    .addField("Memory Usage", `${Math.round(used * 100) / 100} MB`, true)
    .addField("Server Count", bot.guilds.size, true)
    .addField("Member Count", bot.users.size, true)
    .addField("Channel Count", bot.channels.size, true)
    .addField("Node", process.version, true)
    .addField("Discord.js", `v${version}`, true)
    .addField("Commands", jsfile.length + 8, true)
    .addField("Platform", os.platform, true)
    .addField("ARCH", arch, true)
    .addField("-------------------------------------------------------------------------", "-----------------------------------------------------------------------")
    .addField("Created On", bot.user.createdAt.toDateString(), true)
    .addField("Prefix", `\`${prefix}\``, true)
    .addField("Developers", bot.users.get("298432708269441034").tag, true)
    .addField("Bot's Name", bot.user.username, true)
    .addField("Nickname", message.guild.members.get(bot.user.id).nickname !== null ? `${message.guild.members.get(bot.user.id).nickname}` : 'None', true)
    .addField("Version", config.version, true)
    .addField("Command Execute Count", `${cmdz} times`, true)
    message.channel.send(embed);
    });
});
}

module.exports.help = {
  name: 'botinfo',
    aliases: ["bi", "stats"]
}

From a Discord Bot Developer and Glitch User,
TehPig_YT

The module you are using fetches CPU percentage from the server itself, not your actual project.

1 Like

So, the actual CPU of the bt is the Glitch’s Project CPU?

From a Discord Bot Developer and Glitch User,
TehPig_YT

Yes, your best bet is to get the processes CPU usage, which may not entirely show your whole project’s CPU usage, but should show the usage of the bot itself. Take a look at this

I’m not sure if I have done it right but I managed to get a different amount of CPU:
image
With the Glitch Project showing:
image
And Using the following code:

    const previousUsage = process.cpuUsage();

    const startDate = Date.now();
    while (Date.now() - startDate < 500);
    const usage = process.cpuUsage(previousUsage);
    const result = 100 * (usage.user + usage.system) / ((Date.now() - startDate) * 1000) 
      
    fs.readdir("./commands/", (err, files) => {
  
    let jsfile = files.filter(f => f.split(".").pop() === "js");
    if(jsfile.length <= 0){
      return;
    }

    var ping = bot.ping
    let embed = new Discord.RichEmbed()
    .setAuthor(`${bot.user.username}'s Bot Stats and Info`, bot.user.avatarURL)
    .setThumbnail(bot.user.avatarURL)
    .setColor("RANDOM")
    .addField("Uptime", `Days: ${days} | Hours: ${hours} | Minutes: ${mins} | Seconds: ${realTotalSecs}`, true)
    .addField("CPU Usage", `${result}%`, true)
    .addField("Memory Usage", `${Math.round(used * 100) / 100} MB`, true)
    .addField("Server Count", bot.guilds.size, true)
    .addField("Member Count", bot.users.size, true)
    .addField("Channel Count", bot.channels.size, true)
    .addField("Node", process.version, true)
    .addField("Discord.js", `v${version}`, true)
    .addField("Commands", jsfile.length + 1, true)
    .addField("Platform", os.platform, true)
    .addField("ARCH", arch, true)
    .addField("-------------------------------------------------------------------------", "-----------------------------------------------------------------------")
    .addField("Created On", bot.user.createdAt.toDateString(), true)
    .addField("Prefix", `\`${prefix}\``, true)
    .addField("Developers", bot.users.get("298432708269441034").tag, true)
    .addField("Bot's Name", bot.user.username, true)
    .addField("Nickname", message.guild.members.get(bot.user.id).nickname !== null ? `${message.guild.members.get(bot.user.id).nickname}` : 'None', true)
    .addField("Version", config.version, true)
    .addField("Command Execute Count", `${cmdz} times`, true)
    message.channel.send(embed);
    });
}

The rest of the code is not posted due to being for other fields of the embed.

From a Discord Bot Developer and Glitch User,
TehPig_YT

So, how can I improve this and make it say the actual percentage of my project?

Multiple Glitch projects run on a single AWS EC2 instance. When you try get the CPU usage it shows for all of the projects in that EC2 server combined together.

I’m looking forward to making something like this:
image

1 Like

@TehPig_YT For that, you may want to run it on a RasberryPi or a separate computer. Just like what xXProGamerXx said. You will either have to deal with that or self host it. But that CPU stuff isn’t needed really for a bot info.

1 Like

I can tell you that some bot developers have achieved to do it using Glitch, so what you’re saying is not helpful at all.

Hm…if you wanted to get the percentage of available CPU specific to the container (as opposed to the actual disk) that sounds like a docker issue, right? I’m not a docker expert but this might be closer to what you’re looking for.

Nevermind, I couldn’t understand a lot from that but it’s still something.

I have found some information about this issue but I still cannot understand how to use them right.

/sys/fs/cgroup/memory/memory.stat
/sys/fs/cgroup/cpu/cpu.cfs_quota_us
/sys/fs/cgroup/cpu/cpu.cfs_period_us
/sys/fs/cgroup/cpu/cpuacct.usage

Anyways, I’ve solved my problem somehow. Thank you for your help everyone!

1 Like