Having trouble with my collections object in the Discord.js library. Working on a command and event handler to populate commands and events based on files in directories. The issue is that the second collection I populate becomes empty after both are set, even though they populate correctly individually within the map function.
If I switch the order of setting the collections, the problem shifts to whichever one is set second. Debugging shows no issues related to directories or imported files, leading me to believe it's a misunderstanding of how collections work on an object.
Any insights on this would be appreciated!
import { Command, Event, Config } from "../Interfaces/index"
import { Client, Collection, Intents } from "discord.js"
import glob from "glob";
import { promisify } from "util";
const globPromise = promisify(glob)
class Bot extends Client {
public events: Collection<string, Event> = new Collection()
public commands: Collection<string, Command> = new Collection()
public aliases: Collection<string, Command> = new Collection()
public config: Config
public constructor() {
super({ ws: { intents: Intents.ALL } })
}
public async init(config: Config): Promise<void> {
this.config = config
this.login(this.config.token)
const commandFiles: string[] = await globPromise(`${__dirname}/../Commands/**/*.ts`)
commandFiles.map(async (filePath: string) => {
const { command }: { command: Command } = await import(filePath)
this.commands.set(command.name, command)
if (command.aliases?.length !== 0) {
command.aliases?.forEach((alias) => {
this.aliases.set(alias, command)
})
}
})
const eventfiles: string[] = await globPromise(`${__dirname}/../Events/**/*.ts`)
eventfiles.map(async (filePath: string) => {
const { event }: { event: Event } = await import(filePath)
this.events.set(event.name, event)
console.log(this) // Events and commands collection are populated
})
console.log(this) // Events collection is empty and commands collection is populated
}
}