discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Cryptic error from MessageCreateAction.handle during regular operation #7133

Closed kevinlul closed 2 years ago

kevinlul commented 2 years ago

Issue description

On December 20, we encountered two cryptic crashes that we have never seen before from Discord.js internals. They occurred within a minute of each other and we've never seen this before with our use of Discord.js this past year. Timestamps are in UTC.

Dec 20 18:01:38

TypeError: Cannot read properties of undefined (reading 'cache')
    at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:13:41)
    at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:345:31)
    at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:443:22)
    at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:300:10)
    at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:983:20)
    at Receiver.emit (node:events:390:28)
    at Receiver.dataMessage (/app/node_modules/ws/lib/receiver.js:517:14)

Dec 20 18:02:30

TypeError: Cannot read properties of undefined (reading 'cache')
    at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:13:41)
    at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:345:31)
    at Immediate.<anonymous> (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:340:14)
    at processImmediate (node:internal/timers:464:21)

I'm not sure how to reproduce this. It hasn't recurred again, which is good, but it is still not good for regular bot operation to experience an arbitrary crash. Might the official rollout of Guild Member Timeouts have caused something weird?

Code sample

No response

discord.js version

13.1.0

Node.js version

v16.13.1, TypeScript 4.4.3

Operating system

Docker version 20.10.2, build 2291f61 (base image node:16), host Ubuntu 18.04.6 LTS (GNU/Linux 4.15.0-161-generic x86_64)

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

CHANNEL

Which gateway intents are you subscribing to?

GUILDS, GUILD_MESSAGES, DIRECT_MESSAGES

I have tested this issue on a development release

No response

Jiralite commented 2 years ago

Update to discord.js 13.3.1.

kevinlul commented 2 years ago

I understand the merits of updating to the latest version, but can you confirm these are known crashes that were fixed or is it just a matter of best practice?

Jiralite commented 2 years ago

The stack trace your error comes from is relevant to these lines of code at a point in time:

https://github.com/discordjs/discord.js/blob/95d2a4d35ef30d83a2e9ca82b88c978f35c5e1e0/src/client/actions/MessageCreate.js#L12-L16

So in line 13 there, you can see we assume there is a channel with messages always. However, as of 13.3.1:

https://github.com/discordjs/discord.js/blob/5ec04e077bbbb9799f3ef135cade84b77346ef20/src/client/actions/MessageCreate.js#L11-L15

Line 13 checks if a channel is actually text-based. If it is not, we safely return out before trying to access the cache. Ergo, yes this will fix your issue. This fix is present from 13.3.1 onwards.

kyranet commented 2 years ago

Also see #7130.

kevinlul commented 2 years ago

Thanks, we'll upgrade then.