Skip to content

Commit 11c8562

Browse files
committed
[FEAT] response to multiple events & callback to 'group_join' event
1 parent 5e7f1ff commit 11c8562

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ const alice = new Alice([
2828
new Wiki(),
2929
]);
3030

31-
alice.initialize();
31+
alice.init();

src/Alice.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
1-
const auth = require('./auth');
1+
const { Session } = require('./auth');
22
const { Parse } = require('./utils');
3-
const build = require('./build');
3+
const { Commands } = require('./build');
44

5-
const session = new auth.Session();
6-
const commands = new build.Commands();
5+
const session = new Session();
6+
const commands = new Commands();
77

88
class Alice {
99
constructor(commandsArray) {
10-
this.options = {
11-
trigger: 'message_create',
12-
};
10+
this.events = [
11+
{
12+
name: 'message_create',
13+
callback: Alice.onMessage,
14+
},
15+
{
16+
name: 'group_join',
17+
callback: Alice.onJoinedGroup,
18+
},
19+
];
1320

1421
commandsArray.forEach((cmd) => {
1522
commands.register(cmd);
1623
});
1724
}
1825

19-
static async onMessage(message) {
20-
const data = new Parse(message.body);
21-
22-
if (data.command) {
23-
await commands.call(data.command, data, message, session);
24-
}
25-
}
26-
27-
initialize() {
26+
init() {
2827
if (session.exists) {
2928
session.load();
3029
} else {
3130
session.create();
3231
}
3332

34-
session.on(this.options.trigger, Alice.onMessage);
33+
this.events.forEach((e) => {
34+
session.on(e.name, e.callback);
35+
});
36+
3537
session.start();
3638
}
39+
40+
// --- Only events callbacks below ---
41+
42+
static async onMessage(message) {
43+
const data = new Parse(message.body);
44+
45+
if (data.command) {
46+
await commands.call(data, message, session);
47+
}
48+
}
49+
50+
static async onJoinedGroup(notification) {
51+
const contacts = await notification.getRecipients();
52+
contacts.forEach(async (c) => {
53+
const chat = await c.getChat();
54+
await chat.sendMessage(
55+
`Bem vindo(a) a comunidade Coding, ${c.pushname}!`
56+
);
57+
});
58+
}
3759
}
3860

3961
module.exports = {

src/build/Commands.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class Commands {
4545

4646
/**
4747
* Calls (executes) a command.
48-
* @param {string} cmdName - The command's name to be called.
4948
* @param {object} data - The data extracted from the message that called the command.
5049
* @param {string} data.command - The command's name extracted from the message.
5150
* @param {string[]} data.args - The args extracted from the message.
@@ -56,13 +55,13 @@ class Commands {
5655
* @see https://docs.wwebjs.dev/Message.html
5756
* @see https://docs.wwebjs.dev/Client.html
5857
*/
59-
async call(cmdName, data, message, client) {
60-
if (!this.has(cmdName)) {
61-
throw new Error(`${cmdName} is not registered.`);
58+
async call(data, message, client) {
59+
if (!this.has(data.command)) {
60+
throw new Error(`${data.command} is not registered.`);
6261
}
6362

6463
try {
65-
await this.commands[cmdName].execute(data, message, client);
64+
await this.commands[data.command].execute(data, message, client);
6665
} catch (e) {
6766
message.reply(`❗ ${e.message}`);
6867
}

0 commit comments

Comments
 (0)