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
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions packages/discord.js/src/structures/Poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,27 @@ class Poll extends Base {

Object.defineProperty(this, 'message', { value: message });

/**
* The answers of this poll
* @type {Collection<number, PollAnswer|PartialPollAnswer>}
*/
this.answers = new Collection();

this._patch(data);
}

_patch(data) {
if (data.answers) {
for (const answer of data.answers) {
const existing = this.answers.get(answer.answer_id);
if (existing) {
existing._patch(answer);
} else {
this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
}
}
}

if (data.results) {
/**
* Whether this poll's results have been precisely counted
Expand Down Expand Up @@ -111,23 +128,6 @@ class Poll extends Base {
text: null,
};
}

/**
* The answers of this poll
* @type {Collection<number, PollAnswer|PartialPollAnswer>}
*/
this.answers ??= new Collection();

if (data.answers) {
for (const answer of data.answers) {
const existing = this.answers.get(answer.answer_id);
if (existing) {
existing._patch(answer);
} else {
this.answers.set(answer.answer_id, new PollAnswer(this.client, answer, this));
}
}
}
}

/**
Expand Down