mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
PART 2: ESLINT, EXAMPLES & MORE
This commit is contained in:
25
example.js
25
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 );
|
||||
});
|
||||
|
||||
6
index.d.ts
vendored
6
index.d.ts
vendored
@@ -1386,6 +1386,12 @@ declare namespace WAWebJS {
|
||||
senderId: string
|
||||
ack?: number
|
||||
}
|
||||
|
||||
export class PollVote {
|
||||
selectedOptions: string[]
|
||||
sender: string
|
||||
senderTimestampMs: number
|
||||
}
|
||||
}
|
||||
|
||||
export = WAWebJS
|
||||
|
||||
5
index.js
5
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
|
||||
};
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user