-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
105 lines (92 loc) · 3.13 KB
/
index.ts
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
import { Boom } from '@hapi/boom'
import makeWASocket, {
makeCacheableSignalKeyStore,
fetchLatestBaileysVersion,
useMultiFileAuthState,
DisconnectReason,
proto,
} from '@whiskeysockets/baileys'
import MAIN_LOGGER from './src/utils/logger'
import { messageHandler } from './src/handler'
import NodeCache from 'node-cache'
import { textSync } from 'figlet'
import dotenv from 'dotenv'
import chalk from 'chalk'
import { PlaywrightBrowser, getMessage } from './src/lib/_index'
import { executeSavedScriptInNote } from './src/cmd/owner'
export const browser = new PlaywrightBrowser()
let lastDisconnectReason = ''
dotenv.config()
const logger = MAIN_LOGGER.child({})
logger.level = 'error'
const msgRetryCounterCache = new NodeCache()
const startSock = async () => {
const { state, saveCreds } = await useMultiFileAuthState(
'./env/baileys_auth_info'
)
const { version, isLatest } = await fetchLatestBaileysVersion()
console.log(
chalk.red(
textSync('SERO SELFBOT', {
horizontalLayout: 'fitted',
font: 'Letters',
})
)
)
console.log(`Using WA v${version.join('.')}, isLatest: ${isLatest}`)
const waSocket = makeWASocket({
version,
logger,
printQRInTerminal: true,
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
msgRetryCounterCache,
generateHighQualityLinkPreview: true,
markOnlineOnConnect: false,
getMessage: async (key) => {
const msg = await getMessage(key.id!)
return (
msg?.message ||
proto.Message.fromObject({
message: { text: '_Failed to fetch message_' },
})
)
},
})
waSocket.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close') {
if (
(lastDisconnect?.error as Boom)?.output?.statusCode !==
DisconnectReason.loggedOut
) {
lastDisconnectReason =
lastDisconnect?.error?.message || lastDisconnectReason
startSock()
} else {
console.log('Connection closed. You are logged out.')
}
}
console.log('Connection update:', update)
if (connection === 'open') {
console.log(
chalk.yellow('!---------------BOT IS ONLINE---------------!')
)
if (lastDisconnectReason) {
waSocket.sendMessage(process.env.OWNER_NUMBER as string, {
text: `🔰Reconnected! reason: ${lastDisconnectReason}`,
})
} else {
waSocket.sendMessage(process.env.OWNER_NUMBER as string, {
text: '🔰Bot is online!',
})
}
executeSavedScriptInNote(waSocket)
}
})
waSocket.ev.on('creds.update', saveCreds)
waSocket.ev.on('messages.upsert', messageHandler.bind(null, waSocket))
}
startSock()