mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
Add group chat functions, slight refactoring
This commit is contained in:
52
example.js
52
example.js
@@ -1,4 +1,4 @@
|
||||
const { Client } = require('./src')
|
||||
const { Client } = require('./index')
|
||||
|
||||
const client = new Client({puppeteer: {headless: false}});
|
||||
|
||||
@@ -16,16 +16,60 @@ client.on('ready', () => {
|
||||
console.log('READY');
|
||||
});
|
||||
|
||||
client.on('message', (msg) => {
|
||||
client.on('message', async msg => {
|
||||
console.log('MESSAGE RECEIVED', msg);
|
||||
|
||||
if (msg.body == 'ping reply') {
|
||||
if (msg.body == '!ping reply') {
|
||||
// Send a new message as a reply to the current one
|
||||
msg.reply('pong');
|
||||
|
||||
} else if (msg.body == 'ping') {
|
||||
} else if (msg.body == '!ping') {
|
||||
// Send a new message to the same chat
|
||||
client.sendMessage(msg.from, 'pong');
|
||||
|
||||
} else if (msg.body.startsWith('!subject ')) {
|
||||
// Change the group subject
|
||||
let chat = await msg.getChat();
|
||||
if(chat.isGroup) {
|
||||
let newSubject = msg.body.slice(9);
|
||||
chat.setSubject(newSubject);
|
||||
} else {
|
||||
msg.reply('This command can only be used in a group!');
|
||||
}
|
||||
} else if (msg.body.startsWith('!echo ')) {
|
||||
// Replies with the same message
|
||||
msg.reply(msg.body.slice(6));
|
||||
} else if (msg.body.startsWith('!desc ')) {
|
||||
// Change the group description
|
||||
let chat = await msg.getChat();
|
||||
if(chat.isGroup) {
|
||||
let newDescription = msg.body.slice(6);
|
||||
chat.setDescription(newDescription);
|
||||
} else {
|
||||
msg.reply('This command can only be used in a group!');
|
||||
}
|
||||
} else if (msg.body == '!leave') {
|
||||
// Leave the group
|
||||
let chat = await msg.getChat();
|
||||
if(chat.isGroup) {
|
||||
chat.leave();
|
||||
} else {
|
||||
msg.reply('This command can only be used in a group!');
|
||||
}
|
||||
} else if(msg.body == '!groupinfo') {
|
||||
let chat = await msg.getChat();
|
||||
if(chat.isGroup) {
|
||||
msg.reply(`
|
||||
*Group Details*
|
||||
Name: ${chat.name}
|
||||
Description: ${chat.description}
|
||||
Created At: ${chat.createdAt.toString()}
|
||||
Created By: ${chat.owner.user}
|
||||
Participant count: ${chat.participants.length}
|
||||
`);
|
||||
} else {
|
||||
msg.reply('This command can only be used in a group!');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user