deno: Cannot import all the files from a folder.
Hi!
I’m making a Discord bot with dynamic commands (automatic loading/reloading).
It’s not my first Discord bot but all previous was made using node.js and require() to do this dynamically.
But here it doesn’t work using await import() 😕
Here is my code :
import {SEP} from 'https://deno.land/std/path/mod.ts';
import {configs} from './configs.ts';
// configs.commandsFolder = 'src/commands'
const cmdDir: Iterable<Deno.DirEntry> = Deno.readDirSync(Deno.realPathSync(configs.commandsFolder));
for (let command of [...cmdDir]) {
if (command.isFile && command.name.endsWith('.ts')) {
const path: string = Deno.realPathSync(configs.commandsFolder + SEP + command.name);
const commandClass: any = await import(`file://${path}`);
const cmd: any = commandClass.default;
if (!cmd) {
console.warn(`Could not load '${command.name}' command !`);
continue;
}
commands.set(cmd.name, new cmd());
console.log(`Successfully loaded '${cmd.name}' command !`);
}
}
console.log(commands);
I have two commands, named CompileCommand.ts and ReloadCommand.ts, this is the code of the command reload :
import Command from '../classes/Command.ts';
export default class ReloadCommand extends Command {
public constructor() {
super('reload');
}
public async run(message: Message, args: string[]): Promise<void> {
// some code
}
}
So when I launch all this, it loads the first command but stops at the second for no reason, not sending any error (it was sending one in deno 1.4.4 and now I am in 1.4.5). There are the logs :
Check file:///C:/Users/Pierre/WebstormProjects/deno/tsc-bot/mod.ts
Successfully loaded 'CompileCommand' command !
Check file:///C:/Users/Pierre/WebstormProjects/deno/tsc-bot/src/commands/ReloadCommand.ts
(tsc-bot/mod.ts is the file where there is the command handler)
I don’t know what it is doing, why it does that and I want some help ^^’
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21 (10 by maintainers)
Oooh, I understand now, I fixed it and it works now, thank you! And thanks to you for your patience!
@Ayfri thanks! I tested https://github.com/denoland/deno/pull/7946 on your project and it indeed hangs. I will investigate further.