feat+fix: send vote messages & vote update event (#3913)
* add send vote function * Update index.d.ts * fix and add required modules * fix poll update in group chat * adjustments to improvements * add example usages * fix eslint --------- Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
This commit is contained in:
parent
05f651b521
commit
0c36613cc7
1 changed files with 51 additions and 11 deletions
|
|
@ -749,6 +749,32 @@ class Message extends Base {
|
|||
async getPollVotes() {
|
||||
return await this.client.getPollVotes(this.id._serialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send votes to the poll message
|
||||
* @param {Array<string>} selectedOptions Array of options selected.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
async vote(selectedOptions) {
|
||||
if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message';
|
||||
|
||||
await this.client.pupPage.evaluate(async (messageId, votes) => {
|
||||
if (!messageId) return null;
|
||||
if (!Array.isArray(votes)) votes = [votes];
|
||||
let localIdSet = new Set();
|
||||
const msg =
|
||||
window.Store.Msg.get(messageId) || (await window.Store.Msg.getMessagesById([messageId]))?.messages?.[0];
|
||||
if (!msg) return null;
|
||||
|
||||
msg.pollOptions.forEach(a => {
|
||||
for (const option of votes) {
|
||||
if (a.name === option) localIdSet.add(a.localId);
|
||||
}
|
||||
});
|
||||
|
||||
await window.Store.PollsSendVote.sendVote(msg, localIdSet);
|
||||
}, this.id._serialized, selectedOptions);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
|
|
|
|||
Loading…
Reference in a new issue