From a60bd19937c5037fbbe1f741c6d6dc707695ca88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Mon, 22 Apr 2024 18:18:12 -0400 Subject: [PATCH] fix: Polls (#189) * feat: add polls * fix: polls builder and send * fix: transformBody --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- src/builders/Poll.ts | 10 +++++++--- src/common/types/write.ts | 4 ++-- src/structures/Interaction.ts | 7 ++++++- src/structures/channels.ts | 2 ++ 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 5747a1e..730501e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "license": "Apache-2.0", "dependencies": { "chokidar": "^3.6.0", - "discord-api-types": "^0.37.80", + "discord-api-types": "^0.37.81", "magic-bytes.js": "^1.10.0", "ts-mixer": "^6.0.4", "ws": "^8.16.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d31860..3b60c5b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: ^3.6.0 version: 3.6.0 discord-api-types: - specifier: ^0.37.80 - version: 0.37.80 + specifier: ^0.37.81 + version: 0.37.81 magic-bytes.js: specifier: ^1.10.0 version: 1.10.0 @@ -362,8 +362,8 @@ packages: resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} engines: {node: '>=0.10'} - discord-api-types@0.37.80: - resolution: {integrity: sha512-aQTZn3C34TOyOmbr9499DgMINZGRmh5K2gT4lZv1/tUb3swPQ6iCthqMVmA5G8d38lTiCwoSyULYAMHifjHh1g==} + discord-api-types@0.37.81: + resolution: {integrity: sha512-GNUnmQeIO5S9lSDbH3hFCIbfhSO62BYtXEFVWMUsV2H7Xx/lCZIe53uQcYtfaO8nxO0kcqNkYgohrhqQMGxthg==} dot-prop@5.3.0: resolution: {integrity: sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==} @@ -1191,7 +1191,7 @@ snapshots: denque@2.1.0: optional: true - discord-api-types@0.37.80: {} + discord-api-types@0.37.81: {} dot-prop@5.3.0: dependencies: diff --git a/src/builders/Poll.ts b/src/builders/Poll.ts index 14ff266..9410404 100644 --- a/src/builders/Poll.ts +++ b/src/builders/Poll.ts @@ -5,20 +5,20 @@ import { resolvePartialEmoji } from '../structures/extra/functions'; export class PollBuilder { constructor( - public data: DeepPartial & { answers: { media: APIPollMedia }[] }> = {}, + public data: DeepPartial & { answers: { poll_media: APIPollMedia }[] }> = {}, ) { this.data.layout_type = PollLayoutType.Default; } addAnswers(...answers: RestOrArray) { this.data.answers = (this.data.answers ?? []).concat( - answers.flat().map(x => ({ media: this.resolvedPollMedia(x) })), + answers.flat().map(x => ({ poll_media: this.resolvedPollMedia(x) })), ); return this; } setAnswers(...answers: RestOrArray) { - this.data.answers = answers.flat().map(x => ({ media: this.resolvedPollMedia(x) })); + this.data.answers = answers.flat().map(x => ({ poll_media: this.resolvedPollMedia(x) })); return this; } @@ -40,6 +40,10 @@ export class PollBuilder { return this; } + toJSON(): RESTAPIPollCreate { + return { ...this.data } as RESTAPIPollCreate; + } + private resolvedPollMedia(data: PollMedia) { if (!data.emoji) return { text: data.text }; const resolve = resolvePartialEmoji(data.emoji); diff --git a/src/common/types/write.ts b/src/common/types/write.ts index 1344a2e..bb3f790 100644 --- a/src/common/types/write.ts +++ b/src/common/types/write.ts @@ -72,8 +72,8 @@ export type ComponentInteractionMessageUpdate = OmitInsert< export type InteractionCreateBodyRequest = OmitInsert< APIInteractionResponseChannelMessageWithSource['data'], - 'components' | 'embeds', - ResolverProps + 'components' | 'embeds' | 'poll', + SendResolverProps >; export type ModalCreateBodyRequest = APIModalInteractionResponse['data'] | Modal; diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index 92a6386..3cdb14a 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -121,7 +121,8 @@ export class BaseInteraction< case InteractionResponseType.DeferredChannelMessageWithSource: return body; case InteractionResponseType.ChannelMessageWithSource: - case InteractionResponseType.UpdateMessage: + case InteractionResponseType.UpdateMessage: { + const poll = (body as InteractionCreateBodyRequest).poll; return { type: body.type, //@ts-ignore @@ -131,8 +132,10 @@ export class BaseInteraction< components: body.data?.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, embeds: body.data?.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined, attachments: body.data?.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined, + poll: poll ? ('toJSON' in poll ? poll.toJSON() : poll) : undefined, }, }; + } case InteractionResponseType.Modal: return { type: body.type, @@ -162,10 +165,12 @@ export class BaseInteraction< | MessageCreateBodyRequest | MessageWebhookCreateBodyRequest, ) { + const poll = (body as MessageWebhookCreateBodyRequest).poll; return { ...body, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, embeds: body?.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined, + poll: poll ? ('toJSON' in poll ? poll.toJSON() : poll) : undefined, // attachments: body.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined, } as T; } diff --git a/src/structures/channels.ts b/src/structures/channels.ts index c99b15c..58d71ce 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -251,11 +251,13 @@ export class MessagesMethods extends DiscordBase { } static transformMessageBody(body: MessageCreateBodyRequest | MessageUpdateBodyRequest) { + const poll = (body as MessageCreateBodyRequest).poll; return { ...body, components: body.components?.map(x => ('toJSON' in x ? x.toJSON() : x)) ?? undefined, embeds: body.embeds?.map(x => (x instanceof Embed ? x.toJSON() : x)) ?? undefined, attachments: body.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined, + poll: poll ? ('toJSON' in poll ? poll.toJSON() : poll) : undefined, } as T; } }