forked from Prince-Mendiratta/BotsApp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Athena.js
171 lines (157 loc) · 6.78 KB
/
Athena.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const conn = require('./core/sessionString')
const fs = require('fs')
const { join } = require('path')
const config = require('./config')
const banner = require('./lib/banner');
const chalk = require('chalk');
const wa = require('./core/helper');
const { MessageType } = require('@whiskeysockets/baileys');
const Greetings = require('./database/greeting');
const sequelize = config.DATABASE;
const STRINGS = require("./lib/db");
const Blacklist = require('./database/blacklist');
const GENERAL = STRINGS.general;
// const gitPull = require('./core/gitpull');
const clearance = require('./core/clearance');
var client = conn.WhatsApp;
async function main() {
client.logger.level = 'error';
console.log(banner);
var commandHandler = new Map();
try {
var session = conn.restoreSession(config.STRING_SESSION)
client.loadAuthInfo(session)
} catch (err) {
if (err instanceof TypeError || err.message === "given authInfo is null" || err instanceof SyntaxError) {
console.log(
chalk.redBright.bold("Incorrect Session String. Please authenticate again using command -> "),
chalk.yellowBright.bold("npm start")
);
console.debug("[DEBUG] " + err);
fs.writeFileSync('./config.env', `STRING_SESSION=""`);
process.exit(0);
}
else {
console.log(
chalk.redBright.bold("SOMETHING WENT WRONG.\n"),
chalk.redBright.bold("[DEBUG] " + err)
);
process.exit(0)
}
}
client.on('connecting', async () => {
console.log(chalk.yellowBright("[INFO] Connecting to WhatsApp..."));
})
client.on('open', async () => {
console.log(chalk.yellowBright.bold("[INFO] Installing Plugins... Please wait."));
var moduleFiles = fs.readdirSync(join(__dirname, 'modules')).filter((file) => file.endsWith('.js'))
for (var file of moduleFiles) {
try {
const command = require(join(__dirname, 'modules', `${file}`));
console.log(
chalk.magentaBright("[INFO] Successfully imported module"),
chalk.cyanBright.bold(`${file}`)
)
commandHandler.set(command.name, command);
} catch (error) {
console.log(
chalk.blueBright.bold("[INFO] Could not import module"),
chalk.redBright.bold(`${file}`)
)
console.log(`[ERROR] `, error);
continue;
}
}
console.log(chalk.green.bold("[INFO] Plugins Installed Successfully. The bot is ready to use."));
console.log(chalk.yellowBright.bold("[INFO] Connecting to Database."));
try {
await sequelize.authenticate();
console.log(chalk.greenBright.bold('[INFO] Connection has been established successfully.'));
} catch (error) {
console.error('[ERROR] Unable to connect to the database:', error);
}
console.log(chalk.yellowBright.bold("[INFO] Syncing Database..."));
await sequelize.sync();
console.log(chalk.greenBright.bold("[INFO] All models were synchronized successfully."));
console.log(chalk.greenBright.bold("[INFO] Connected! Welcome to Athena"));
client.sendMessage(
client.user.jid,
GENERAL.SUCCESSFUL_CONNECTION.format({
worktype: config.WORK_TYPE,
}),
MessageType.text
);
})
await client.connect();
client.on('group-participants-update', async update => {
// console.log("-------------------" + "GROUP PARTICIPANT UPDATE" + "-------------------");
// console.log(update.participants);
// console.log(update.action);
// console.log(update.jid);
var groupId = update.jid;
try {
if (update.action === 'add') {
var enable = await Greetings.checkSettings(groupId, "welcome");
if (enable === false || enable === "OFF") {
return;
}
var Msg = await Greetings.getMessage(groupId, "welcome");
client.sendMessage(groupId, Msg.message, MessageType.text);
return;
}
else if (update.action === 'remove') {
var enable = await Greetings.checkSettings(groupId, "goodbye");
if (enable === false || enable === "OFF") {
return;
}
var Msg = await Greetings.getMessage(groupId, "goodbye");
client.sendMessage(groupId, Msg.message, MessageType.text);
return;
}
}
catch (err) {
// console.log("Greeting message are off");
}
});
client.on('chat-update', async chat => {
if (!chat.hasNewMessage) return
if (!chat.messages) return
// console.log("-------------------------------------------")
chat = chat.messages.all()[0];
var sender = chat.key.remoteJid;
const groupMetadata = sender.endsWith("@g.us") ? await client.groupMetadata(sender) : '';
var Athena = wa.resolve(chat, client, groupMetadata);
// console.log(Athena);
if (Athena.isCmd) {
let isBlacklist = await Blacklist.getBlacklistUser(Athena.sender, Athena.chatId);
const cleared = await clearance(Athena, client, isBlacklist);
if (!cleared) {
return;
}
console.log(chalk.redBright.bold(`[INFO] ${Athena.commandName} command executed.`));
const command = commandHandler.get(Athena.commandName);
var args = Athena.body.trim().split(/\s+/).slice(1);
// console.log("ARGS -> " + args);
// args.forEach(arg => console.log("arg -> " + arg + " type -> " + typeof(arg)));
// console.log("-------------------------------------------")
if (!command) {
client.sendMessage(Athena.chatId, "```Woops, invalid command! Use``` *.help* ```to display the command list.```", MessageType.text);
return;
} else if (command && Athena.commandName == "help") {
try {
command.handle(client, chat, Athena, args, commandHandler);
return;
} catch (err) {
console.log(chalk.red("[ERROR] ", err));
return;
}
}
try {
command.handle(client, chat, Athena, args).catch(err => console.log("[ERROR] " + err));
} catch (err) {
console.log(chalk.red("[ERROR] ", err));
}
}
})
}
main().catch(err => console.log('[ERROR] : %s', chalk.redBright.bold(err)));