-
Notifications
You must be signed in to change notification settings - Fork 8
Video Correction Notes
Welcome to the Discord-Bot-Examples wiki!
(dipamsen)
You are demonstrating three different types of import statements in a file, so might be worth explaining what they are: ( MDN Ref )
import { Client, Events, GatewayIntentBits } from 'discord.js';
import dotenv from 'dotenv'; // though this is changed in the final example to a named import
import * as choochoo from './commands/choochoo.js';
Firstly, import { a, b } from "library"
is not Destructuring. You can call it that, everyone calls it that because the earlier syntax const {a, b} = require("library")
is in fact destructuring. But the import statement is just a new syntax for Named Imports - importing multiple things from a module. These "things" correspond to Named Exports, which is what you are doing in command files; export const fooBar = 42
=> import { fooBar } from "file"
.
// library.js
export const data = {
name: "Coding Train"
}
export function add(x, y) {
return x + y;
}
// index.js
import { data, add } from "library.js";
console.log(data);
console.log(add(3, 4));
While having named exports, it is possible to namespace all the exports under a single name by using import * as name from "file"
, which will import all named exports as properties under a single object, name
. (Namespaced import)
// library.js
export const data = {
name: "Coding Train"
}
export function add(x, y) {
return x + y;
}
// index.js
import * as myLib from "library.js";
console.log(myLib.data);
console.log(myLib.add(3, 4));
Finally, the import name from "file"
(though you wont need this) is default import, which will import the default export (duh), which is exported as export default myValue
. Here the name of the import doesnt matter.
// library.js
let answer = 42
export default answer // only one thing can be exported as default
// index.js
import library from "library.js" // name of import can be anything, usually name of library/file