-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
210 lines (185 loc) · 9.03 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fs = require("fs")
const { default: makeWASocket, BufferJSON, initInMemoryKeyStore, DisconnectReason, MessageType,
MessageOptions, MimeType } = require("@adiwajshing/baileys-md")
const config = require('./config/config.json')
const { banner, getBuffer, getRandom } = require('./lib/functions')
const prefix = config.prefix
async function start() {
let milla = undefined;
const connect = () => {
let status = undefined;
try {
const value = JSON.parse(fs.readFileSync('./config/session.json', { encoding: 'utf-8' }), BufferJSON.reviver);
status = {
creds: value.creds,
keys: initInMemoryKeyStore(value.keys)
};
} catch { }
return status;
};
const saveConnection = (status) => {
status = status || (milla === null || milla === void 0 ? void 0 : milla.authState);
fs.writeFileSync('./config/session.json',
JSON.stringify(status, BufferJSON.replacer, 2));
};
const starts = () => {
const client = makeWASocket({
auth: connect(),
printQRInTerminal: true,
})
console.log(banner.string)
return client;
}
milla = starts();
milla.ev.on('messages.upsert', async ({ messages }) => {
console.log(JSON.stringify(messages, undefined, 2))
try {
// Reading Messages //
msg = messages[0]
if (!msg.message) return
if (msg.key.fromMe) return
const type = Object.keys(msg.message)[0]
// body = msg.message?.conversation.startsWith(prefix) || msg.message?.extendedTextMessage?.text.startsWith(prefix) || msg.message?.imageMessage?.caption.startsWith(prefix) || msg.message?.videoMessage?.caption.startsWith(prefix) || msg.message?.viewOnceMessage?.message?.imageMessage?.caption.startsWith(prefix) || msg.message?.viewOnceMessage?.message?.videoMessage?.caption.startsWith(prefix) || msg.message?.templateMessage?.hydratedTemplate?.hydratedTitleText || msg.message?.buttonsResponseMessage?.selectedDisplayText || msg.message?.listResponseMessage?.title || " ";
body = (type === 'conversation' && msg.message.conversation.startsWith(prefix)) ? msg.message.conversation : (type == 'imageMessage') && msg.message[type].caption.startsWith(prefix) ? msg.message[type].caption : (type == 'videoMessage') && msg.message[type].caption.startsWith(prefix) ? msg.message[type].caption : (type == 'extendedTextMessage') && msg.message[type].text.startsWith(prefix) ? msg.message[type].text : (type == 'listResponseMessage') && msg.message[type].singleSelectReply.selectedRowId ? msg.message[type].singleSelectReply.selectedRowId : (type == 'buttonsResponseMessage') && msg.message[type].selectedButtonId ? msg.message[type].selectedButtonId : ""
button = (type == 'buttonsResponseMessage') ? msg.message.buttonsResponseMessage.selectedDisplayText : ''
chats = (type === 'conversation') ? msg.message.conversation : (type === 'extendedTextMessage') ? msg.message.extendedTextMessage.text : ''
selectedButton = (type == 'buttonsResponseMessage') ? msg.message.buttonsResponseMessage.selectedButtonId : ''
responseButton = (type == 'listResponseMessage') ? msg.message.listResponseMessage.title : ''
const from = msg.isGroup ? msg.participant : msg.key.fromMe ? milla.user.jid : msg.key.remoteJid
const isGroup = from.endsWith('@g.us')
const sender = isGroup ? msg.participant : msg.key.remoteJid
const command = body.slice(1).trim().split(/ +/).shift().toLowerCase()
// Reading Messages //
// Important //
const ownerNumber = [`${config.dev}`]
const isOwner = ownerNumber.includes(sender)
const pushname = msg.message.pushname
const args = body.trim().split(/ +/).slice(1)
const q = args.join(" ")
// Important //
const isUrl = (url) => {
return url.match(new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/, 'gi'))
}
const reply = (string) => {
milla.sendMessage(from, { text: string })
}
const sendPhoto = (imageDir, caption) => {
milla.sendMessage(from, {
image: fs.readFileSync(imageDir),
caption: caption
})
}
const textButtons = (firstId, firstText, secondId, secondText, content) => {
var buttonsContent = [
{ buttonId: firstId, buttonText: { displayText: firstText }, type: 1 },
{ buttonId: secondId, buttonText: { displayText: secondText }, type: 1 }
]
var msgContent = {
contentText: content,
footerText: 'Milla - Multi Device',
buttons: buttonsContent,
headerType: 1
}
return msgContent
}
switch (command) {
case 'menu':
if (!isOwner) return reply('Opa, estou em fases de testes ainda!')
await reply(`${pushname}`)
break
case 'imagem':
await sendPhoto('./assets/logo.png', 'Oi! Isso é um teste de envio de imagem.')
break
case 'download':
if(args == 'story') {
let username = args[1]
hx.igstory(username)
.then(async (result) => {
for (let i of result.medias) {
if (i.url.includes('mp4')) {
const jays = await getBuffer(i.url)
await milla.sendMessage(from, {
video: jays,
quoted: msg
})
reply('Prontinho.')
} else {
const jays = await getBuffer(i.url)
await milla.sendMessage(from, {
video: jays,
quoted: msg
})
reply('Prontinho.')
}
}
});
}
break
case 'buttons':
await milla.sendMessage(from, {
buttons: {
contentText: "Teste",
footerText: "MillaBot",
buttons: [
{
buttonId: "teste1",
buttonText: {
displayText: "MP3 (audio)"
},
type: 1
},
{
buttonId: "teste2",
buttonText: {
displayText: "MP4 (video)"
},
type: 1
}
],
headerType: 1
}
})
break
case 'list':
const lista = {
buttonText: 'button.buttonText',
description: 'button.description',
sections: [{//início
"title": "Menus",
"rows": [{
"title": "Sobre a Milla",
"rowId": "#about"
},
{
"title": "Novidades da Versão (25/09/21)",
"rowId": "#logs"
}]
}],
listType: 1
}
await milla.sendMessage(from, { list: lista })
break
}
} catch (e) {
console.log(e)
}
})
milla.ev.on('connection.update', (update) => {
var _a, _b;
const { connection, lastDisconnect } = update;
if (connection === 'close') {
// reconnect if not logged out
if (((_b = (_a = lastDisconnect.error) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode) !== DisconnectReason.loggedOut) {
milla = starts();
}
else {
console.log('connection closed');
}
}
console.log('connection update', update.connection);
});
// listen for when the auth state is updated
// it is imperative you save this data, it affects the signing keys you need to have conversations
milla.ev.on('auth-state.update', saveConnection)
}
start()