-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
536 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/discord.js/src/client/actions/MessagePollVoteAdd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const Events = require('../../util/Events'); | ||
|
||
class MessagePollVoteAddAction extends Action { | ||
handle(data) { | ||
const channel = this.getChannel(data); | ||
if (!channel?.isTextBased()) return false; | ||
|
||
const message = this.getMessage(data, channel); | ||
if (!message) return false; | ||
|
||
const { poll } = message; | ||
|
||
const answer = poll.answers.get(data.answer_id); | ||
if (!answer) return false; | ||
|
||
answer.voteCount++; | ||
|
||
/** | ||
* Emitted whenever a user votes in a poll. | ||
* @event Client#messagePollVoteAdd | ||
* @param {PollAnswer} pollAnswer The answer that was voted on | ||
* @param {Snowflake} userId The id of the user that voted | ||
*/ | ||
this.client.emit(Events.MessagePollVoteAdd, answer, data.user_id); | ||
|
||
return { poll }; | ||
} | ||
} | ||
|
||
module.exports = MessagePollVoteAddAction; |
33 changes: 33 additions & 0 deletions
33
packages/discord.js/src/client/actions/MessagePollVoteRemove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const Events = require('../../util/Events'); | ||
|
||
class MessagePollVoteRemoveAction extends Action { | ||
handle(data) { | ||
const channel = this.getChannel(data); | ||
if (!channel?.isTextBased()) return false; | ||
|
||
const message = this.getMessage(data, channel); | ||
if (!message) return false; | ||
|
||
const { poll } = message; | ||
|
||
const answer = poll.answers.get(data.answer_id); | ||
if (!answer) return false; | ||
|
||
answer.voteCount--; | ||
|
||
/** | ||
* Emitted whenever a user removes their vote in a poll. | ||
* @event Client#messagePollVoteRemove | ||
* @param {PollAnswer} pollAnswer The answer where the vote was removed | ||
* @param {Snowflake} userId The id of the user that removed their vote | ||
*/ | ||
this.client.emit(Events.MessagePollVoteRemove, answer, data.user_id); | ||
|
||
return { poll }; | ||
} | ||
} | ||
|
||
module.exports = MessagePollVoteRemoveAction; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/MESSAGE_POLL_VOTE_ADD.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.MessagePollVoteAdd.handle(packet.d); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/discord.js/src/client/websocket/handlers/MESSAGE_POLL_VOTE_REMOVE.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.MessagePollVoteRemove.handle(packet.d); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.