Skip to content

Commit

Permalink
Add pollResults to Message
Browse files Browse the repository at this point in the history
Closes #187
  • Loading branch information
DonovanDMC committed Aug 3, 2024
1 parent 22ac8dc commit fad8c12
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
22 changes: 21 additions & 1 deletion lib/structures/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import type {
GetPollAnswerUsersOptions,
Call,
MessageSnapshot,
MessageMentions
MessageMentions,
MessagePollResults
} from "../types/channels";
import type { RawMember } from "../types/guilds";
import type { DeleteWebhookMessageOptions, EditWebhookMessageOptions } from "../types/webhooks";
Expand Down Expand Up @@ -108,6 +109,8 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
pinned: boolean;
/** The poll on this message, if any. */
poll?: Poll;
/** The poll results extracted from the embeds of this message. */
pollResults?: MessagePollResults;
/** This message's relative position, if in a thread. */
position?: number;
/** The reactions on this message. */
Expand Down Expand Up @@ -224,6 +227,21 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
}
if (data.embeds !== undefined) {
this.embeds = this.client.util.embedsToParsed(data.embeds);
const pollResultEmbed = this.embeds.find(embed => embed.type === "poll_result");
if (pollResultEmbed) {
const questionText = pollResultEmbed.fields!.find(field => field.name === "poll_question_text")!.value;
const totalVotes = pollResultEmbed.fields!.find(field => field.name === "total_votes")!.value;
const victorAnswerID = pollResultEmbed.fields!.find(field => field.name === "victor_answer_id")?.value;
const victorAnswerText = pollResultEmbed.fields!.find(field => field.name === "victor_answer_text")?.value;
const victorAnswerVotes = pollResultEmbed.fields!.find(field => field.name === "victor_answer_votes")!.value;
this.pollResults = {
questionText,
totalVotes: Number(totalVotes),
victorAnswerID: victorAnswerID === undefined ? undefined : Number(victorAnswerID),
victorAnswerText: victorAnswerText === undefined ? undefined : victorAnswerText,
victorAnswerVotes: Number(victorAnswerVotes)
};
}
}
if (data.flags !== undefined) {
this.flags = data.flags;
Expand Down Expand Up @@ -565,6 +583,8 @@ export default class Message<T extends AnyTextableChannel | Uncached = AnyTextab
nonce: this.nonce,
pinned: this.pinned,
position: this.position,
poll: this.poll?.toJSON(),
pollResults: this.pollResults,
reactions: this.reactions,
referencedMessage: this.referencedMessage?.toJSON(),
stickerItems: this.stickerItems,
Expand Down
11 changes: 10 additions & 1 deletion lib/types/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export interface Embed extends EmbedBase {
video?: EmbedVideo;
}

export type EmbedType = "rich" | "image" | "video" | "gifv" | "article" | "link";
export type EmbedType = "rich" | "image" | "video" | "gifv" | "article" | "link" | "poll_result";

export interface EmbedAuthorBase {
name: string;
Expand Down Expand Up @@ -1306,3 +1306,12 @@ export interface MessageMentions {
/** The users mentioned in this message. */
users: Array<User>;
}


export interface MessagePollResults {
questionText: string;
totalVotes: number;
victorAnswerID?: number;
victorAnswerText?: string;
victorAnswerVotes: number;
}
5 changes: 4 additions & 1 deletion lib/types/json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import type {
ThreadOnlyChannels,
PollQuestion,
PollResults,
PollAnswer
PollAnswer,
MessagePollResults
} from "./channels";
import type { ScheduledEventEntityMetadata } from "./scheduled-events";
import type { AvatarDecorationData } from "./users";
Expand Down Expand Up @@ -518,6 +519,8 @@ export interface JSONMessage extends JSONBase {
}>;
nonce?: number | string;
pinned: boolean;
poll?: JSONPoll;
pollResults?: MessagePollResults;
position?: number;
reactions: Array<MessageReaction>;
referencedMessage?: JSONMessage | null;
Expand Down

0 comments on commit fad8c12

Please sign in to comment.