Hi everyone. I was hoping someone could point out a flaw in my code that could be leading to a memory leak while webscraping.I'm using the date-events module to trigger a web-scrape every minute, stringifying that web scrape and then isolating a particular string from the webpage. That string is then used for discord.js.For months my code ran with a relatively smooth memory usage, but once I started trying this single webscraping function, the memory usage trends upwards from 15 Mb to 40 Mb over the course of 3 to 4 days. (And I assume it would go higher)Specifically, I use the body argument from a request module web-scrape. At the end, I return the final action.Any help would be appreciated.require('request')('https://projectnelth.com', function (error, response, body) { var { Client, MessageEmbed } = require('discord.js'); var data = body.toString(); var MessageToEdit = FactionsDiscord.channels.cache.find(ch => ch.name === 'factions').messages.cache.array()[0]; var preDataString = String(JSON.stringify(data)); var DataString = String(JSON.stringify(data)).substring(preDataString.indexOf('box-server-status'), preDataString.indexOf('box-discord')); var ServerDataAnchor = DataString.indexOf('Alliance',DataString.indexOf('%)')-15); var ServerDataAnchor2 = DataString.indexOf('%)', ServerDataAnchor+15)+2; var FactionBalance = DataString.substring(ServerDataAnchor, ServerDataAnchor2); var EmbedToEdit = MessageToEdit.embeds[0]; var totalOnline = FactionsDiscord.members.cache.filter(member => member.presence.status !== null); //Returns number of members online. var HordeTotal = totalOnline.filter(member => member.roles.cache.some(r=>["Horde"].includes(r.name))).size; var AllianceTotal = totalOnline.filter(member => member.roles.cache.some(r=>["Alliance"].includes(r.name))).size; var TotalFactioned = (HordeTotal+AllianceTotal); var AlliancePercent = Math.floor((AllianceTotal/TotalFactioned)*100); var HordePercent = Math.floor((HordeTotal/TotalFactioned)*100); var TotalPercent = (AlliancePercent+HordePercent); if (TotalPercent !== 100) { if (AlliancePercent > HordePercent) { HordePercent += (100-TotalPercent); } else { AlliancePercent += (100-TotalPercent); } } var UpdatedFactionsEmbed = new MessageEmbed(EmbedToEdit); var IngameFactionString = FactionBalance; //var IngameFactionString = `**Discord:** Alliance (50%) | Horde (50%)`; var DiscordFactionString = `**Discord:** Alliance (${AlliancePercent}%) | Horde (${HordePercent}%)`; UpdatedFactionsEmbed.setDescription(`**Ingame:** ${IngameFactionString} ${DiscordFactionString} ${ALLI} -> Join the Alliance! ${HORDE} -> Join the Horde! :x: -> Remove my faction.`); return MessageToEdit.edit(UpdatedFactionsEmbed); });
Submitted July 14, 2020 at 10:23PM by PN-Cryptid
No comments:
Post a Comment