#help with importing

1 messages · Page 1 of 1 (latest)

spice belfry
#

my command runner requires me to import the command from the commands directory and create a new instance of it to register it with my bot, but calling await import(join(dir, file)).default; crashes the application. is there a workaround/alternative to importing modules dynamically ?

#
$ bun run .
[0.06ms] ".env"
50 |         }
51 |       }
52 |       return new FixedQueue;
53 |     }();
54 | 
55 |     function processTicksAndRejections() {
                                                                               ^
SyntaxError: Indirectly exported binding name 'Context' is not found.
      at processTicksAndRejections (:55:76)
error: script "start" exited with code 1 (SIGHUP)```
#

this is the error thrown

knotty dirge
#

Can you share some code that reproduces this?

spice belfry
# knotty dirge Can you share some code that reproduces this?
export async function registerCommands(bot: Bot<Context>, dir: string = join(import.meta.dir, '../commands')) {
    const files = readdirSync(dir);

    for (const file of files) {
        const path = join(dir, file);
        const stat = lstatSync(path);

        if (stat.isDirectory()) {
            await registerCommands(bot, path);
        } else {
            const Cmd = (await import(join(dir, file))).default;
            const command = new Cmd() as Command;

            bot.command(command.name, async (ctx) => {
                if (command.owner) {
                    if (ctx.from?.id.toString() === '1429912972') {
                        await command.run(ctx);
                    }
                } else {
                    await command.run(ctx);
                }
            });
        }
    }
}```

this is the function that registers each individual command, i commented out everything except the `default` import for Cmd and that's when it threw the error
#

ah wait

#

one moment

#

let me try something

#

nevermind didnt change anything

#

i thought since i was importing default, changing the class from export default class ... to export class Class ... would change anything

knotty dirge
#

this isn't yet something that I can actually run myself since I don't know what's in ./commands or what Bot is, for instance

#

it'll be easier to help if you can boil this down to like, 1-2 files

spice belfry
#
// commands/general/purchase.ts

import { InlineKeyboard } from 'grammy';
import { Command, Context } from 'lib';

export class PurchaseCommand extends Command {
    public constructor() {
        super({
            name: 'purchase'
        });
    }

    public async run(ctx: Context) {
        const purchaseButtons = new InlineKeyboard()
            .text('1 Day ($5)', 'daybutton')
            .row()
            .text('1 Week ($25)', 'weekbutton')
            .row()
            .text('1 Month ($100)', 'monthbutton')
            .row()
            .text('Lifetime ($500)', 'lifetimebutton')
            .row()
            .text('Cancel', 'cancelbutton')
            .row();

        await ctx.reply('Select a subscription plan below', {
            reply_markup: purchaseButtons
        });
    }
}
``` this is the class being imported
#

the only file in commands atm

#

everything on the bot related side seems to be fine

knotty dirge
#

pretty sure you need to do a default import from "lib"

#

try that

spice belfry
#

oh to clarify

knotty dirge
#

import lib from "lib"

spice belfry
#

lib is src/lib

knotty dirge
#

bruh

spice belfry
#

and inside lib there is an index.ts importing this

#

sec

#
import { Listener } from './base/listener';
import { Command } from './base/command';
import { Context } from './types/context';

export { Listener, Command, Context };
#

bad practice ?

wind hedge
#

what is Context exactly

spice belfry
#
import { HydrateApiFlavor, HydrateFlavor } from '@grammyjs/hydrate';
import { ParseModeFlavor } from '@grammyjs/parse-mode';
import { Context as Ctx, Api } from 'grammy';

export type Context = ParseModeFlavor<HydrateFlavor<Ctx>> & HydrateApiFlavor<Api>;
wind hedge
#

not sure how

spice belfry
#

any potential workarounds? 😭

#

if not then i might need to import every command manually and register it one by one in the meantime until theres a fix for this

#

its weird though, i import the context type in different parts of my code with no problem

wind hedge
#

are you using Context like it's a value and not a type

#

this should be a ts error

spice belfry
#

i only import Context when i need to use it as a type

knotty dirge
#

import { type Context } from "./types/context" maybe

spice belfry
spice belfry
#
$ bun run .
[0.07ms] ".env"
12 | 
13 |            if (stat.isDirectory()) {
14 |                    await registerCommands(bot, path);
15 |            } else {
16 |                    const Cmd = (await import(join(dir, file))).default;
17 |                    const command = new Cmd() as Command;
                       ^
TypeError: undefined is not a constructor (evaluating 'new (await import(join(dir, file))).default')
      at /home/denyed/Desktop/infinite/src/utilities/register.ts:17:19
      at processTicksAndRejections (:55:76)
error: script "start" exited with code 1 (SIGHUP)```
#

console logging Cmd returns undefined

#

removing .default from the variable returns

Module {
  PurchaseCommand: [class PurchaseCommand]
}```
#

think i fixed it