Skip to content

Commit 8be5c77

Browse files
committed
chore: bug fixes
1 parent e2a9e49 commit 8be5c77

7 files changed

Lines changed: 109 additions & 66 deletions

File tree

src/commands/admin.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/commands/demote.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
1-
import { Message } from "../types/message"
1+
import { Message } from "../types/message";
22
import log from "../components/utils/log";
3+
import { setAdmin } from "../components/services/user";
34

45
export const info = {
56
command: "demote",
6-
description: "Demote mentioned users from admin.",
7+
description: "Demote mentioned users from group or bot admin.",
78
usage: "demote <@user>",
89
example: "demote @user123",
9-
role: "admin",
10+
role: "super-admin",
1011
cooldown: 5000,
1112
};
1213

1314
export default async function (msg: Message): Promise<void> {
15+
const query = msg.body.replace(/^demote\b\s*/i, "").trim();
16+
17+
const bot = /--bot/i.test(query);
18+
const group = /--group/i.test(query);
19+
20+
if (!bot && !group) {
21+
await msg.reply("Please provide a valid flag: `--bot` or `--admin`.");
22+
return;
23+
}
24+
1425
if (msg.mentionedIds.length === 0) {
1526
await msg.reply("Please mention a user to demote.");
1627
return;
1728
}
1829

19-
const chat = await msg.getChat();
30+
if (group) {
31+
const chat = await msg.getChat();
2032

21-
if (!chat.isGroup) {
22-
await msg.reply("This command only works in groups.");
33+
if (!chat.isGroup) {
34+
await msg.reply("This command only works in groups.");
35+
return;
36+
}
37+
38+
const groupChat = chat as any;
39+
40+
try {
41+
for (const userId of msg.mentionedIds) {
42+
await groupChat.demoteParticipants([userId]);
43+
}
44+
} catch (err) {
45+
log.error("Demote", err);
46+
await msg.reply("Failed to demote user. Make sure I am an admin.");
47+
}
2348
return;
2449
}
2550

26-
const groupChat = chat as any;
51+
const lids = msg.mentionedIds.map((id) => id.split("@")[0]);
2752

28-
try {
29-
for (const userId of msg.mentionedIds) {
30-
await groupChat.demoteParticipants([userId]);
31-
}
32-
} catch (err) {
33-
log.error("Demote", err);
34-
await msg.reply("Failed to demote user. Make sure I am an admin.");
53+
for (const lid of lids) {
54+
await setAdmin(lid, true);
3555
}
56+
await msg.reply("You are now promoted as bot admin.");
3657
}

src/commands/help.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ function buildUserPage(
3939
return response;
4040
}
4141

42-
function buildAdminPage(adminCommands: string[]): string {
42+
function buildAdminPage(type: string, adminCommands: string[]): string {
4343
let response = `
44-
\`Help Admin\`
44+
\`Help ${type}\`
4545
help [command] for more details on a specific command.
4646
4747
| • ${adminCommands.join("\n | • ") || "_None_"}
@@ -50,7 +50,7 @@ function buildAdminPage(adminCommands: string[]): string {
5050
}
5151

5252
export default async function (msg: Message): Promise<void> {
53-
const match = /^help(?:\s+(?:--admin|\w+))?$/i.exec(msg.body.trim());
53+
const match = /^help(?:\s+(admin|super-admin|\d+))?$/i.exec(msg.body.trim());
5454
if (!match) return;
5555

5656
const query = msg.body
@@ -69,7 +69,7 @@ export default async function (msg: Message): Promise<void> {
6969
7070
*Usage:* ${matchCommands.usage || "No usage"}
7171
*Example:* ${matchCommands.example || "No example"}
72-
*Role:* ${matchCommands.role || "user"}
72+
*Role:* ${matchCommands.role || "User"}
7373
*Cooldown:* ${matchCommands.cooldown || 5000}ms
7474
`;
7575
await msg.reply(response);
@@ -82,7 +82,7 @@ export default async function (msg: Message): Promise<void> {
8282
.filter((cmd: CommandType) => cmd.role === "admin")
8383
.map((cmd: CommandType) => cmd.command)
8484
.sort((a, b) => a.localeCompare(b));
85-
await msg.reply(buildAdminPage(adminCommands));
85+
await msg.reply(buildAdminPage("Admin", adminCommands));
8686
return;
8787
}
8888

@@ -92,7 +92,7 @@ export default async function (msg: Message): Promise<void> {
9292
.filter((cmd: CommandType) => cmd.role === "super-admin")
9393
.map((cmd: CommandType) => cmd.command)
9494
.sort((a, b) => a.localeCompare(b));
95-
await msg.reply(buildAdminPage(superCommands));
95+
await msg.reply(buildAdminPage("Super Admin", superCommands));
9696
return;
9797
}
9898

src/commands/promote.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
1-
import { Message } from "../types/message"
1+
import { Message } from "../types/message";
22
import log from "../components/utils/log";
3+
import { setAdmin } from "../components/services/user";
34

45
export const info = {
56
command: "promote",
6-
description: "Promote mentioned users to admin.",
7+
description: "Promote mentioned users to group or bot admin.",
78
usage: "promote <@user>",
89
example: "promote @user123",
9-
role: "admin",
10+
role: "super-admin",
1011
cooldown: 5000,
1112
};
1213

1314
export default async function (msg: Message): Promise<void> {
15+
const query = msg.body.replace(/^promote\b\s*/i, "").trim();
16+
17+
const bot = /--bot/i.test(query);
18+
const group = /--group/i.test(query);
19+
20+
if (!bot && !group) {
21+
await msg.reply("Please provide a valid flag: `--bot` or `--admin`.");
22+
return;
23+
}
24+
1425
if (msg.mentionedIds.length === 0) {
1526
await msg.reply("Please mention a user to promote.");
1627
return;
1728
}
1829

19-
const chat = await msg.getChat();
30+
if (group) {
31+
const chat = await msg.getChat();
2032

21-
if (!chat.isGroup) {
22-
await msg.reply("This command only works in groups.");
33+
if (!chat.isGroup) {
34+
await msg.reply("This command only works in groups.");
35+
return;
36+
}
37+
38+
const groupChat = chat as any;
39+
40+
try {
41+
for (const userId of msg.mentionedIds) {
42+
await groupChat.promoteParticipants([userId]);
43+
}
44+
} catch (err) {
45+
log.error("Promote", err);
46+
await msg.reply("Failed to promote user. Make sure I am an admin.");
47+
}
2348
return;
2449
}
2550

26-
const groupChat = chat as any;
51+
const lids = msg.mentionedIds.map((id) => id.split("@")[0]);
2752

28-
try {
29-
for (const userId of msg.mentionedIds) {
30-
await groupChat.promoteParticipants([userId]);
31-
}
32-
} catch (err) {
33-
log.error("Promote", err);
34-
await msg.reply("Failed to promote user. Make sure I am an admin.");
53+
for (const lid of lids) {
54+
await setAdmin(lid, true);
3555
}
56+
await msg.reply("You are now demoted as bot admin.");
3657
}

src/commands/stalk.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Message } from "../types/message";
2-
import { getUserbyLid } from "../components/services/user";
2+
import { getUserbyLid, isAdmin } from "../components/services/user";
33
import redis from "../components/redis";
44
import { client } from "../components/client";
55
import { MessageMedia } from "whatsapp-web.js";
@@ -57,8 +57,15 @@ export default async function (msg: Message): Promise<void> {
5757
return;
5858
}
5959

60+
const botId = (await client()).info.wid._serialized;
6061
const jid = msg.mentionedIds[0];
6162
const lid = jid.split("@")[0];
63+
const isUserAdmin = await isAdmin(lid);
64+
65+
if (lid === botId || isUserAdmin) {
66+
await msg.reply("You cannot do that, please mention someone else.");
67+
return;
68+
}
6269

6370
const [user, isBlockPermanently, isBlockedTemporarily, userProfilePicture] =
6471
await Promise.all([

src/components/events/message.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,17 @@ export default async function (msg: Message, type: string): Promise<void> {
219219
return;
220220
}
221221

222+
const isSuperAdmin = msg.fromMe;
223+
const isAdminUser = isUserAdmin || isSuperAdmin;
224+
222225
if (
223-
(handler.role === "super-admin" && !msg.fromMe) ||
224-
(handler.role === "admin" && !(isUserAdmin || msg.fromMe))
226+
(handler.role === "super-admin" && !isSuperAdmin) ||
227+
(handler.role === "admin" && !isAdminUser)
225228
) {
226229
return;
227230
}
228231

232+
229233
log.info("Message", lid, key);
230234
msg.body = newMessageBody;
231235

src/components/utils/log.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import log from "npmlog";
2-
import fs from "fs";
3-
import path from "path";
2+
3+
if (process.env.DEBUG === "false") {
4+
const methodsToPatch = ["error", "warn"];
5+
6+
for (const method of methodsToPatch) {
7+
const original = log[method].bind(log);
8+
9+
log[method] = function (prefix: string, ...args: any[]) {
10+
const formattedArgs = args.map((arg) => {
11+
if (arg instanceof Error) {
12+
return `${arg.name}: ${arg.message}`;
13+
}
14+
return arg;
15+
});
16+
17+
return original(prefix, ...formattedArgs);
18+
};
19+
}
20+
}
421

522
export default log;

0 commit comments

Comments
 (0)