From 2020d00ee4c1ce5f349a1a028f1138efe05f2857 Mon Sep 17 00:00:00 2001 From: purpshell Date: Fri, 23 Dec 2022 13:09:01 +0200 Subject: [PATCH] PART 2: ESLINT, EXAMPLES & MORE --- example.js | 25 +++++++++++++++++++++++++ index.d.ts | 6 ++++++ index.js | 5 ++++- package.json | 4 +--- src/Client.js | 3 +-- src/structures/Message.js | 8 ++++---- src/structures/PollVote.js | 6 +++++- src/util/Injected.js | 6 +++--- 8 files changed, 49 insertions(+), 14 deletions(-) diff --git a/example.js b/example.js index 99dc7ea..1dcd7a7 100644 --- a/example.js +++ b/example.js @@ -197,6 +197,27 @@ client.on('message', async msg => { client.sendMessage(msg.from, list); } else if (msg.body === '!reaction') { msg.react('👍'); + } else if (msg.body.startsWith('!vote ')) { + if (msg.hasQuotedMsg) { + const quotedMsg = await msg.getQuotedMessage(); + if (quotedMsg.type === 'poll_creation') { + const options = msg.body.slice(6).split('//'); + const voteCount = {}; + for (const pollVote of quotedMsg.pollVotes) { + for (const selectedOption of pollVote.selectedOptions) { + if (!voteCount[selectedOption]) voteCount[selectedOption] = 0; + voteCount[selectedOption]++; + } + } + const voteCountStr = Object.entries(voteCount).map(([vote, number]) => ` -${vote}: ${number}`).join('\n'); + quotedMsg.reply( + `Voting to poll ${quotedMsg.body}, with options: ${options.join(', ')}\ncurrent vote count:\n${voteCountStr}` + ); + quotedMsg.vote(options); + } else { + msg.reply('Usage: !vote TEST1//TEST2'); + } + } } }); @@ -253,6 +274,10 @@ client.on('group_update', (notification) => { console.log('update', notification); }); +client.on('poll_vote', (vote) => { + console.log(`Vote received, from ${vote.sender}, ${vote.selectedOptions.map(a => ` - ${a}`).join('\n')}`); +}); + client.on('change_state', state => { console.log('CHANGE STATE', state ); }); diff --git a/index.d.ts b/index.d.ts index be5089a..995dba6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1386,6 +1386,12 @@ declare namespace WAWebJS { senderId: string ack?: number } + + export class PollVote { + selectedOptions: string[] + sender: string + senderTimestampMs: number + } } export = WAWebJS diff --git a/index.js b/index.js index b498064..86bb594 100644 --- a/index.js +++ b/index.js @@ -21,12 +21,15 @@ module.exports = { ProductMetadata: require('./src/structures/ProductMetadata'), List: require('./src/structures/List'), Buttons: require('./src/structures/Buttons'), + PollVote: require('./src/structures/PollVote'), + Call: require('./src/structures/Call'), + // Auth Strategies NoAuth: require('./src/authStrategies/NoAuth'), LocalAuth: require('./src/authStrategies/LocalAuth'), RemoteAuth: require('./src/authStrategies/RemoteAuth'), LegacySessionAuth: require('./src/authStrategies/LegacySessionAuth'), - + ...Constants }; diff --git a/package.json b/package.json index 450d31f..8d0cb3a 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,10 @@ "homepage": "https://wwebjs.dev/", "dependencies": { "@pedroslopez/moduleraid": "^5.0.2", - "fluent-ffmpeg": "^2.1.2", "jsqr": "^1.3.1", "mime": "^3.0.0", "node-fetch": "^2.6.5", - "node-webpmux": "^3.1.0", - "puppeteer": "^13.0.0" + "node-webpmux": "^3.1.0" }, "devDependencies": { "@types/node-fetch": "^2.5.12", diff --git a/src/Client.js b/src/Client.js index 0182e2a..75235cb 100644 --- a/src/Client.js +++ b/src/Client.js @@ -543,8 +543,7 @@ class Client extends EventEmitter { } } }); - window.Store.PollVote.on('add', (vote) => window.onPollVote) - + window.Store.PollVote.on('add', (vote) => window.onPollVote(vote)); { const module = window.Store.createOrUpdateReactionsModule; const ogMethod = module.createOrUpdateReactions; diff --git a/src/structures/Message.js b/src/structures/Message.js index 5e662c0..fc6c496 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -50,7 +50,7 @@ class Message extends Base { * Message content * @type {string} */ - this.body = this.hasMedia ? data.caption || '' : data.body || ''; + this.body = this.hasMedia ? data.caption || '' : data.body || data.pollName || ''; /** * Message type @@ -250,7 +250,7 @@ class Message extends Base { /** Current poll votes, refresh with Message.refreshPollVotes() */ this.pollVotes = data.pollVotes.map((pollVote) => { - return new PollVote(this.client, pollVote); + return new PollVote(this.client, {...pollVote, pollCreationMessage: this}); }); } @@ -549,10 +549,10 @@ class Message extends Base { async refreshPollVotes() { if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message'; const pollVotes = await this.client.evaluate((parentMsgId) => { - return Store.PollVote.getForParent(parentMsgId).getModelsArray().map(a => a.serialize()) + return window.Store.PollVote.getForParent(parentMsgId).getModelsArray().map(a => a.serialize()); }, this.id); this.pollVotes = pollVotes.map((pollVote) => { - return new PollVote(this.client, pollVote); + return new PollVote(this.client, {...pollVote, pollCreationMessage: this}); }); return; } diff --git a/src/structures/PollVote.js b/src/structures/PollVote.js index c3f7a9a..c9cd44e 100644 --- a/src/structures/PollVote.js +++ b/src/structures/PollVote.js @@ -1,5 +1,6 @@ 'use strict'; +const { Message } = require('.'); const Base = require('./Base'); /** @@ -20,11 +21,14 @@ class PollVote extends Base { }); /** Sender of the Poll vote */ - this.sender = data.sender; + this.sender = data.sender._serialized; /** Timestamp of the time it was sent in milliseconds */ this.senderTimestampMs = data.senderTimestampMs; + /** The poll creation message associated with the poll vote */ + this.parentPollMessage = new Message(this.client, data.pollCreationMessage); + return super._patch(data); } } diff --git a/src/util/Injected.js b/src/util/Injected.js index 0aeac79..db051e2 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -407,7 +407,7 @@ exports.LoadUtils = () => { } if (msg.type == 'poll_creation') { - msg.pollVotes = Store.PollVote.getForParent(msg.id).getModelsArray().map(a => a.serialize()); + msg.pollVotes = window.Store.PollVote.getForParent(msg.id).getModelsArray().map(a => a.serialize()); } delete msg.pendingAckUpdate; @@ -636,7 +636,7 @@ exports.LoadUtils = () => { for (const option of selectedOptions) { if (a.name == option) localIdSet.add(a.localId); } - }) + }); await window.Store.SendVote.sendVote(msg, localIdSet); - } + }; };