From 3234fc6b3b5d839cd48e4c65e31c4426db4103ff Mon Sep 17 00:00:00 2001 From: Jacob Morrison Date: Thu, 27 Mar 2025 14:01:05 -0400 Subject: [PATCH] fix(Poll): ensure `this.answers` is set before we reference it (#10809) * Ensure his.answers is set sooner if it's null during a patch * Move data.answers block up as well to ensure the patched answers are set * Ensure collection is set in constructor instead --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/src/structures/Poll.js | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/discord.js/src/structures/Poll.js b/packages/discord.js/src/structures/Poll.js index 3fd0630e4..202c94ade 100644 --- a/packages/discord.js/src/structures/Poll.js +++ b/packages/discord.js/src/structures/Poll.js @@ -43,10 +43,27 @@ class Poll extends Base { Object.defineProperty(this, 'message', { value: message }); + /** + * The answers of this poll + * @type {Collection} + */ + 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 @@ -111,23 +128,6 @@ class Poll extends Base { text: null, }; } - - /** - * The answers of this poll - * @type {Collection} - */ - 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)); - } - } - } } /**