Skip to content

fix(Poll): ensure this.answers is set before we reference it #10809

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

Merged
merged 4 commits into from
Mar 27, 2025

Conversation

JMTK
Copy link
Contributor

@JMTK JMTK commented Mar 19, 2025

Please describe the changes this PR makes and why it should be merged:
I use polls but I don't subscribe to the messageUpdate event, but I've been occassionally seeing this pop up. It doesn't seem to happen for every poll so I'm not 100% sure on reproduce steps. I think it's a minor bug that stems from the overhaul #10328

[UnhandledPromise] TypeError: Cannot read properties of undefined (reading 'get')

Promise {
<rejected> TypeError: Cannot read properties of undefined (reading 'get')
    at Poll._patch (/app/node_modules/discord.js/src/structures/Poll.js:58:37)
    at new Poll (/app/node_modules/discord.js/src/structures/Poll.js:46:10)
    at Message._patch (/app/node_modules/discord.js/src/structures/Message.js:425:21)
    at Message._update (/app/node_modules/discord.js/src/structures/Base.js:30:10)
    at MessageUpdateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageUpdate.js:14:29)
    at module.exports [as MESSAGE_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_UPDATE.js:6:57)
    at Client._handlePacket (/app/node_modules/discord.js/src/client/Client.js:355:33)
    at WebSocketManager.emit (/app/node_modules/@vladfrangu/async_event_emitter/src/index.ts:513:28)
    at WebSocketShard.<anonymous> (/app/node_modules/@discordjs/ws/src/strategies/sharding/SimpleShardingStrategy.ts:33:47)
    at WebSocketShard.emit (/app/node_modules/@vladfrangu/async_event_emitter/src/index.ts:513:28) 

Status and versioning classification:
Patch

@JMTK JMTK requested a review from a team as a code owner March 19, 2025 17:19
Copy link

vercel bot commented Mar 19, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
discord-js ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 5:57pm
discord-js-guide ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 5:57pm

Copy link
Member

@almeidx almeidx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the if (data.answers) block up too

@github-project-automation github-project-automation bot moved this from Todo to Review in Progress in discord.js Mar 19, 2025
@Jiralite Jiralite requested a review from Copilot March 26, 2025 18:16
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an intermittent bug where this.answers was sometimes undefined when referenced in the Poll structure.

  • Moves the initialization of this.answers to the constructor to guarantee its definition.
  • Removes duplicate initialization of this.answers within the _patch method.
Comments suppressed due to low confidence (2)

packages/discord.js/src/structures/Poll.js:50

  • Initializing this.answers in the constructor ensures it is defined before it is referenced in _patch, preventing the potential TypeError.
this.answers = new Collection();

packages/discord.js/src/structures/Poll.js:111

  • The duplicate initialization of this.answers in _patch has been removed as it is no longer necessary and could lead to unintended side effects.
this.answers ??= new Collection();

@github-project-automation github-project-automation bot moved this from Review in Progress to Review Approved in discord.js Mar 27, 2025
@kodiakhq kodiakhq bot merged commit 3234fc6 into discordjs:main Mar 27, 2025
7 checks passed
@github-project-automation github-project-automation bot moved this from Review Approved to Done in discord.js Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants