0

So I have a discord bot and I want to make it be able to duel with players. However I kept running into a FATAL ERROR, which I figured was from the while loop. I'm kind of new at this, so is there some sort of rule against using while loop in discord.js? I've been stuck at this for days and I tried googling but I just confused myself even more.

my code:

msg.channel.send(`${taggedUser}, ${msg.guild.members.cache.get(msg.author.id).displayName} wants to duel you. Do you accept? (Reply yes or no.)`);
msg.channel.awaitMessages(m => m.author.id == taggedUser.id, {max: 1, time: 10000})
.then(collected => {
if (collected.first().content.toLowerCase() == 'yes') {
    msg.channel.send("Let the battle begin!");
    p1points = 100;
    p2points = 100;
    while (p1points !== 0 || p2points !== 0) {
        const randomOption = results[Math.floor(Math.random() * results.length)];
        msg.channel.send(`**${player1}** casted ${randomOption.message} (∩•̀ω•́)⊃-⋆`);
        p2points = p2points + randomOption.xp;
        msg.channel.send(`**${player1}**: ${p1points}, **${player2}**: ${p2points}`);
        const randomOption2 = results[Math.floor(Math.random() * results.length)];
        msg.channel.send(`**${player2}** casted ${randomOption2.message} ⋆-⊂(•̀ω•́∩)`);
        p1points = p1points + randomOption2.xp;
        msg.channel.send(`**${player1}**: ${p1points}, **${player2}**: ${p2points}`);
    }
    if (p1points === 0) {
        msg.channel.send(`**${player1}** is DEFEATED. AAAAAAh! (ノ゚Д゚)八(゚Д゚)八(゚Д゚)ノィェーィ!`);
        msg.channel.send(`CONGRATULATIONS, ${player2}! (っ◕‿◕)っ :heart:`);
    }
    if (p2points === 0) {
        msg.channel.send(`**${player2}** is DEFEATED. AAAAAAh! (ノ゚Д゚)八(゚Д゚)八(゚Д゚)ノィェーィ!`);
        msg.channel.send(`CONGRATULATIONS, ${player1}! (っ◕‿◕)っ :heart:`);
    }
}

else if (collected.first().content.toLowerCase() == 'no') {
    msg.channel.send("Yikes, are you scared? Maybe next time then!"); 
}                                   
}).catch(() => {
    msg.channel.send('No reply after 10 seconds, duel is cancelled!');
});

Error:

<--- Last few GCs --->
[4:0x3778960]    21250 ms: Mark-sweep 254.7 (258.3) -> 254.2 (258.5) MB, 201.4 / 0.0 ms  (average mu = 0.119, current mu = 0.012) allocation failure scavenge might not succeed
[4:0x3778960]    21556 ms: Mark-sweep 255.0 (258.5) -> 254.5 (258.8) MB, 303.1 / 0.0 ms  (average mu = 0.059, current mu = 0.012) allocation failure scavenge might not succeed
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x13c5b79]
1: StubFrame [pc: 0x13c6a4d]
Security context: 0x26d9b2a808d1 <JSObject>
2: /* anonymous */ [0x2c9d420f9409] [internal/modules/cjs/loader.js:~605] [pc=0x270b156db629](this=0x2c9d420c0799 <JSFunction Module (sfi = 0x26d9b2ab0d21)>,0x15d111435a01 <String[#7]: ../User>,0x0ac218d43aa1 <JSArray[1]>,0x18752e1c06e9 <false>)
3: /* anonymous */ [0x2c9d420f69f1] [internal/modules/cjs/loader.js:~892] [pc=...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Writing Node.js report to file: report.20200522.160742.4.0.001.json
Node.js report completed
1: 0xa09830 node::Abort() [node]
2: 0xa09c55 node::OnFatalError(char const*, char const*) [node]
3: 0xb7d71e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xb7da99 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xd2a1f5  [node]
6: 0xd2a886 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
7: 0xd37105 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
8: 0xd37fb5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
9: 0xd3aa6c v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xd0163b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
11: 0x104300e v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
12: 0x13c5b79  [node]
Maryam Dinzly
  • 13
  • 1
  • 4

2 Answers2

0

Read the full error, specifically this line

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Looking for JavaScript heap out of memory brings up some answers like this answer and this one

mmoomocow
  • 1,173
  • 7
  • 27
0

I made a small guess out of what I see going on in the code you provided.

First, you need to make sure you aren't mixing up your operators. In your snippet example, the way you are using the or operator is telling the while loop to execute until both player's points equal exactly 0. And since you are only adding to the player points and not subtracting the points will never equal 0 unless the .xp property is a negative number.

Secondly, for those times where a players points can end up being less than 0 then I'd recommend doing

while (p1points > 0 && p2points > 0) {

instead of

while (p1points !== 0 && p2points !== 0) {

because if you use > 0 then it'll stop executing whenever one of the players points are exactly 0 or less than 0.

BannerBomb
  • 53
  • 1
  • 13