PART 2: ESLINT, EXAMPLES & MORE

This commit is contained in:
purpshell
2022-12-23 13:09:01 +02:00
parent a519105870
commit 2020d00ee4
8 changed files with 49 additions and 14 deletions

View File

@@ -197,6 +197,27 @@ client.on('message', async msg => {
client.sendMessage(msg.from, list); client.sendMessage(msg.from, list);
} else if (msg.body === '!reaction') { } else if (msg.body === '!reaction') {
msg.react('👍'); 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); 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 => { client.on('change_state', state => {
console.log('CHANGE STATE', state ); console.log('CHANGE STATE', state );
}); });

6
index.d.ts vendored
View File

@@ -1386,6 +1386,12 @@ declare namespace WAWebJS {
senderId: string senderId: string
ack?: number ack?: number
} }
export class PollVote {
selectedOptions: string[]
sender: string
senderTimestampMs: number
}
} }
export = WAWebJS export = WAWebJS

View File

@@ -21,6 +21,9 @@ module.exports = {
ProductMetadata: require('./src/structures/ProductMetadata'), ProductMetadata: require('./src/structures/ProductMetadata'),
List: require('./src/structures/List'), List: require('./src/structures/List'),
Buttons: require('./src/structures/Buttons'), Buttons: require('./src/structures/Buttons'),
PollVote: require('./src/structures/PollVote'),
Call: require('./src/structures/Call'),
// Auth Strategies // Auth Strategies
NoAuth: require('./src/authStrategies/NoAuth'), NoAuth: require('./src/authStrategies/NoAuth'),

View File

@@ -30,12 +30,10 @@
"homepage": "https://wwebjs.dev/", "homepage": "https://wwebjs.dev/",
"dependencies": { "dependencies": {
"@pedroslopez/moduleraid": "^5.0.2", "@pedroslopez/moduleraid": "^5.0.2",
"fluent-ffmpeg": "^2.1.2",
"jsqr": "^1.3.1", "jsqr": "^1.3.1",
"mime": "^3.0.0", "mime": "^3.0.0",
"node-fetch": "^2.6.5", "node-fetch": "^2.6.5",
"node-webpmux": "^3.1.0", "node-webpmux": "^3.1.0"
"puppeteer": "^13.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node-fetch": "^2.5.12", "@types/node-fetch": "^2.5.12",

View File

@@ -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 module = window.Store.createOrUpdateReactionsModule;
const ogMethod = module.createOrUpdateReactions; const ogMethod = module.createOrUpdateReactions;

View File

@@ -50,7 +50,7 @@ class Message extends Base {
* Message content * Message content
* @type {string} * @type {string}
*/ */
this.body = this.hasMedia ? data.caption || '' : data.body || ''; this.body = this.hasMedia ? data.caption || '' : data.body || data.pollName || '';
/** /**
* Message type * Message type
@@ -250,7 +250,7 @@ class Message extends Base {
/** Current poll votes, refresh with Message.refreshPollVotes() */ /** Current poll votes, refresh with Message.refreshPollVotes() */
this.pollVotes = data.pollVotes.map((pollVote) => { 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() { async refreshPollVotes() {
if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message'; if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message';
const pollVotes = await this.client.evaluate((parentMsgId) => { 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.id);
this.pollVotes = pollVotes.map((pollVote) => { this.pollVotes = pollVotes.map((pollVote) => {
return new PollVote(this.client, pollVote); return new PollVote(this.client, {...pollVote, pollCreationMessage: this});
}); });
return; return;
} }

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { Message } = require('.');
const Base = require('./Base'); const Base = require('./Base');
/** /**
@@ -20,11 +21,14 @@ class PollVote extends Base {
}); });
/** Sender of the Poll vote */ /** Sender of the Poll vote */
this.sender = data.sender; this.sender = data.sender._serialized;
/** Timestamp of the time it was sent in milliseconds */ /** Timestamp of the time it was sent in milliseconds */
this.senderTimestampMs = data.senderTimestampMs; 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); return super._patch(data);
} }
} }

View File

@@ -407,7 +407,7 @@ exports.LoadUtils = () => {
} }
if (msg.type == 'poll_creation') { 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; delete msg.pendingAckUpdate;
@@ -636,7 +636,7 @@ exports.LoadUtils = () => {
for (const option of selectedOptions) { for (const option of selectedOptions) {
if (a.name == option) localIdSet.add(a.localId); if (a.name == option) localIdSet.add(a.localId);
} }
}) });
await window.Store.SendVote.sendVote(msg, localIdSet); await window.Store.SendVote.sendVote(msg, localIdSet);
} };
}; };