Skip to content

Commit 87f4df0

Browse files
committed
refractor: minor adjustments and corrections
1 parent 8aefb84 commit 87f4df0

17 files changed

Lines changed: 125 additions & 189 deletions

File tree

src/commands/block.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ export default async function (msg: Message): Promise<void> {
2525
const jid = msg.mentionedIds[0];
2626
const lid = jid.split("@")[0];
2727

28-
// todo: _serialized will return an pid instead of lid
29-
// changce to mention contracts or continue?
30-
const botId = (await client()).info.wid._serialized;
28+
const self = (await client()).info.wid._serialized;
3129
const isUserAdmin = await isAdmin(lid);
3230

33-
if (!msg.fromMe && (isUserAdmin || lid === botId.split("@")[0])) {
34-
await msg.reply("Unable to block the user");
31+
if (!msg.fromMe && isUserAdmin) {
32+
await msg.reply("Unable to block the user.");
33+
return;
34+
}
35+
36+
const mentions = await msg.getMentions();
37+
const user = mentions[0];
38+
if (self.split("@")[0] === user.id._serialized.split("@")[0]) {
39+
await msg.reply("Unable to block the user.");
3540
return;
3641
}
3742

src/commands/delete.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message } from "../types/message"
1+
import { Message } from "../types/message";
22
import { client } from "../components/client";
33

44
export const info = {
@@ -11,13 +11,12 @@ export const info = {
1111
};
1212

1313
export default async function (msg: Message): Promise<void> {
14-
if (!msg.hasQuotedMsg) {
15-
await msg.reply("Please reply the message you want to delete.");
16-
return;
17-
}
14+
if (!msg.hasQuotedMsg) return;
15+
1816
const quoted = await msg.getQuotedMessage();
1917

2018
if (quoted.fromMe) {
19+
if (!msg.fromMe && /[aeiou]/i.test(quoted.body)) return;
2120
await quoted.delete(true, true);
2221
}
2322
}

src/commands/demote.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Message } from "../types/message";
22
import log from "../components/utils/log";
33
import { isAdmin, setAdmin } from "../components/services/user";
4+
import client from "../components/client";
45

56
export const info = {
67
command: "demote",

src/commands/fbdl.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import log from "../components/utils/log";
66
import axios from "../components/axios";
77
import fs from "fs";
88
import he from "he";
9+
import { fileExists } from "../components/utils/file";
910

1011
export const info = {
1112
command: "fbdl",
@@ -16,15 +17,6 @@ export const info = {
1617
cooldown: 5000,
1718
};
1819

19-
const fileExists = async (filePath: string) => {
20-
try {
21-
await fs.promises.access(filePath, fs.constants.F_OK);
22-
return true;
23-
} catch {
24-
return false;
25-
}
26-
};
27-
2820
function md5FromUrl(url: string) {
2921
return crypto.createHash("md5").update(url).digest("hex");
3022
}

src/commands/me.ts

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ import { Message } from "../types/message";
22
import { getUserbyLid, isAdmin } from "../components/services/user";
33
import redis from "../components/redis";
44
import { client } from "../components/client";
5-
import { MessageMedia } from "whatsapp-web.js";
65
import 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 timestamp from "../components/utils/timestamp";
11-
import fs from "fs";
12-
import path from "path";
13-
import log from "../components/utils/log";
146
import { RateEntry } from "../components/utils/rateLimiter";
7+
import { getCurrentTimeByCountryCode } from "../components/utils/time";
8+
import downloadProfilePicture from "../components/utils/profile/download";
159

1610
export const info = {
1711
command: "me",
@@ -22,30 +16,6 @@ export const info = {
2216
cooldown: 5000,
2317
};
2418

25-
const fileExists = async (filePath: string) => {
26-
try {
27-
await fs.promises.access(filePath, fs.constants.F_OK);
28-
return true;
29-
} catch {
30-
return false;
31-
}
32-
};
33-
34-
function getCurrentTimeByCountryCode(countryCode: string) {
35-
const timezones: Timezone[] | undefined = getTimezonesForCountry(
36-
countryCode.toUpperCase(),
37-
);
38-
39-
if (!timezones || timezones.length === 0) {
40-
throw new Error("Invalid country code or no timezones found.");
41-
}
42-
43-
const timezone = timezones[0];
44-
const localTime = moment().tz(timezone.name).format("YYYY-MM-DD HH:mm:ss");
45-
46-
return { countryCode, timezone, localTime };
47-
}
48-
4919
export default async function (msg: Message): Promise<void> {
5020
const jid = msg.author ?? msg.from;
5121
const lid = jid.split("@")[0];
@@ -61,36 +31,7 @@ export default async function (msg: Message): Promise<void> {
6131
redis.get(`block:${lid}`),
6232
redis.get(`rate:${lid}`),
6333
isAdmin(lid),
64-
(async () => {
65-
try {
66-
const tempDir = "./.temp";
67-
await fs.promises.mkdir(tempDir, { recursive: true });
68-
const savePath = path.join(tempDir, `profile_${lid}.png`);
69-
70-
if (await fileExists(savePath)) {
71-
return MessageMedia.fromFilePath(savePath);
72-
}
73-
74-
const avatarUrl = await (await client()).getProfilePicUrl(jid);
75-
76-
if (!avatarUrl) return undefined;
77-
78-
const res = await axios.get(avatarUrl, {
79-
responseType: "arraybuffer",
80-
});
81-
const buffer = Buffer.from(res.data, "binary");
82-
83-
await fs.promises.writeFile(savePath, buffer);
84-
return new MessageMedia(
85-
"image/jpeg",
86-
buffer.toString("base64"),
87-
"avatar.jpg",
88-
);
89-
} catch (error) {
90-
log.error("Me", "Unable to fetch user profile pic:", error);
91-
return undefined;
92-
}
93-
})(),
34+
downloadProfilePicture(jid),
9435
]);
9536

9637
if (!user) {

src/commands/play.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Innertube, UniversalCache, Utils } from "youtubei.js";
77
import util from "util";
88
import log from "../components/utils/log";
99
import { DownloadOptions } from "youtubei.js/dist/src/types";
10+
import { fileExists } from "../components/utils/file";
1011

1112
const execPromise = util.promisify(exec);
1213

@@ -19,15 +20,6 @@ export const info = {
1920
cooldown: 5000,
2021
};
2122

22-
const fileExists = async (filePath: string) => {
23-
try {
24-
await fs.promises.access(filePath, fs.constants.F_OK);
25-
return true;
26-
} catch {
27-
return false;
28-
}
29-
};
30-
3123
async function safeDownload(
3224
yt: Innertube,
3325
id: string,

src/commands/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const info = {
66
description: "Send all settings value",
77
usage: "settings",
88
example: "settings",
9-
role: "super-admin",
9+
role: "user",
1010
cooldown: 5000,
1111
};
1212

src/commands/stalk.ts

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import { Message } from "../types/message";
22
import { getUserbyLid, isAdmin } from "../components/services/user";
33
import redis from "../components/redis";
44
import { client } from "../components/client";
5-
import { MessageMedia } from "whatsapp-web.js";
65
import 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";
136
import { RateEntry } from "../components/utils/rateLimiter";
7+
import { getCurrentTimeByCountryCode } from "../components/utils/time";
8+
import downloadProfilePicture from "../components/utils/profile/download";
149

1510
export 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-
4819
export 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 };

src/commands/unload.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Message } from "../types/message"
1+
import { Message } from "../types/message";
22
import { commands, commandDirs } from "../components/utils/cmd/loader";
33
import Loader from "../components/utils/cmd/loader";
44
import path from "path";
@@ -13,7 +13,10 @@ export const info = {
1313
};
1414

1515
export default async function (msg: Message): Promise<void> {
16-
const query = msg.body.replace(/^unload\b\s*/i, "").trim().toLowerCase();
16+
const query = msg.body
17+
.replace(/^unload\b\s*/i, "")
18+
.trim()
19+
.toLowerCase();
1720

1821
if (!query) {
1922
await msg.reply("Please specify a command to unload.");
@@ -27,7 +30,13 @@ export default async function (msg: Message): Promise<void> {
2730

2831
if (query === "unload") {
2932
await msg.reply(
30-
`"${query}" can't be un-unloaded, 'cause the command that unloads can't be unloaded when it unloads unloading!`
33+
`"${query}" can't be un-unloaded, 'cause the command that unloads can't be unloaded when it unloads unloading!`,
34+
);
35+
return;
36+
}
37+
if (query === "reload") {
38+
await msg.reply(
39+
`"${query}" can't be un-unloaded, 'cause the command that reloads can't be unloaded when it reloads reloading!`,
3140
);
3241
return;
3342
}
@@ -42,8 +51,7 @@ export default async function (msg: Message): Promise<void> {
4251
delete require.cache[require.resolve(filePath)];
4352
delete commands[query];
4453
found = true;
45-
} catch {
46-
}
54+
} catch {}
4755
}
4856
}
4957

src/commands/video.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "path";
55
import { Innertube, UniversalCache, Utils } from "youtubei.js";
66
import log from "../components/utils/log";
77
import { DownloadOptions } from "youtubei.js/dist/src/types";
8+
import { fileExists } from "../components/utils/file";
89

910
export const info = {
1011
command: "video",
@@ -15,15 +16,6 @@ export const info = {
1516
cooldown: 5000,
1617
};
1718

18-
const fileExists = async (filePath: string) => {
19-
try {
20-
await fs.promises.access(filePath, fs.constants.F_OK);
21-
return true;
22-
} catch {
23-
return false;
24-
}
25-
};
26-
2719
async function safeDownload(
2820
yt: Innertube,
2921
id: string,

0 commit comments

Comments
 (0)