2

I am creating a Self-Bot in discord.js, I have created a function to make a box like:

╭──────────────────────╮
│        Title         │
├──────────────────────┤
│ A message that was   │
│ auto wrapped.        │
╰──────────────────────╯

But when I was working with this I got this error: <--- Last few GCs --->

[5414:0x102801600]    34628 ms: Mark-sweep 1402.3 (1462.5) -> 1402.2 (1431.5) MB, 1428.1 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1431 ms) last resort 
[5414:0x102801600]    35832 ms: Mark-sweep 1402.2 (1431.5) -> 1402.2 (1431.5) MB, 1203.7 / 0.0 ms  last resort 


<--- JS stacktrace --->

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

Security context: 0x1ecd007266a1 <JS Object>
        2: prettiefy [/Users/adm/Desktop/Discord/Client/prettiefy.js:~3] [pc=0x115c254f97bd](this=0x1031bc4e25a1 <an Object with map 0x3b708c34aea1>,s=0x1031bc4e25d9 <an Object with map 0x3b708c34b161>)
        3: run [/Users/adm/Desktop/Discord/Client/prettiefy.js:103] [pc=0x115c255b92d8](this=0x1031bc4e25a1 <an Object with map 0x3b708c34aea1>,str=0x1031bc4e2691 <String[17]: Message|Title|30 >,split=0...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/local/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
 4: v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/usr/local/bin/node]
 5: v8::internal::Runtime_AllocateInTargetSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
 6: 0x115c2528ed46
Abort trap: 6

I have no clue why this error happens, here's the code

const wrap = require ('wordwrap');

module.exports.prettiefy = s => { var _Result = '';
  const _default = {
    width: 25, // Prefered width if width is not defined
    title: 0, // having the title as Int(0) will skip titlebar
    message: 'Specify a message!', // Default message if message is not set
    softCorners: true // if false it will not have rounded corners.
  };

  if (typeof s == 'object') {
    if (s.width == undefined) s.width = _default.width;
    if (s.title == undefined) s.title = _default.title;
    if (s.softCorners == undefined || typeof s.softCorners !== 'boolean') s.softCorners = _default.softCorners;
    if (s.message == undefined) s.message = _default.message;
  } else if (typeof s == 'string') { s = {
    width: _default.width,
    title: _default.title,
    message: s,
    softCorners: _default.softCorners
  }; } else { throw new Error('arg[0] be a typeof string or object') }

  let _ = {
    title: (typeof s.title == 'string' ? wrap(0, s.width, {mode:'hard'})(s.title).split('\n') : (typeof s.title == 'number' && s.title == 0 ? '\077' : '' + wrap(0, s.width, {mode: 'hard'})('' + s.title).split('\n')) ),
    msg: wrap(0, s.width, {mode: 'hard'})(s.message).split('\n'),

    corners: {
      soft: ['╭', '╮', '╰', '╯'],
      hard: ['┌', '┐', '└', '┘']
    },

    l: ['─', '│', '├', '┤'],

        c: []
  };

    s.width += 1;

    _.c = _.corners.soft;
    if (!s.softCorners) _.c = _.corners.hard;

    /* Line: 1 aka Start */
    _Result += _.c[0] + _.l[0]; // Beginning: +-
    for (i = 0; i < s.width; i++) _Result += _.l[0]; // Center: --
    _Result += _.l[0] + _.c[1]; // End: -+

    /* Line: 2 aka Title */
    if (s.title !== '\077') {
        for (T = 0; T < _.title.length; i++) {
            _Result += _.l[1] + ' '; // Beginning: |\
            for (i = 0; i < (s.width / 2) - (_.title.length / 2); i++) _Result += ' '; // Center: \
            _Result += _.title; // Center: Message
            for (i = 0.5; i < (s.width / 2) - (_.title.length / 2); i++) _Result += ' '; // Center: \
            _Result += ' ' + _.l[1]; // End: \|
        }

        /* Line: 3 aka Title_Split */
        _Result += _.l[2] + _.l[0]; // Beginning: |-
        for (i = 0; i < s.width; i++) _Result += ' '; // Center: --
        _Result += ' ' + _.l[2]; // End: -|
    }

    /* Line: 4 aka Message */
    for (C = 0; C < _.msg.length; C++) {
        _Result += _.l[2] + _.l[0]; // Beginning: |\
        _Result += _.msg[C] // Center: Message
        for (i = 0; i < s.width - _.msg[C].length; i++) _Result += ' '; // Center: \
        _Result += ' ' + _.l[2]; // End: \|
    }

    /* Line 5 aka End */
    _Result += _.c[2] + _.l[0]; // Beginning: +-
    for (i = 0; i < s.width; i++) _Result += _.l[0];
    _Result += _.l[0] + _.c[3]; // End: -+

    // End;
    return _Result;
};

module.exports.translate = function (str, Splitter = '|', Skipper = '/Skip/') {
    if (typeof str !== 'string') str = '' + String(str);
    var Res = {};

    str = str.split(Splitter);

    /*
        Arg[0]: Message,
        Arg[1]: Title,
        Arg[2]: Width,
        Arg[3]: softCorners
    */

    if (str.length < 1) throw new Error('Need atleast one argument!');

    if (str[0] !== undefined || str[0] == Skipper) Res.message = str[0];
    if (str[1] !== undefined || str[1] == Skipper) Res.title = str[1];
    if (str[2] !== undefined || str[2] == Skipper) Res.width = parseInt(str[2]);
    if (str[3] !== undefined || str[3] == Skipper) Res.softCorners = str[3];

    return Res;
}

module.exports.run = function (str, split) { return module.exports.prettiefy(module.exports.translate(str, split)); }

Does anyone have any clue why this is happening?

I do have 32 GigaBytes of ram, I see it is a memory program, but why does it crash when I have 32GB and it tried to allocate apx. 1,5

Alliator
  • 73
  • 1
  • 10
  • HINT: You probably doing something you have not intended to (like having a very long array or a endless loop). You can begin with tracing the exact place node is crashing at. – Shmuel H. Apr 15 '17 at 21:22
  • It crashed on the last line of my code (line 103) – Alliator Apr 15 '17 at 21:23
  • What_Result`'s length is? See [here](http://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory#38560292), V8/node.js has an around 1.7GB memory limit. – Shmuel H. Apr 15 '17 at 21:25
  • What do you mean? – Alliator Apr 15 '17 at 21:29
  • My code has no infinte loops, hmm? – Alliator Apr 15 '17 at 21:30
  • Sorry, I meant how long is `_Result` in the end? See my previous commend. Also, in some cases for example `(s.width / 2) - (_.title.length / 2)`, if that would be `<0`, you would end up with an infinite loop. – Shmuel H. Apr 15 '17 at 21:34
  • `_Result` on the end is `154 characters long`, `(s.width / 2) - (_.title.length / 2) = 17.5` – Alliator Apr 15 '17 at 21:43
  • Sorry if it is a bit offtopic, but I would like to point out the fact, that making Self-bots is against Discord Terms of Service, as pointed out by their article [here](https://support.discord.com/hc/en-us/articles/115002192352-Automated-user-accounts-self-bots-) – RishiMath Jul 03 '21 at 14:59

0 Answers0