feat: polls (#10185)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Almeida
2024-05-01 00:21:07 +01:00
committed by GitHub
parent c7adce351a
commit a1aeaeb9d8
23 changed files with 562 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import { InteractionsAPI } from './interactions.js';
import { InvitesAPI } from './invite.js';
import { MonetizationAPI } from './monetization.js';
import { OAuth2API } from './oauth2.js';
import { PollAPI } from './poll.js';
import { RoleConnectionsAPI } from './roleConnections.js';
import { StageInstancesAPI } from './stageInstances.js';
import { StickersAPI } from './sticker.js';
@@ -23,6 +24,7 @@ export * from './interactions.js';
export * from './invite.js';
export * from './monetization.js';
export * from './oauth2.js';
export * from './poll.js';
export * from './roleConnections.js';
export * from './stageInstances.js';
export * from './sticker.js';
@@ -48,6 +50,8 @@ export class API {
public readonly oauth2: OAuth2API;
public readonly poll: PollAPI;
public readonly roleConnections: RoleConnectionsAPI;
public readonly stageInstances: StageInstancesAPI;
@@ -69,8 +73,9 @@ export class API {
this.guilds = new GuildsAPI(rest);
this.invites = new InvitesAPI(rest);
this.monetization = new MonetizationAPI(rest);
this.roleConnections = new RoleConnectionsAPI(rest);
this.oauth2 = new OAuth2API(rest);
this.poll = new PollAPI(rest);
this.roleConnections = new RoleConnectionsAPI(rest);
this.stageInstances = new StageInstancesAPI(rest);
this.stickers = new StickersAPI(rest);
this.threads = new ThreadsAPI(rest);

View File

@@ -0,0 +1,51 @@
/* eslint-disable jsdoc/check-param-names */
import { makeURLSearchParams, type RequestData, type REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPIPollAnswerVotersQuery,
type RESTGetAPIPollAnswerVotersResult,
type RESTPostAPIPollExpireResult,
type Snowflake,
} from 'discord-api-types/v10';
export class PollAPI {
public constructor(private readonly rest: REST) {}
/**
* Gets the list of users that voted for a specific answer in a poll
*
* @see {@link https://discord.com/developers/docs/resources/poll#get-answer-voters}
* @param channelId - The id of the channel containing the message
* @param messageId - The id of the message containing the poll
* @param answerId - The id of the answer to get voters for
* @param query - The query for getting the list of voters
* @param options - The options for getting the list of voters
*/
public async getAnswerVoters(
channelId: Snowflake,
messageId: Snowflake,
answerId: number,
query: RESTGetAPIPollAnswerVotersQuery,
{ signal }: Pick<RequestData, 'signal'> = {},
) {
return this.rest.get(Routes.pollAnswerVoters(channelId, messageId, answerId), {
signal,
query: makeURLSearchParams(query),
}) as Promise<RESTGetAPIPollAnswerVotersResult>;
}
/**
* Immediately expires (i.e. ends) a poll
*
* @see {@link https://discord.com/developers/docs/resources/poll#expire-poll}
* @param channelId - The id of the channel containing the message
* @param messageId - The id of the message containing the poll
* @param options - The options for expiring the poll
*/
public async expirePoll(channelId: Snowflake, messageId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.post(Routes.expirePoll(channelId, messageId), {
signal,
}) as Promise<RESTPostAPIPollExpireResult>;
}
}

View File

@@ -1,4 +1,4 @@
import { setTimeout, clearTimeout } from 'node:timers';
import { clearTimeout, setTimeout } from 'node:timers';
import type { REST } from '@discordjs/rest';
import { calculateShardId } from '@discordjs/util';
import { WebSocketShardEvents } from '@discordjs/ws';
@@ -49,6 +49,7 @@ import {
type GatewayMessageCreateDispatchData,
type GatewayMessageDeleteBulkDispatchData,
type GatewayMessageDeleteDispatchData,
type GatewayMessagePollVoteDispatchData,
type GatewayMessageReactionAddDispatchData,
type GatewayMessageReactionRemoveAllDispatchData,
type GatewayMessageReactionRemoveDispatchData,
@@ -143,6 +144,8 @@ export interface MappedEvents {
[GatewayDispatchEvents.MessageCreate]: [WithIntrinsicProps<GatewayMessageCreateDispatchData>];
[GatewayDispatchEvents.MessageDelete]: [WithIntrinsicProps<GatewayMessageDeleteDispatchData>];
[GatewayDispatchEvents.MessageDeleteBulk]: [WithIntrinsicProps<GatewayMessageDeleteBulkDispatchData>];
[GatewayDispatchEvents.MessagePollVoteAdd]: [WithIntrinsicProps<GatewayMessagePollVoteDispatchData>];
[GatewayDispatchEvents.MessagePollVoteRemove]: [WithIntrinsicProps<GatewayMessagePollVoteDispatchData>];
[GatewayDispatchEvents.MessageReactionAdd]: [WithIntrinsicProps<GatewayMessageReactionAddDispatchData>];
[GatewayDispatchEvents.MessageReactionRemove]: [WithIntrinsicProps<GatewayMessageReactionRemoveDispatchData>];
[GatewayDispatchEvents.MessageReactionRemoveAll]: [WithIntrinsicProps<GatewayMessageReactionRemoveAllDispatchData>];
@@ -198,9 +201,8 @@ export class Client extends AsyncEventEmitter<MappedEvents> {
this.gateway.on(WebSocketShardEvents.Dispatch, ({ data: dispatch, shardId }) => {
this.emit(
// TODO: This comment will have to be moved down once the new Poll events are added to the `ManagerShardEventsMap`
// @ts-expect-error event props can't be resolved properly, but they are correct
dispatch.t,
// @ts-expect-error event props can't be resolved properly, but they are correct
this.wrapIntrinsicProps(dispatch.d, shardId),
);
});