0

in events/messageCache.ts:

import { Events, Message } from 'discord.js';


export interface MessageCacheEntery {
    authorID: string;
    deleted: boolean;
    message: string;
    timestamp: number;
}

export let messageCache: MessageCacheEntery[] = [];

const guildID = '1254163158914957342';
module.exports = {
     name: Events.MessageCreate,
     once: false,
     async execute(message: Message) {
        if (message.guild?.id == guildID && message.author.bot == false) {
            messageCache.unshift(
                {
                    authorID: message.author.id,
                    deleted: false,
                    message: message.content,
                    timestamp: Date.now()
                }
            );
            console.log(messageCache);
        }
        
    }
}

this event works fine by it self!

was trying to access the exported cached messages Array messageCache in another event and I kept getting errors stating that messageCache was undefined, which was weird since even typescript didn't warn me about it being undefined and from my experience with other projects it's supposed to work. Im sure this error is bigger in scope to just discord.js, but this is where I encountered it.

I tried exporting it at the end of the script file with export { messageCache } and also tried multiple ways of importing it.

here is an example command:

import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import { messageCache, MessageCacheEntery } from "../../events/messageCache";

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('test to get message cache'),
    async execute(interaction: CommandInteraction) {
        await interaction.reply({ content: JSON.stringify(messageCache) });
    }
}

I expect to at least get an empty array but instead I get messageCache being undefined, this error doesn't happen to the type MessageCacheEntery as if I defined messageCache using the exported type like this:

import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import { MessageCacheEntery } from "../../events/messageCache";

let messageCache : MessageCacheEntery[] = [];

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('test to get message cache'),
    async execute(interaction: CommandInteraction) {
        await interaction.reply({ content: JSON.stringify(messageCache) });
    }
}

I would get an empty array as a reply.

this is just an example command, this error also happens when trying to export the variable array to other events too

0

0