discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.
https://discordjs.guide
MIT License
1.57k stars 2.3k forks source link

tutorial ES6 update #1554

Open time4nothing opened 8 months ago

time4nothing commented 8 months ago

Is your feature request related to a problem? Please describe.

Your current tutorial is filled with require() and module.exports. Converting to the ES6 import/export does not work.

1) your dynamic command file setup has a require() that I could not figure out how to replace (admittedly, still learning node, but getting fairly proficient at it) 2) get error "SlashCommandBuilder is not a constructor" trying to convert command module

Describe the solution you'd like

I would like to see an updated tutorial with proper ES6 import/export so I can properly learn current coding methods.

Describe alternatives you've considered

No response

Additional notes

I do not wish to learn outdated coding methods, so converting my project to CommonJS is a last resort that I would prefer not to do.

monbrey commented 8 months ago

Thanks for the feedback - we do have plans to rewrite the examples to ES6. In fact, many already have been but the updated guide has not yet gone live.

For information on how to use ES6 import correctly, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

For example, importing SlashCommandBuilder is import { SlashCommandBuilder } from 'discord.js';

The equivalent to the in-line require() you mentioned is the dynamic import: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import

time4nothing commented 8 months ago

Thank you for the response. I am looking forward to that update, as every other tutorial I could find also used the require() syntax, despite saying it's updated for ES6...

In regards to your helpful advice and links:

1: I did import the SlashCommandBuilder in that way, but running the code gives me an error that SlashCommandBuilder is not a constructor. Worked fine if I c/p the lines directly into my index.js though.

2: I did try the import() function, but trying to use the object that should have been imported, was not working for me. Probably something I am doing wrong, but so far unable to work it out.

Prashantjha14 commented 8 months ago

Along with ES6, it will be great if you add TypeScript examples :)