Discord.js: Error "TypeError: fields.flat is not a function" when receiving an embedded message

Created on 8 Mar 2020  Â·  7Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:

An exception is raised from discord.js when some links are pasted down in a channel the bot has access to.
This happens even with the basic sample code given in the documentation. The error can be triggered by simply pasting the following link in any channel the bot can read: https://store.steampowered.com/app/1146940/I_will_eat_you/

Here is the full stack trace:

TypeError: fields.flat is not a function
    at Function.normalizeFields (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/MessageEmbed.js:434:8)
    at MessageEmbed.setup (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/MessageEmbed.js:68:50)
    at new MessageEmbed (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/MessageEmbed.js:11:10)
    at embeds.map.e (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/Message.js:101:48)
    at Array.map (<anonymous>)
    at Message._patch (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/Message.js:101:39)
    at new Message (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/structures/Message.js:43:20)
    at MessageManager.add (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/managers/BaseManager.js:49:32)
    at MessageManager.add (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/managers/MessageManager.js:29:18)
    at MessageCreateAction.handle (/home/auliyaa/dev/discord-genialo/node_modules/discord.js/src/client/actions/MessageCreate.js:13:40)

When looking at the code in MessageEmbed.js:

  static normalizeFields(...fields) {
    return fields
      .flat(2)
      .map(field =>
        this.normalizeField(
          field && field.name,
          field && field.value,
          field && typeof field.inline === 'boolean' ? field.inline : false,
        ),
      );
  }

Patching it into:

  static normalizeFields(...fields) {
    return fields
      .reduce((acc, val) => acc.concat(val), [])
      .map(field =>
        this.normalizeField(
          field && field.name,
          field && field.value,
          field && typeof field.inline === 'boolean' ? field.inline : false,
        ),
      );
  }

seems to fix the issue. However I'm not really fluent with nodejs and I'm not sure if this is the right way to fix this.

Include a reproducible code sample here, if possible:

As indicated above, the basic example given-out in the documentation is enough to trigger this issue:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('TOKEN');

Further details:

  • discord.js version: 12.0.1
  • Node.js version: 10.15.2
  • Operating system: PopOS 19.10
  • Priority this issue should have – please be realistic and elaborate if possible: High since it seems like it can make any bot crash when a user paste a link in any channel.

  • [ ] I have also tested the issue on latest master, commit hash:

unverified bug

Most helpful comment

This version of nodejs is not yet available on official repos. In addition, ArrayPrototype.flat is not yet standardized and there are alternatives forms (as proposed in the issue).

In any case, I'll add a patch on my side for the time being since this application is meant to be deployed on setups where I won't be able to touch the actual version of nodejs.

All 7 comments

Please update node.js to latest LTS (12), that'll solve your issue

This version of nodejs is not yet available on official repos. In addition, ArrayPrototype.flat is not yet standardized and there are alternatives forms (as proposed in the issue).

In any case, I'll add a patch on my side for the time being since this application is meant to be deployed on setups where I won't be able to touch the actual version of nodejs.

If you are saying that it's not available in apt or any other package manager, please install newer node using actual, official instructions for your linux distribution, available here.

Currently there is node 13, node 12 is LTS for a good while now, and discord.js library moved on to that version.

As stated above, I'll be deploying this code on platforms where I won't be able to change the version of nodejs. This is not really an issue on my developer setup but it will be for the production environment.

Anyway you may close this issue. I applied the workaround mentioned above (based on the example given on the MDN). I'll remove it once I'll be able to update to node 12.

Thanks for your time.

The proper approach here would be to use Babel for environments like this.

I had the same issue. Unfortunately, Patching did not work for me. Upgrading my nodejs to version 12 worked for me. Thanks for raising this.

bot not playing music when i do /play it search but it doesnt go on the voice chat and it doesnt even play ;(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DatMayo picture DatMayo  Â·  3Comments

smchase picture smchase  Â·  3Comments

kvn1351 picture kvn1351  Â·  3Comments

BrandonCookeDev picture BrandonCookeDev  Â·  3Comments

Brawaru picture Brawaru  Â·  3Comments