From 3254e206a785700d7ab8ec68288cea3d5137de86 Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:05:43 -0400 Subject: [PATCH 1/4] fix: use answer_id from data, not options --- interactions/api/events/processors/message_events.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interactions/api/events/processors/message_events.py b/interactions/api/events/processors/message_events.py index 74a10cbde..d8a8b5d7f 100644 --- a/interactions/api/events/processors/message_events.py +++ b/interactions/api/events/processors/message_events.py @@ -99,7 +99,7 @@ async def _on_raw_message_poll_vote_add(self, event: "RawGatewayEvent") -> None: event.data["channel_id"], event.data["message_id"], event.data["user_id"], - event.data["option"], + event.data["answer_id"], ) ) @@ -118,6 +118,6 @@ async def _on_raw_message_poll_vote_remove(self, event: "RawGatewayEvent") -> No event.data["channel_id"], event.data["message_id"], event.data["user_id"], - event.data["option"], + event.data["answer_id"], ) ) From c62de65eb0d465ef42cf6ec35dc91b3595aaba23 Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:06:46 -0400 Subject: [PATCH 2/4] fix: correctly deserialize question for polls --- interactions/models/discord/poll.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/models/discord/poll.py b/interactions/models/discord/poll.py index 7498e0c14..9051791ef 100644 --- a/interactions/models/discord/poll.py +++ b/interactions/models/discord/poll.py @@ -86,7 +86,7 @@ class PollResults(DictSerializationMixin): @attrs.define(eq=False, order=False, hash=False, kw_only=True) class Poll(DictSerializationMixin): - question: PollMedia = attrs.field(repr=False) + question: PollMedia = attrs.field(repr=False, converter=PollMedia.from_dict) """The question of the poll. Only text media is supported.""" answers: list[PollAnswer] = attrs.field(repr=False, factory=list, converter=PollAnswer.from_list) """Each of the answers available in the poll, up to 10.""" From 26358368d1cf4ce1cde8bc5df9807c70c3522cce Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Sun, 30 Jun 2024 14:00:08 -0400 Subject: [PATCH 3/4] ci: add tests for polls --- tests/test_bot.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 9e2f8c7c6..5735f4525 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -3,7 +3,7 @@ import os from asyncio import AbstractEventLoop from contextlib import suppress -from datetime import datetime +from datetime import datetime, timedelta import pytest import pytest_asyncio @@ -33,6 +33,8 @@ ParagraphText, Message, GuildVoice, + Poll, + PollMedia, ) from interactions.models.discord.asset import Asset from interactions.models.discord.components import ActionRow, Button, StringSelectMenu @@ -432,6 +434,84 @@ async def test_components(bot: Client, channel: GuildText) -> None: await thread.delete() +@pytest.mark.asyncio +async def test_polls(bot: Client, channel: GuildText) -> None: + msg = await channel.send("Polls Tests") + thread = await msg.create_thread("Test Thread") + + try: + poll_1 = Poll.create("Test Poll", duration=1, answers=["Answer 1", "Answer 2"]) + assert poll_1.to_dict() == { + "question": {"text": "Test Poll"}, + "layout_type": 1, + "duration": 1, + "allow_multiselect": False, + "answers": [{"poll_media": {"text": "Answer 1"}}, {"poll_media": {"text": "Answer 2"}}], + } + msg_1 = await thread.send(poll=poll_1) + + assert msg_1.poll is not None + assert msg_1.poll.question.to_dict() == PollMedia(text="Test Poll").to_dict() + assert msg_1.poll.expiry <= msg_1.created_at + timedelta(hours=1, minutes=1) + poll_1_answer_medias = [poll_answer.poll_media.to_dict() for poll_answer in msg_1.poll.answers] + assert poll_1_answer_medias == [ + PollMedia.create(text="Answer 1").to_dict(), + PollMedia.create(text="Answer 2").to_dict(), + ] + + poll_2 = Poll.create("Test Poll 2", duration=1, allow_multiselect=True) + poll_2.add_answer("Answer 1") + poll_2.add_answer("Answer 2") + assert poll_2.to_dict() == { + "question": {"text": "Test Poll 2"}, + "layout_type": 1, + "duration": 1, + "allow_multiselect": True, + "answers": [{"poll_media": {"text": "Answer 1"}}, {"poll_media": {"text": "Answer 2"}}], + } + msg_2 = await thread.send(poll=poll_2) + + assert msg_2.poll is not None + assert msg_2.poll.question.to_dict() == PollMedia(text="Test Poll 2").to_dict() + assert msg_2.poll.expiry <= msg_2.created_at + timedelta(hours=1, minutes=1) + assert msg_2.poll.allow_multiselect + poll_2_answer_medias = [poll_answer.poll_media.to_dict() for poll_answer in msg_2.poll.answers] + assert poll_2_answer_medias == [ + PollMedia.create(text="Answer 1").to_dict(), + PollMedia.create(text="Answer 2").to_dict(), + ] + + poll_3 = Poll.create( + "Test Poll 3", + duration=1, + answers=[PollMedia.create(text="One", emoji="1️⃣"), PollMedia.create(text="Two", emoji="2️⃣")], + ) + assert poll_3.to_dict() == { + "question": {"text": "Test Poll 3"}, + "layout_type": 1, + "duration": 1, + "allow_multiselect": False, + "answers": [ + {"poll_media": {"text": "One", "emoji": {"name": "1️⃣", "animated": False}}}, + {"poll_media": {"text": "Two", "emoji": {"name": "2️⃣", "animated": False}}}, + ], + } + msg_3 = await thread.send(poll=poll_3) + + assert msg_3.poll is not None + assert msg_3.poll.question.to_dict() == PollMedia(text="Test Poll 3").to_dict() + assert msg_3.poll.expiry <= msg_3.created_at + timedelta(hours=1, minutes=1) + poll_3_answer_medias = [poll_answer.poll_media.to_dict() for poll_answer in msg_3.poll.answers] + assert poll_3_answer_medias == [ + PollMedia.create(text="One", emoji="1️⃣").to_dict(), + PollMedia.create(text="Two", emoji="2️⃣").to_dict(), + ] + + finally: + with suppress(interactions.errors.NotFound): + await thread.delete() + + @pytest.mark.asyncio async def test_webhooks(bot: Client, guild: Guild, channel: GuildText) -> None: test_thread = await channel.create_thread("Test Thread") From 91f2239b4744d51bc5916040102856efeb79f84c Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Sun, 30 Jun 2024 14:12:26 -0400 Subject: [PATCH 4/4] test: make poll dict test more resilient --- tests/test_bot.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/test_bot.py b/tests/test_bot.py index 5735f4525..33267eaf5 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -441,13 +441,17 @@ async def test_polls(bot: Client, channel: GuildText) -> None: try: poll_1 = Poll.create("Test Poll", duration=1, answers=["Answer 1", "Answer 2"]) - assert poll_1.to_dict() == { + test_data_1 = { "question": {"text": "Test Poll"}, "layout_type": 1, "duration": 1, "allow_multiselect": False, "answers": [{"poll_media": {"text": "Answer 1"}}, {"poll_media": {"text": "Answer 2"}}], } + poll_1_dict = poll_1.to_dict() + for key in poll_1_dict.keys(): + assert poll_1_dict[key] == test_data_1[key] + msg_1 = await thread.send(poll=poll_1) assert msg_1.poll is not None @@ -462,13 +466,16 @@ async def test_polls(bot: Client, channel: GuildText) -> None: poll_2 = Poll.create("Test Poll 2", duration=1, allow_multiselect=True) poll_2.add_answer("Answer 1") poll_2.add_answer("Answer 2") - assert poll_2.to_dict() == { + test_data_2 = { "question": {"text": "Test Poll 2"}, "layout_type": 1, "duration": 1, "allow_multiselect": True, "answers": [{"poll_media": {"text": "Answer 1"}}, {"poll_media": {"text": "Answer 2"}}], } + poll_2_dict = poll_2.to_dict() + for key in poll_2_dict.keys(): + assert poll_2_dict[key] == test_data_2[key] msg_2 = await thread.send(poll=poll_2) assert msg_2.poll is not None @@ -486,7 +493,7 @@ async def test_polls(bot: Client, channel: GuildText) -> None: duration=1, answers=[PollMedia.create(text="One", emoji="1️⃣"), PollMedia.create(text="Two", emoji="2️⃣")], ) - assert poll_3.to_dict() == { + test_data_3 = { "question": {"text": "Test Poll 3"}, "layout_type": 1, "duration": 1, @@ -496,6 +503,10 @@ async def test_polls(bot: Client, channel: GuildText) -> None: {"poll_media": {"text": "Two", "emoji": {"name": "2️⃣", "animated": False}}}, ], } + poll_3_dict = poll_3.to_dict() + for key in poll_3_dict.keys(): + assert poll_3_dict[key] == test_data_3[key] + msg_3 = await thread.send(poll=poll_3) assert msg_3.poll is not None