discordjs / discord.js

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

TypeError: thread.members._add is not a function #10393

Closed JMTK closed 2 months ago

JMTK commented 3 months ago

Which package is this bug report for?

discord.js

Issue description

Creating this issue for posterity, bulk of the discussion and fix were noted here: https://discord.com/channels/222078108977594368/1248260322163294238

After a recent update to @discordjs/ws, it seems like Messages are being instantiated as channels in some very specific cases.

Full trace of the issue is here:

Trace
    at TextChannel._patch (/app/node_modules/discord.js/src/structures/TextChannel.js:15:15)
    at ChannelManager._add (/app/node_modules/discord.js/src/managers/ChannelManager.js:42:27)
    at MessageCreateAction.getPayload (/app/node_modules/discord.js/src/client/actions/Action.js:27:73)
    at MessageCreateAction.getChannel (/app/node_modules/discord.js/src/client/actions/Action.js:44:12)
    at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:9:26)
    at 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:355:31)
    at WebSocketManager.<anonymous> (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12)
    at WebSocketManager.emit (/app/node_modules/@vladfrangu/async_event_emitter/dist/index.cjs:282:31)
    at WebSocketShard.<anonymous> (/app/node_modules/@discordjs/ws/dist/index.js:1190:51)

Sample "TextChannel" that is actually a "thread":

<ref *2> TextChannel {
  type: 0,
  guild: <ref *1> Guild {
    id: '755765126140919881',
    name: '彩虹研究社:rainbow:',
    icon: 'b2ea11110ab979ddda535b7c4479efc9',
    features: [
      'AUTO_MODERATION',
      'ROLE_ICONS',
      'SEVEN_DAY_THREAD_ARCHIVE',
      'ANIMATED_BANNER',
      'NEWS',
      'SOUNDBOARD',
      'BANNER',
      'ANIMATED_ICON',
      'INVITE_SPLASH',
      'CHANNEL_ICON_EMOJIS_GENERATED',
      'THREE_DAY_THREAD_ARCHIVE',
      'MEMBER_PROFILES',
      'COMMUNITY',
      'VANITY_URL',
      'PRIVATE_THREADS'
    ],
    (TRUNCATING SO I CAN PASTE IN DISCORD)
  },
  guildId: '755765126140919881',
  permissionOverwrites: PermissionOverwriteManager { channel: [Circular *2] },
  messages: GuildMessageManager { channel: [Circular *2] },
  threads: GuildTextThreadManager { channel: [Circular *2] },
  nsfw: false,
  flags: ChannelFlagsBitField { bitfield: 0 },
  id: '1262790110647156736',
  parentId: null,
  defaultThreadRateLimitPerUser: null,
  memberCount: 7,
  rawPosition: 4,
  lastMessageId: '1262806103016607784'
}

(Note the memberCount property here means we know it's a thread)

I think the possible problem commit was: https://github.com/discordjs/discord.js/pull/10278

Code sample

Not sure on exactly what the code sample is to reproduce this yet

Versions

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

User, Channel, GuildMember, Message, Reaction, ThreadMember

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildPresences, GuildMessages, DirectMessages

I have tested this issue on a development release

No response

kevinlul commented 3 months ago

Stack trace from another crash

/app/node_modules/discord.js/src/client/actions/Action.js:27
    return this.client.options.partials.includes(partialType) ? manager._add(data, cache) : manager.cache.get(id);
                                                                                                          ^
TypeError: Cannot read properties of undefined (reading 'get')
    at ThreadMembersUpdateAction.getPayload (/app/node_modules/discord.js/src/client/actions/Action.js:27:107)
    at ThreadMembersUpdateAction.getThreadMember (/app/node_modules/discord.js/src/client/actions/Action.js:112:17)
    at /app/node_modules/discord.js/src/client/actions/ThreadMembersUpdate.js:22:35
    at Array.reduce (<anonymous>)
    at ThreadMembersUpdateAction.handle (/app/node_modules/discord.js/src/client/actions/ThreadMembersUpdate.js:21:32)
    at module.exports [as THREAD_MEMBERS_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js:4:38)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31)
    at WebSocketManager.<anonymous> (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12)
    at WebSocketManager.emit (/app/node_modules/@vladfrangu/async_event_emitter/src/index.ts:459:28)
    at WebSocketShard.<anonymous> (/app/node_modules/@discordjs/ws/src/strategies/sharding/SimpleShardingStrategy.ts:32:47)
Qjuh commented 3 months ago

Stack trace from another crash

/app/node_modules/discord.js/src/client/actions/Action.js:27
    return this.client.options.partials.includes(partialType) ? manager._add(data, cache) : manager.cache.get(id);
                                                                                                          ^
TypeError: Cannot read properties of undefined (reading 'get')
    at ThreadMembersUpdateAction.getPayload (/app/node_modules/discord.js/src/client/actions/Action.js:27:107)
    at ThreadMembersUpdateAction.getThreadMember (/app/node_modules/discord.js/src/client/actions/Action.js:112:17)
    at /app/node_modules/discord.js/src/client/actions/ThreadMembersUpdate.js:22:35
    at Array.reduce (<anonymous>)
    at ThreadMembersUpdateAction.handle (/app/node_modules/discord.js/src/client/actions/ThreadMembersUpdate.js:21:32)
    at module.exports [as THREAD_MEMBERS_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js:4:38)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31)
    at WebSocketManager.<anonymous> (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12)
    at WebSocketManager.emit (/app/node_modules/@vladfrangu/async_event_emitter/src/index.ts:459:28)
    at WebSocketShard.<anonymous> (/app/node_modules/@discordjs/ws/src/strategies/sharding/SimpleShardingStrategy.ts:32:47)

Do you by any chance have any custom makeCache: settings in your Client constructor? Because if your crash has the same reason as OP's it would mean you have a <Guild>.members.cache._add method defined, which regular Collection from discord.js doesn't have.

kevinlul commented 3 months ago

I do not: https://github.com/DawnbrandBots/emcee-tournament-bot/blob/master/src/index.ts#L28-L39

I have that on a different bot but that bot has yet to crash because of this despite being in exponentially more servers.

Qjuh commented 3 months ago

I do not: https://github.com/DawnbrandBots/emcee-tournament-bot/blob/master/src/index.ts#L28-L39

I have that on a different bot but that bot has yet to crash because of this despite being in exponentially more servers.

Then linking to the code of the bot that didn't crash won't help much in determining the cause... or did you mean you have makeCache on another bot that doesn't crash, but this one you linked is the one that crashed with said error? Ah, my bad, it's literally the difference of it you have Partials.ThreadMember enabled or not that decides which of them errors.