@@ -138,6 +138,7 @@ import Long from 'long';
138138import mimeTypes from 'mime-types' ;
139139import NodeCache from 'node-cache' ;
140140import cron from 'node-cron' ;
141+ import dayjs from 'dayjs' ;
141142import { release } from 'os' ;
142143import { join } from 'path' ;
143144import P from 'pino' ;
@@ -173,6 +174,30 @@ function normalizeJid(jid: string): string {
173174 return jid ;
174175}
175176
177+ // Function to clear corrupted session data
178+ async function clearCorruptedSessionData ( instanceId : string , baileysCache : CacheService ) {
179+ try {
180+ // Clear all baileys cache for this instance
181+ await baileysCache . deleteAll ( instanceId ) ;
182+
183+ // Clear session-related cache patterns
184+ const patterns = [
185+ `${ instanceId } _*` ,
186+ `*${ instanceId } *` ,
187+ `*session*${ instanceId } *` ,
188+ `*prekey*${ instanceId } *`
189+ ] ;
190+
191+ for ( const pattern of patterns ) {
192+ await baileysCache . deleteAll ( pattern ) ;
193+ }
194+
195+ console . log ( `Cleared corrupted session data for instance: ${ instanceId } ` ) ;
196+ } catch ( error ) {
197+ console . error ( 'Error clearing session data:' , error ) ;
198+ }
199+ }
200+
176201// Adicione a função getVideoDuration no início do arquivo
177202async function getVideoDuration ( input : Buffer | string | Readable ) : Promise < number > {
178203 const MediaInfoFactory = ( await import ( 'mediainfo.js' ) ) . default ;
@@ -675,6 +700,9 @@ export class BaileysStartupService extends ChannelStartupService {
675700
676701 this . endSession = false ;
677702
703+ // Clear any corrupted session data before connecting
704+ await clearCorruptedSessionData ( this . instanceId , this . baileysCache ) ;
705+
678706 this . client = makeWASocket ( socketConfig ) ;
679707
680708 if ( this . localSettings . wavoipToken && this . localSettings . wavoipToken . length > 0 ) {
@@ -3188,12 +3216,35 @@ export class BaileysStartupService extends ChannelStartupService {
31883216 const cachedNumbers = await getOnWhatsappCache ( numbersToVerify ) ;
31893217 console . log ( 'cachedNumbers' , cachedNumbers ) ;
31903218
3191- const filteredNumbers = numbersToVerify . filter (
3192- ( jid ) => ! cachedNumbers . some ( ( cached ) => cached . jidOptions . includes ( jid ) ) ,
3193- ) ;
3219+ // Filter numbers that are not cached OR should be re-verified
3220+ const filteredNumbers = numbersToVerify . filter ( ( jid ) => {
3221+ const cached = cachedNumbers . find ( ( cached ) => cached . jidOptions . includes ( jid ) ) ;
3222+ // If not cached, we should verify
3223+ if ( ! cached ) return true ;
3224+
3225+ // For Brazilian numbers, force verification if both formats exist in cache
3226+ // to ensure we're using the correct format
3227+ const isBrazilian = jid . startsWith ( '55' ) && jid . includes ( '@s.whatsapp.net' ) ;
3228+ if ( isBrazilian ) {
3229+ const numberPart = jid . replace ( '@s.whatsapp.net' , '' ) ;
3230+ const hasDigit9 = numberPart . length === 13 && numberPart . slice ( 4 , 5 ) === '9' ;
3231+ const altFormat = hasDigit9
3232+ ? numberPart . slice ( 0 , 4 ) + numberPart . slice ( 5 )
3233+ : numberPart . slice ( 0 , 4 ) + '9' + numberPart . slice ( 4 ) ;
3234+ const altJid = altFormat + '@s.whatsapp.net' ;
3235+
3236+ // If both formats exist in cache, prefer the one with 9
3237+ const altCached = cachedNumbers . find ( ( c ) => c . jidOptions . includes ( altJid ) ) ;
3238+ if ( cached && altCached && ! hasDigit9 ) {
3239+ return true ; // Force verification to get the correct format
3240+ }
3241+ }
3242+
3243+ return false ; // Use cached result
3244+ } ) ;
31943245 console . log ( 'filteredNumbers' , filteredNumbers ) ;
31953246
3196- const verify = await this . client . onWhatsApp ( ...filteredNumbers ) ;
3247+ const verify = filteredNumbers . length > 0 ? await this . client . onWhatsApp ( ...filteredNumbers ) : [ ] ;
31973248 console . log ( 'verify' , verify ) ;
31983249 normalVerifiedUsers = await Promise . all (
31993250 normalUsers . map ( async ( user ) => {
0 commit comments