#help with importing
1 messages · Page 1 of 1 (latest)
$ 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
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
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
// 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
oh to clarify
import lib from "lib"
lib is src/lib
bruh
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 ?
what is Context exactly
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>;
it seems something is importing Context as a js value (vs a typescript type)
not sure how
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
i only import Context when i need to use it as a type
import { type Context } from "./types/context" maybe
i'll try this
so, this did seem to resolve the previous problem with the import, but now it's not picking it up as a constructor
$ 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