Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
feac79b
chore: bump versions
sergiooak Jan 25, 2024
a5c533a
feat: change use baileys over wwebjs
sergiooak Jan 25, 2024
a9ad2a1
feat: move temp folder
sergiooak Jan 25, 2024
434c6e5
feat: make baileys MVP
sergiooak Jan 25, 2024
5fd90b2
chore: move out old events
sergiooak Jan 25, 2024
3c92286
chore: move events to independent files
sergiooak Jan 27, 2024
9dcfc97
feat: load events on the fly
sergiooak Jan 27, 2024
f707240
fix: trace events instead of logger.info
sergiooak Jan 27, 2024
eeb046f
feat: parse message types
sergiooak Jan 27, 2024
49bd6a7
feat: create message meta middleware
sergiooak Jan 27, 2024
f64a64d
fix: update message object
sergiooak Jan 27, 2024
5b42458
chore: remove comments and logs
sergiooak Jan 28, 2024
19fdb29
fix: better extraction of message type
sergiooak Jan 28, 2024
812bba7
feat: add middleware to parsw message like wwebjs
sergiooak Jan 28, 2024
eede36f
chore: new dependencies
sergiooak Jan 28, 2024
01aa51a
feat: add startedAt property to processMessage function
sergiooak Jan 28, 2024
114f02a
fix: disable command function temporary
sergiooak Jan 28, 2024
786bd5c
feat: implement mvp functions
sergiooak Jan 28, 2024
f2c0f28
fix: disable broken commands
sergiooak Jan 28, 2024
aa9284a
fix: disable not supported yet validations
sergiooak Jan 28, 2024
ec2b6d3
feat: add messageMedia generator
sergiooak Jan 28, 2024
c20c1a5
fix: wait message on chats
sergiooak Jan 30, 2024
1449aec
fix: delete separate event
sergiooak Jan 30, 2024
80739a3
Fix linting errors and update database connection
sergiooak Jan 30, 2024
3d57933
mvp
sergiooak Jan 30, 2024
dcb5326
fix: forgotten commit
sergiooak Jan 30, 2024
fb65b56
fix: auto restart
sergiooak Jan 31, 2024
0974d6e
fix: better close
sergiooak Jan 31, 2024
7a27bbf
fix: less chars
sergiooak Jan 31, 2024
41758b8
fix: even less
sergiooak Jan 31, 2024
d017eb6
fix: new divider
sergiooak Jan 31, 2024
446a321
fix
sergiooak Jan 31, 2024
b9f040e
feat: warn user to restart after qr-code first read
sergiooak Feb 3, 2024
5c1227b
feat: show error to user with a debug group invite link
sergiooak Feb 3, 2024
6a334ad
feat: refactor and add 3 retries to each fetch
sergiooak Feb 3, 2024
0656601
fix: change detection if message has media or if the quoted message has
sergiooak Feb 3, 2024
ec75921
fix: change validator to not detect body '...' as function
sergiooak Feb 3, 2024
02847e6
chore: remove console.log statement in unhandledRejection event
sergiooak Feb 3, 2024
d8e3476
feat: add getDBUrl function
sergiooak Feb 3, 2024
b70a524
chore: remove console logs
sergiooak Feb 3, 2024
adb83e6
fix: remove double function declaration
sergiooak Feb 3, 2024
b48ac9f
feat: mvp of vip bot system
sergiooak Feb 3, 2024
072db0d
fix: only works on pv
sergiooak Feb 3, 2024
ae197a0
feat: add function to force update
sergiooak Feb 3, 2024
4acb3d1
feat: add function to set preferences
sergiooak Feb 3, 2024
60da359
fix: add support to work with local functions
sergiooak Feb 3, 2024
c3c88db
fix: change the default overwrite to work
sergiooak Feb 3, 2024
de65078
feat: add support for empty preferences
sergiooak Feb 3, 2024
5ad7e8a
fix: remove left over debug message
sergiooak Feb 3, 2024
5b34a44
fix: do not parse empty message
sergiooak Feb 5, 2024
b648f44
feat: add new queue and anti-spam system
sergiooak Feb 5, 2024
80413ec
fix: change logs to single string
sergiooak Feb 5, 2024
37de50e
chore: simplify logger
sergiooak Feb 5, 2024
05dcaf2
feat: improve whatsapp boot strategy
sergiooak Feb 5, 2024
1c612c8
feat: detect messages updates
sergiooak Feb 5, 2024
6152db9
chore: buff response speed
sergiooak Feb 5, 2024
f739e07
chore: slow down bot responses
sergiooak Feb 5, 2024
7c7993a
chore: remove peacock colors
sergiooak Feb 8, 2024
728cae1
fix: temp disable send seen
sergiooak Feb 8, 2024
449991f
fix: temp log every message to detect last message before ban
sergiooak Feb 8, 2024
ae55d78
fix: temp enable vip only to 7041 and 5852
sergiooak Feb 8, 2024
2e18988
fix: temp disable reaction
sergiooak Feb 8, 2024
f77c222
fix: temp active vip only to all bots
sergiooak Feb 9, 2024
2545ddb
feat: activate bot trough site
sergiooak Feb 12, 2024
099f7d4
feat: log error
sergiooak Feb 12, 2024
a19a477
fix: log queue
sergiooak Feb 12, 2024
1fd1a38
fix
sergiooak Feb 12, 2024
b36f1cc
feat: reactivate reactions
sergiooak Feb 13, 2024
b283feb
perf: stickers now only send wait reaction
sergiooak Feb 13, 2024
6ecbd95
fix: disable read more trick
sergiooak Feb 13, 2024
9c542dc
fix: disable log
sergiooak Feb 13, 2024
54ff592
perf: reactions emojis loaded via db
sergiooak Feb 13, 2024
8b95cf4
fix: comment out unused code
sergiooak Feb 13, 2024
3135ad2
fix: remove logs
sergiooak Feb 13, 2024
f1225a9
perf: improve ping
sergiooak Feb 13, 2024
7880964
chore: add types
sergiooak Feb 13, 2024
eabb8ec
fix: use global in the right way
sergiooak Feb 14, 2024
057e8d2
fix: disable zero
sergiooak Feb 14, 2024
7b16e0d
fix: temp do not send online
sergiooak Feb 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"rules": {
"no-useless-escape": [
"off"
],
"no-unmodified-loop-condition": [
"off"
],
"no-irregular-whitespace": [
"off"
]
}
}
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
node_modules

.wwebjs_auth
.wwebjs_cache
auth_info_baileys

.env
package-lock.json
errors.log

temp/*
!temp/.gitkeep
src/temp/*
!src/temp/.gitkeep
20 changes: 0 additions & 20 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,2 @@
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#14e8a2",
"activityBar.background": "#14e8a2",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#bb4ff0",
"activityBarBadge.foreground": "#15202b",
"commandCenter.border": "#15202b99",
"sash.hoverBorder": "#14e8a2",
"statusBar.background": "#10b981",
"statusBar.foreground": "#15202b",
"statusBarItem.hoverBackground": "#0c8a60",
"statusBarItem.remoteBackground": "#10b981",
"statusBarItem.remoteForeground": "#15202b",
"titleBar.activeBackground": "#10b981",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveBackground": "#10b98199",
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "#10b981"
}
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deadbyte-v3",
"version": "3.0.0",
"version": "3.5.0",
"description": "deadbyte 3rd generation",
"author": "sergiooak",
"main": "src/index.js",
Expand All @@ -15,21 +15,28 @@
},
"license": "unlicense",
"dependencies": {
"change-case": "^5.3.0",
"@whiskeysockets/baileys": "^6.6.0",
"change-case": "^5.4.2",
"cheerio": "^1.0.0-rc.12",
"citty": "^0.1.5",
"dayjs": "^1.11.10",
"dotenv": "^16.3.1",
"dotenv": "^16.4.1",
"file-type": "^19.0.0",
"filenamify": "^6.0.0",
"fluent-ffmpeg": "^2.1.2",
"form-data": "^4.0.0",
"link-preview-js": "^3.0.5",
"mime-types": "^2.1.35",
"node-cache": "^5.1.2",
"node-cron": "^3.0.3",
"openai": "^4.24.1",
"node-webpmux": "^3.2.0",
"openai": "^4.25.0",
"pino": "^8.17.2",
"pino-pretty": "^10.3.1",
"qrcode-terminal": "^0.12.0",
"qs": "^6.11.2",
"read-chunk": "^4.0.3",
"sharp": "^0.32.6",
"whatsapp-web.js": "^1.23.0",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
102 changes: 57 additions & 45 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from 'node-fetch'
import logger from './logger.js'
import { kebabCase } from 'change-case'
import logger from './logger.js'
import fetch from 'node-fetch'
import qs from 'qs'
//
// ===================================== Variables ======================================
Expand Down Expand Up @@ -48,7 +48,6 @@ doLogin()
//
/**
* Check if the API is online
*
* @returns {boolean} true if the API is online
*/
export function isOnline () {
Expand All @@ -57,7 +56,6 @@ export function isOnline () {

/**
* Get the token
*
* @returns {string} token
*/
export function getToken () {
Expand All @@ -66,7 +64,6 @@ export function getToken () {

/**
* Load the commands from the database
*
* @returns {object} commands
*/
export async function loadCommands () {
Expand Down Expand Up @@ -98,7 +95,6 @@ export async function loadCommands () {

/**
* Get the commands
*
* @returns {object} commands
*/
export function getCommands () {
Expand Down Expand Up @@ -129,35 +125,35 @@ export function getBot () {
* @param {import('whatsapp-web.js').Contact} contact
*/
export async function findOrCreateContact (contact) {
// 1 - Check if contact.id._serialized is on the cache
if (contactsCache[contact.id._serialized]) {
contactsCache[contact.id._serialized].lastSeen = new Date()
return contactsCache[contact.id._serialized]
const id = contact.id.replace('@s.whatsapp.net', '@c.us')

// 1 - Check if contact is on the cache
if (contactsCache[id]) {
contactsCache[id].lastSeen = new Date()
return contactsCache[id]
}

// 2 - If not, fetch from the database
const response = await fetch(`${dbUrl}/contacts/${contact.id._serialized}`, {
const response = await fetch(`${dbUrl}/contacts/${id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
data: {
name: contact.name,
number: contact.id.user,
number: id.split('@')[0],
pushname: contact.pushname,
isMyContact: contact.isMyContact,
wid: contact.id._serialized
wid: id
}
})
})
const data = await response.json()

// 3 - Save on the cache
contactsCache[contact.id._serialized] = data
contactsCache[contact.id._serialized].lastSeen = new Date()
return contactsCache[contact.id._serialized]
contactsCache[id] = data
contactsCache[id].lastSeen = new Date()
return contactsCache[id]
}

// mini cache system for contacts, every minute filter out the contacts that haven't been seen in the last 5 minutes
Expand All @@ -171,38 +167,51 @@ setInterval(() => {
})
}, 60_000)

/**
* Force a contact update on the database
* @param {import('whatsapp-web.js').Contact} contact
* @returns {Promise<object>} - The updated contact
*/
export async function forceContactUpdate (contact) {
const id = contact.id.replace('@s.whatsapp.net', '@c.us')
delete contactsCache[id]
return await findOrCreateContact(contact)
}

/**
* Find or create a chat on the database
*
* @param {import('whatsapp-web.js').Chat} chat
*/
export async function findOrCreateChat (chat) {
export async function findOrCreateChat (msg) {
// 1 - Check if chat.id._serialized is on the cache
if (chatsCache[chat.id._serialized]) {
chatsCache[chat.id._serialized].lastSeen = new Date()
return chatsCache[chat.id._serialized]
const id = msg.isGroup
? msg.aux.group.id
: msg.contact.id.replace('@s.whatsapp.net', '@c.us')

if (chatsCache[id]) {
chatsCache[id].lastSeen = new Date()
return chatsCache[id]
}

// 2 - If not, fetch from the database
const response = await fetch(`${dbUrl}/chats/${chat.id._serialized}`, {
const response = await fetch(`${dbUrl}/chats/${id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
data: {
name: chat.name,
isGroup: chat.isGroup,
wid: chat.id._serialized
name: msg.isGroup ? msg.aux.group.subject : msg.pushname,
isGroup: msg.isGroup,
wid: id
}
})
})
const data = await response.json()

// 3 - Save on the cache
chatsCache[chat.id._serialized] = data
chatsCache[chat.id._serialized].lastSeen = new Date()
chatsCache[id] = data
chatsCache[id].lastSeen = new Date()

return data
}
Expand Down Expand Up @@ -274,45 +283,49 @@ export async function saveActionToDB (moduleName, functionName, msg) {
const commandGroupID = commandGroup?.id
const command = commandGroup.commands.find((command) => command.slug === kebabCase(functionName))
const commandID = command?.id
const contact = await findOrCreateContact(msg.aux.sender)
const contact = await findOrCreateContact(msg.contact)
const contactID = contact.id
const chat = await findOrCreateChat(msg.aux.chat)
const chat = await findOrCreateChat(msg)
const chatID = chat.id

const action = await createAction(commandGroupID, commandID, chatID, contactID)

const action = createAction(commandGroupID, commandID, chatID, contactID)
try {
const actionID = action.id
return { action, actionID, commandGroup, commandGroupID, command, commandID, contact, contactID, chat, chatID }
return { action, commandGroup, commandGroupID, command, commandID, contact, contactID, chat, chatID }
} catch (error) {
logger.error('Error saving action to database', error)
logger.error('Action:', action)
}
}

export async function findCurrentBot (client) {
export async function findCurrentBot (socket) {
if (!socket.user) { // TODO: auto-restart
logger.warn('Bot never connected before')
logger.warn('Remember to restart after reading the QR code')
return
}
const id = socket.user.id.split(':')[0] + '@c.us'
// 1 - Check if bot already exists on db

const findQuery = qs.stringify(
{
filters: {
wid: {
$eq: client.info.wid._serialized
$eq: id
}
}
},
{
encodeValuesOnly: true // prettify URL
}
)
while (!token) { // wait token to be populated
await new Promise(resolve => setTimeout(resolve, 100))
}
const find = await fetch(`${dbUrl}/bots?${findQuery}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})

const { data: findData } = await find.json()

if (findData.length) {
Expand All @@ -321,7 +334,6 @@ export async function findCurrentBot (client) {
}

// 2 - If not, create it

const create = await fetch(`${dbUrl}/bots`, {
method: 'POST',
headers: {
Expand All @@ -330,12 +342,12 @@ export async function findCurrentBot (client) {
},
body: JSON.stringify({
data: {
wid: client.info.wid._serialized,
pushname: client.info.pushname,
platform: client.info.platform
wid: id,
pushname: socket.user.name
}
})
})

const { data: createData } = await create.json()
bot = createData.id
}
Loading