@@ -2,15 +2,10 @@ import { Message } from "../types/message";
22import { getUserbyLid , isAdmin } from "../components/services/user" ;
33import redis from "../components/redis" ;
44import { client } from "../components/client" ;
5- import { MessageMedia } from "whatsapp-web.js" ;
65import parsePhoneNumber from "libphonenumber-js" ;
7- import moment from "moment-timezone" ;
8- import { getTimezonesForCountry , Timezone } from "countries-and-timezones" ;
9- import axios from "../components/axios" ;
10- import fs from "fs" ;
11- import path from "path" ;
12- import log from "../components/utils/log" ;
136import { RateEntry } from "../components/utils/rateLimiter" ;
7+ import { getCurrentTimeByCountryCode } from "../components/utils/time" ;
8+ import downloadProfilePicture from "../components/utils/profile/download" ;
149
1510export const info = {
1611 command : "stalk" ,
@@ -21,30 +16,6 @@ export const info = {
2116 cooldown : 5000 ,
2217} ;
2318
24- const fileExists = async ( filePath : string ) => {
25- try {
26- await fs . promises . access ( filePath , fs . constants . F_OK ) ;
27- return true ;
28- } catch {
29- return false ;
30- }
31- } ;
32-
33- function getCurrentTimeByCountryCode ( countryCode : string ) {
34- const timezones : Timezone [ ] | undefined = getTimezonesForCountry (
35- countryCode . toUpperCase ( ) ,
36- ) ;
37-
38- if ( ! timezones || timezones . length === 0 ) {
39- throw new Error ( "Invalid country code or no timezones found." ) ;
40- }
41-
42- const timezone = timezones [ 0 ] ;
43- const localTime = moment ( ) . tz ( timezone . name ) . format ( "YYYY-MM-DD HH:mm:ss" ) ;
44-
45- return { countryCode, timezone, localTime } ;
46- }
47-
4819export default async function ( msg : Message ) : Promise < void > {
4920 const chat = await msg . getChat ( ) ;
5021 if ( ! chat . isGroup ) {
@@ -57,12 +28,11 @@ export default async function (msg: Message): Promise<void> {
5728 return ;
5829 }
5930
60- const botId = ( await client ( ) ) . info . wid . _serialized ;
6131 const jid = msg . mentionedIds [ 0 ] ;
6232 const lid = jid . split ( "@" ) [ 0 ] ;
6333 const isUserAdmin = await isAdmin ( lid ) ;
6434
65- if ( ! msg . fromMe && ( lid === botId . split ( "@" ) [ 0 ] || isUserAdmin ) ) {
35+ if ( ! msg . fromMe && isUserAdmin ) {
6636 await msg . reply ( "You cannot do that, please mention someone else." ) ;
6737 return ;
6838 }
@@ -72,36 +42,7 @@ export default async function (msg: Message): Promise<void> {
7242 getUserbyLid ( lid ) ,
7343 redis . get ( `block:${ lid } ` ) ,
7444 redis . get ( `rate:${ lid } ` ) ,
75- ( async ( ) => {
76- try {
77- const tempDir = "./.temp" ;
78- await fs . promises . mkdir ( tempDir , { recursive : true } ) ;
79- const savePath = path . join ( tempDir , `profile_${ lid } .png` ) ;
80-
81- if ( await fileExists ( savePath ) ) {
82- return MessageMedia . fromFilePath ( savePath ) ;
83- }
84-
85- const avatarUrl = await ( await client ( ) ) . getProfilePicUrl ( jid ) ;
86-
87- if ( ! avatarUrl ) return undefined ;
88-
89- const res = await axios . get ( avatarUrl , {
90- responseType : "arraybuffer" ,
91- } ) ;
92- const buffer = Buffer . from ( res . data , "binary" ) ;
93-
94- await fs . promises . writeFile ( savePath , buffer ) ;
95- return new MessageMedia (
96- "image/jpeg" ,
97- buffer . toString ( "base64" ) ,
98- "avatar.jpg" ,
99- ) ;
100- } catch ( error ) {
101- log . error ( "Me" , "Unable to fetch user profile pic:" , error ) ;
102- return undefined ;
103- }
104- } ) ( ) ,
45+ downloadProfilePicture ( jid ) ,
10546 ] ) ;
10647
10748 if ( ! user ) {
@@ -111,10 +52,16 @@ export default async function (msg: Message): Promise<void> {
11152 return ;
11253 }
11354
55+ const self = ( await client ( ) ) . info . wid . _serialized ;
56+
57+ if ( self . split ( "@" ) [ 0 ] === user . number ) {
58+ await msg . reply ( "You cannot do that, please mention someone else." ) ;
59+ return ;
60+ }
61+
11462 const phoneNumber = parsePhoneNumber ( `+${ user . number } ` ) ;
11563 const countryCode = phoneNumber ?. country || "" ;
11664 const time = getCurrentTimeByCountryCode ( countryCode ) ;
117- const self = ( await client ( ) ) . info . wid . _serialized ;
11865 const ratelimit : RateEntry = isBlockedTemporarily
11966 ? JSON . parse ( isBlockedTemporarily )
12067 : { timestamps : [ ] , penaltyCount : 0 , penaltyUntil : 0 } ;
0 commit comments