fix: Polls (#189)

* feat: add polls

* fix: polls builder and send

* fix: transformBody
This commit is contained in:
Marcos Susaña 2024-04-22 18:18:12 -04:00 committed by GitHub
parent f223eb1123
commit a60bd19937
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 23 additions and 12 deletions

View File

@ -21,7 +21,7 @@
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"chokidar": "^3.6.0", "chokidar": "^3.6.0",
"discord-api-types": "^0.37.80", "discord-api-types": "^0.37.81",
"magic-bytes.js": "^1.10.0", "magic-bytes.js": "^1.10.0",
"ts-mixer": "^6.0.4", "ts-mixer": "^6.0.4",
"ws": "^8.16.0" "ws": "^8.16.0"

10
pnpm-lock.yaml generated
View File

@ -12,8 +12,8 @@ importers:
specifier: ^3.6.0 specifier: ^3.6.0
version: 3.6.0 version: 3.6.0
discord-api-types: discord-api-types:
specifier: ^0.37.80 specifier: ^0.37.81
version: 0.37.80 version: 0.37.81
magic-bytes.js: magic-bytes.js:
specifier: ^1.10.0 specifier: ^1.10.0
version: 1.10.0 version: 1.10.0
@ -362,8 +362,8 @@ packages:
resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==}
engines: {node: '>=0.10'} engines: {node: '>=0.10'}
discord-api-types@0.37.80: discord-api-types@0.37.81:
resolution: {integrity: sha512-aQTZn3C34TOyOmbr9499DgMINZGRmh5K2gT4lZv1/tUb3swPQ6iCthqMVmA5G8d38lTiCwoSyULYAMHifjHh1g==} resolution: {integrity: sha512-GNUnmQeIO5S9lSDbH3hFCIbfhSO62BYtXEFVWMUsV2H7Xx/lCZIe53uQcYtfaO8nxO0kcqNkYgohrhqQMGxthg==}
dot-prop@5.3.0: dot-prop@5.3.0:
resolution: {integrity: sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==} resolution: {integrity: sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==}
@ -1191,7 +1191,7 @@ snapshots:
denque@2.1.0: denque@2.1.0:
optional: true optional: true
discord-api-types@0.37.80: {} discord-api-types@0.37.81: {}
dot-prop@5.3.0: dot-prop@5.3.0:
dependencies: dependencies:

View File

@ -5,20 +5,20 @@ import { resolvePartialEmoji } from '../structures/extra/functions';
export class PollBuilder { export class PollBuilder {
constructor( constructor(
public data: DeepPartial<Omit<RESTAPIPollCreate, 'answers'> & { answers: { media: APIPollMedia }[] }> = {}, public data: DeepPartial<Omit<RESTAPIPollCreate, 'answers'> & { answers: { poll_media: APIPollMedia }[] }> = {},
) { ) {
this.data.layout_type = PollLayoutType.Default; this.data.layout_type = PollLayoutType.Default;
} }
addAnswers(...answers: RestOrArray<PollMedia>) { addAnswers(...answers: RestOrArray<PollMedia>) {
this.data.answers = (this.data.answers ?? []).concat( 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; return this;
} }
setAnswers(...answers: RestOrArray<PollMedia>) { setAnswers(...answers: RestOrArray<PollMedia>) {
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; return this;
} }
@ -40,6 +40,10 @@ export class PollBuilder {
return this; return this;
} }
toJSON(): RESTAPIPollCreate {
return { ...this.data } as RESTAPIPollCreate;
}
private resolvedPollMedia(data: PollMedia) { private resolvedPollMedia(data: PollMedia) {
if (!data.emoji) return { text: data.text }; if (!data.emoji) return { text: data.text };
const resolve = resolvePartialEmoji(data.emoji); const resolve = resolvePartialEmoji(data.emoji);

View File

@ -72,8 +72,8 @@ export type ComponentInteractionMessageUpdate = OmitInsert<
export type InteractionCreateBodyRequest = OmitInsert< export type InteractionCreateBodyRequest = OmitInsert<
APIInteractionResponseChannelMessageWithSource['data'], APIInteractionResponseChannelMessageWithSource['data'],
'components' | 'embeds', 'components' | 'embeds' | 'poll',
ResolverProps SendResolverProps
>; >;
export type ModalCreateBodyRequest = APIModalInteractionResponse['data'] | Modal; export type ModalCreateBodyRequest = APIModalInteractionResponse['data'] | Modal;

View File

@ -121,7 +121,8 @@ export class BaseInteraction<
case InteractionResponseType.DeferredChannelMessageWithSource: case InteractionResponseType.DeferredChannelMessageWithSource:
return body; return body;
case InteractionResponseType.ChannelMessageWithSource: case InteractionResponseType.ChannelMessageWithSource:
case InteractionResponseType.UpdateMessage: case InteractionResponseType.UpdateMessage: {
const poll = (body as InteractionCreateBodyRequest).poll;
return { return {
type: body.type, type: body.type,
//@ts-ignore //@ts-ignore
@ -131,8 +132,10 @@ export class BaseInteraction<
components: body.data?.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, 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, 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, attachments: body.data?.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined,
poll: poll ? ('toJSON' in poll ? poll.toJSON() : poll) : undefined,
}, },
}; };
}
case InteractionResponseType.Modal: case InteractionResponseType.Modal:
return { return {
type: body.type, type: body.type,
@ -162,10 +165,12 @@ export class BaseInteraction<
| MessageCreateBodyRequest | MessageCreateBodyRequest
| MessageWebhookCreateBodyRequest, | MessageWebhookCreateBodyRequest,
) { ) {
const poll = (body as MessageWebhookCreateBodyRequest).poll;
return { return {
...body, ...body,
components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined, components: body.components?.map(x => (x instanceof ActionRow ? x.toJSON() : x)) ?? undefined,
embeds: body?.embeds?.map(x => (x instanceof Embed ? 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, // attachments: body.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined,
} as T; } as T;
} }

View File

@ -251,11 +251,13 @@ export class MessagesMethods extends DiscordBase {
} }
static transformMessageBody<T>(body: MessageCreateBodyRequest | MessageUpdateBodyRequest) { static transformMessageBody<T>(body: MessageCreateBodyRequest | MessageUpdateBodyRequest) {
const poll = (body as MessageCreateBodyRequest).poll;
return { return {
...body, ...body,
components: body.components?.map(x => ('toJSON' in x ? x.toJSON() : x)) ?? undefined, components: body.components?.map(x => ('toJSON' in x ? x.toJSON() : x)) ?? undefined,
embeds: body.embeds?.map(x => (x instanceof Embed ? 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, attachments: body.attachments?.map((x, i) => ({ id: i, ...resolveAttachment(x) })) ?? undefined,
poll: poll ? ('toJSON' in poll ? poll.toJSON() : poll) : undefined,
} as T; } as T;
} }
} }