1

I'm coding a bot that auto-enters a voice chat at midnight, and then plays a song.

I've tried using this:

 // require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
const fs = require('fs');
const broadcast = client.voice.createBroadcast();
// Checks the date
var date = new Date()


// when the client is ready, run this code
// this event will only trigger one time after logging in

client.once('ready', () => {
    console.log('Ready!');
});

client.login(token);

function timeReached(){
    setInterval(function(){ date = new Date(); }, 30000);
    if (date.getHours() === 14 && date.getMinutes() === 8) 
    {
        return true;
    }
    else
    {
        return false;
    }
    }

client.on("ready", () => {
    const channel = client.channels.cache.get("my chat");
    if (!channel) return console.error("The channel does not exist!");
    while (timeReached != true)
    {
        timeReached();
    }
    const conection = channel.connect();
    const dispatcher = connection.play ("audio.mp3");
    setTimeout(function(){
        connection.disconnect();
    }, 5000)    
});

But after a while running, this message appears on the console:

<--- Last few GCs --->

[13860:00000136C7E96090]    23337 ms: Scavenge 2028.1 (2035.6) -> 2027.6 (2036.4) MB, 9.1 / 0.0 ms  (average mu = 0.142, current mu = 0.062) allocation failure
[13860:00000136C7E96090]    23346 ms: Scavenge 2028.8 (2036.4) -> 2028.3 (2047.1) MB, 6.6 / 0.0 ms  (average mu = 0.142, current mu = 0.062) allocation failure
[13860:00000136C7E96090]    23485 ms: Scavenge 2035.3 (2047.1) -> 2034.5 (2040.1) MB, 10.3 / 0.0 ms  (average mu = 0.142, current mu = 0.062) allocation failure


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 00007FF7E62C77DD]
Security context: 0x0261bc4408d1 <JSObject>
    1: incRefCount(aka incRefCount) [0000021BF3E94141] [internal/timers.js:~289] [pc=0000019AF4857628](this=0x0042117804b1 <undefined>)
    2: /* anonymous */ [0000025542A3FFC9] [C:\Users\R2\Desktop\oleo_de_macaco_bot\index.js:~34] [pc=0000019AF485910C](this=0x03aac97c2429 <EventEmitter map = 00000101FF0CA349>)
    3: emit [000003FAB26E3969] [events.js:327...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF7E56B470F napi_wrap+113583
 2: 00007FF7E565F7D6 v8::base::CPU::has_sse+66646
 3: 00007FF7E56605D6 v8::base::CPU::has_sse+70230
 4: 00007FF7E5E742EE v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF7E5E5C3C1 v8::SharedArrayBuffer::Externalize+833
 6: 00007FF7E5D2890C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436
 7: 00007FF7E5D33B40 v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312
 8: 00007FF7E5D30664 v8::internal::Heap::PageFlagsAreConsistent+3204
 9: 00007FF7E5D25E63 v8::internal::Heap::CollectGarbage+1283
10: 00007FF7E5D2C6F4 v8::internal::Heap::GlobalSizeOfObjects+212
11: 00007FF7E5D6251B v8::internal::StackGuard::HandleInterrupts+907
12: 00007FF7E5AAD719 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+7737
13: 00007FF7E62C77DD v8::internal::SetupIsolateDelegate::SetupHeap+546637
14: 0000019AF4857628

Judging by this line:

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

It seems to be that my bot is running out of memory. How can I alocate more memory to it or circuvent this problem?

P.S.: It is my very first bot, and i know very little about javascript.

Tenclea
  • 1,444
  • 2
  • 10
  • 23
  • Does this answer your question? [Node.js heap out of memory](https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory) – Syntle Jun 09 '20 at 17:31

2 Answers2

2

Near the bottom of your code, I see this while loop:

while (timeReached != true)
{
    timeReached();
}

I'm not exactly certain what you're trying to do here, but timeReached is a function, and so that condition, timeReached != true, always returns true, which means that timeReached() will be called repeatedly until the program terminates. This is where I'd assume the error is coming from.

Try changing the code to see if the value the function returns is not true (notice the parenthesis):

while (timeReached() != true)
{
    timeReached();
}
ElectrifyPro
  • 53
  • 1
  • 4
  • Thank you! I've implemented your change, but the problem still persists. The middle lines have suffered slight alterations, but the error message remains the same. – Lucas Moura Jun 09 '20 at 17:51
  • Could you tell me what you want to achieve with the `timeReached()` function? That'll help me solve the issue. – ElectrifyPro Jun 09 '20 at 20:05
1

I think that the interval created inside of the timeReached function is resulting in a huge memory leak.

Since you are always checking if the date is reached inside of your while statement, I think that you should replace the following line in your hour-checking function (setInterval(function(){ date = new Date(); }, 30000);) to simply const date = new Date();

Tenclea
  • 1,444
  • 2
  • 10
  • 23
  • I've changed that already and it seems to be working now. Just need to tweak the room-joining code. Thanks anyway! – Lucas Moura Jun 09 '20 at 20:09
  • 1
    @LucasMoura try to replace your `channel.connect()` with `channel.join()` ;) – Tenclea Jun 09 '20 at 20:10
  • Now the problem lies on the "connection.play('audio.mp3'); " bit. It is not being recoganized as a valid function. – Lucas Moura Jun 09 '20 at 20:19
  • 1
    @LucasMoura you should switch to a `.then(connection => {` callback in that case (right after joining the channel), and make sure to move all the code below the join function inside of the new callback (.then) – Tenclea Jun 09 '20 at 20:24
  • @LucasMoura glad I could have helped :) – Tenclea Jun 09 '20 at 20:30