Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot destructure property 'cache' of 'guild[key]' as it is undefined. #10069

Open
JMTK opened this issue Jan 3, 2024 · 3 comments · May be fixed by #10070
Open

Cannot destructure property 'cache' of 'guild[key]' as it is undefined. #10069

JMTK opened this issue Jan 3, 2024 · 3 comments · May be fixed by #10070

Comments

@JMTK
Copy link
Contributor

JMTK commented Jan 3, 2024

Which package is this bug report for?

discord.js

Issue description

This was seemingly fixed before, but I've noticed it happening again recently. I can't seem to reliably reproduce it but after running my bot for a certain amount of time, it'll pop up as an uncaught exception. I'm not 100% sure if it's directly related to Stickers only or it just happens to happen with stickers. Seems to be happening with emojis as well. I double checked all my guilds to see if they had those properties and they did, so it seems to be a timing issue.

TypeError: Cannot destructure property 'cache' of 'guild[key]' as it is undefined.
    at Sweepers._sweepGuildDirectProp (/app/node_modules/discord.js/src/util/Sweepers.js:411:15)
    at Sweepers.sweepEmojis (/app/node_modules/discord.js/src/util/Sweepers.js:106:17)
    at Timeout._onTimeout (/app/node_modules/discord.js/src/util/Sweepers.js:462:21)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
TypeError: Cannot destructure property 'cache' of 'guild[key]' as it is undefined.
    at Sweepers._sweepGuildDirectProp (/app/node_modules/discord.js/src/util/Sweepers.js:411:15)
    at Sweepers.sweepStickers (/app/node_modules/discord.js/src/util/Sweepers.js:212:17)
    at Timeout._onTimeout (/app/node_modules/discord.js/src/util/Sweepers.js:462:21)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

Code sample

const getComparisonTimestamp = (e: any) => e?.createdTimestamp ?? lastFilterTimestamp;
const discordCacheItemsWeDontWant = {
    interval: intervalDefaultInSec,
    filter: Discord.Sweepers.filterByLifetime({
        lifetime: 5,
        getComparisonTimestamp
    })
} as Discord.SweepOptions<string, any>;

let mybot: Discord.Client;
const defaultClientOptions = {
    intents: [
        Discord.IntentsBitField.Flags.Guilds,
        Discord.IntentsBitField.Flags.GuildMembers,
        Discord.IntentsBitField.Flags.GuildVoiceStates,
        Discord.IntentsBitField.Flags.GuildPresences,
        Discord.IntentsBitField.Flags.GuildEmojisAndStickers,
        Discord.IntentsBitField.Flags.GuildMessages,
        Discord.IntentsBitField.Flags.MessageContent,
        Discord.IntentsBitField.Flags.DirectMessages,
        Discord.IntentsBitField.Flags.GuildMessageReactions
    ],
    partials: [
        Discord.Partials.Channel,
        Discord.Partials.Reaction,
        Discord.Partials.GuildMember,
        Discord.Partials.Message
    ],
    rest: {
        offset: 250,
        retries: 10
    },
    closeTimeout: 10000,
    makeCache: Discord.Options.cacheEverything(),
    sweepers: {
        ...Discord.Options.DefaultSweeperSettings,
        messages: discordCacheItemsWeDontWant,
        emojis: discordCacheItemsWeDontWant,
        reactions: discordCacheItemsWeDontWant,
        stageInstances: discordCacheItemsWeDontWant,
        invites: discordCacheItemsWeDontWant,
        bans: discordCacheItemsWeDontWant,
        stickers: discordCacheItemsWeDontWant,
        threads: discordCacheItemsWeDontWant,
        threadMembers: discordCacheItemsWeDontWant
    }
} as Discord.ClientOptions;

Versions

discord.js: 14.14.1
node: 21.4.0

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

User, Channel, GuildMember, Message

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildVoiceStates, GuildPresences, GuildMessages, DirectMessages, MessageContent

I have tested this issue on a development release

No response

@Qjuh
Copy link
Contributor

Qjuh commented Jan 4, 2024

Seems like an unavailable Guild. Sweepers should probably check the guild.available flag before trying to sweep it or have a ?? {} fallback for guild[key] in the referenced line.

@JMTK
Copy link
Contributor Author

JMTK commented Jan 4, 2024

I can put together a PR to try it out.

Unfortunately I don't have a great way to confirm that it's because of guild unavailable. Is there are reason those properties wouldn't be set if it's unavailable? Looking at the code for stickers

it looks like it should be setting the property even if the data isn't there.

@JMTK JMTK linked a pull request Jan 4, 2024 that will close this issue
@Jiralite Jiralite added the has PR label Jan 4, 2024
@Qjuh
Copy link
Contributor

Qjuh commented Jan 4, 2024

The code you referenced is in _patch but because of

if (data.unavailable) {
/**
* Whether the guild is available to access. If it is not available, it indicates a server outage
* @type {boolean}
*/
this.available = false;
} else {
this._patch(data);
if (!data.channels) this.available = false;
}
that won’t be called for unavailable Guilds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants