Erenay
Erenay

Reputation: 1

Discord.js RangeError: Maximum call stack size exceeded

The pingChart file is create on the /, but this error appears. I type /ping in Discord and this happens:

C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:21
.filter(key => !key.startsWith('_')) ^

RangeError: Maximum call stack size exceeded at Array.filter () at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:21:6) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58) at flatten (C:\Users\Erenay\Desktop\bot\node_modules\discord.js\src\util\Util.js:48:58)

/commands/ping.js

const {
  Discord,
  EmbedBuilder,
  SlashCommandBuilder,
  AttachmentBuilder
} = require('discord.js')
const {
  updatePingChart,
  canvas
} = require("../helpers/chart");

function wait(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}
const fs = require("fs")
module.exports = {
  slash: true,
  cooldown: 1200,

  data: new SlashCommandBuilder()
    .setName('ping')
    .setDescription('Shows bot ping.'),

  async execute(client, interaction) {

    let pinge = '<:Bot:1065675104027148338>'
    let bote = '<:Ping:1065675122683412481>'
    let messageping = new Date().getTime() - interaction.createdTimestamp
    let ping = await client.ws.ping
    
    await updatePingChart(ping)
    
    const stream = canvas.createPNGStream();
    const attachment = new AttachmentBuilder(stream, {
      name: 'ping.png'
    });
    
    const embed = new EmbedBuilder()
      .addFields({
        name: `${bote} **Bot ping:**`,
        value: `\`${ping}ms\``
      }, {
        name: `${pinge} **Message ping:**`,
        value: `\`${messageping}ms\``
      })
      .setColor(`Gold`)
      
    await interaction.reply({
      embeds: [embed],
      attachments: [attachment]
    })
    
    wait(5000)
    
    const fileName = 'pingChart.png';
    fs.unlinkSync(fileName);
  }
}

/helpers/chart.js

const db = require("croxydb")
const { createCanvas } = require("canvas")
const width2 = 800;
const height2 = 400;
const Chart = require("chart.js/auto")

function wait(time) {
  return new Promise(resolve => setTimeout(resolve, time));
}

const fs = require("fs")
let myChart;
const canvas = createCanvas(width2, height2);
const ctx2 = canvas.getContext('2d');

async function updatePingChart(url) {
  if (myChart) {
    myChart.destroy();
  }

  const uptimeLinks = db.get("Ping") || {};
  console.log(uptimeLinks);
  const labels = uptimeLinks[0].label
  const pinkg = uptimeLinks[0].ping
  console.log(labels, pinkg);
  
  const configuration = {
    type: 'line',
    data: {
      labels: labels,
      datasets: [{
        label: 'Ping (ms)',
        backgroundColor: 'rgba(75, 192, 192, 0.2)',
        borderColor: 'rgba(75, 192, 192, 1)',
        data: pinkg,
        yAxisID: 'y-axis-1',
      }]
    },
    options: {
      scales: {
        x: [{
          type: 'linear',
          position: 'bottom',
          ticks: {
            callback: function(value, index) {
              if (index % 12 === 0 && labels[index / 12]) {
                return labels[index / 12];
              } else {
                return '';
              }
            }
          }
        }],
        y: [{
            type: 'linear',
            display: true,
            position: 'left',
            id: 'y-axis-1',
          },
          {
            type: 'linear',
            display: true,
            position: 'right',
            id: 'y-axis-2',
            ticks: {
              max: 1,
              min: 0,
              stepSize: 1,
              callback: function(value) {
                return value === 1 ? 'Up' : 'Down';
              }
            }
          },
        ]
      }
    }
  };

  myChart = new Chart(ctx2, configuration);

  const out = fs.createWriteStream('pingChart.png');
  const stream = canvas.createPNGStream();

  return new Promise((resolve, reject) => {
    stream.pipe(out);
    out.on('finish', () => {
      resolve('The chart image was saved.');
    });
    out.on('error', (err) => {
      reject(err);
    });
  });
}

module.exports = {
  updatePingChart: updatePingChart,
  canvas: canvas
};

Upvotes: 0

Views: 162

Answers (1)

Erenay
Erenay

Reputation: 1

I fixed this error

I change the content

  const { updatePingChart } = require("../helpers/chart");
  const stream = updatePingChart().stream

and I changed the chart.js file

updatePingChart() function will be return the stream

Upvotes: 0

Related Questions