Skip to content

Commit c893d76

Browse files
committed
feat: added admin settings and preferences
1 parent e2d85c0 commit c893d76

7 files changed

Lines changed: 129 additions & 11 deletions

File tree

src/commands/callreject.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Message } from "../../types/message";
2+
import { saveSetting } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "callreject",
7+
description: "Enable/disable automatically rejecting calls.",
8+
usage: "callreject <on|off>",
9+
example: "callreject on",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
const query = msg.body.replace(/^callreject\b\s*/i, "").trim();
16+
if (query.length === 0 && !/(on|off)/.test(query)) {
17+
await msg.reply("Please provide a value its either on or off.");
18+
return;
19+
}
20+
21+
await saveSetting("call_reject", query);
22+
await msg.reply(`Call Reject successfuly set \`${query}\`.`);
23+
}

src/commands/printsettings.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Message } from "../../types/message";
2+
import { getAllSettings } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "printsettings",
7+
description: "Send all settings value",
8+
usage: "printsettings",
9+
example: "printsettings",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
if (!/^printsettings/i.test(msg.body)) return;
16+
17+
const allSettings = await getAllSettings();
18+
19+
if (!allSettings || Object.keys(allSettings).length === 0) {
20+
await msg.reply("No settings found.");
21+
return;
22+
}
23+
24+
const formatted = Object.entries(allSettings)
25+
.map(([name, value]) => `${name} : ${value}`)
26+
.join("\n ");
27+
28+
const settings = `
29+
\`Settings\`
30+
31+
${formatted}
32+
`;
33+
await msg.reply(settings);
34+
}

src/commands/recentedit.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Message } from "../../types/message";
2+
import { saveSetting } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "resentedit",
7+
description: "Enable/disable automatically resending of edit messages.",
8+
usage: "resentedit <on|off>",
9+
example: "resentedit on",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
const query = msg.body.replace(/^resentedit\b\s*/i, "").trim();
16+
if (query.length === 0 && !/(on|off)/.test(query)) {
17+
await msg.reply("Please provide a value its either on or off.");
18+
return;
19+
}
20+
21+
await saveSetting("resent_edit", query);
22+
await msg.reply(`Resent Edit successfuly set \`${query}\`.`);
23+
}

src/commands/resentunsend.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Message } from "../../types/message";
2+
import { saveSetting } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "recentunsent",
7+
description: "Enable/disable automatically resending of unsend messages.",
8+
usage: "recentunsent <on|off>",
9+
example: "recentunsent on",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
const query = msg.body.replace(/^recentunsent\b\s*/i, "").trim();
16+
if (query.length === 0 && !/(on|off)/.test(query)) {
17+
await msg.reply("Please provide a value its either on or off.");
18+
return;
19+
}
20+
21+
await saveSetting("resent_unsent", query);
22+
await msg.reply(`Resent Unsent successfuly set \`${query}\`.`);
23+
}

src/components/events/call.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Call } from "whatsapp-web.js";
22
import log from "../utils/log";
33
import { client } from "../client";
4+
import { getSetting } from "../services/settings";
45

56
const voiceResponses = [
67
"Sorry, I can't take voice calls right now.",
@@ -23,6 +24,8 @@ function getRandomResponse(arr: string[]) {
2324
export default async function (call: Call) {
2425
try {
2526
if (call.fromMe) return;
27+
const isCallMustReject = await getSetting("call_reject");
28+
if (!isCallMustReject || isCallMustReject == "off") return;
2629

2730
await call.reject();
2831
if (!call.from) return;

src/components/events/edit.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "whatsapp-web.js";
22
import { getUserbyLid } from "../services/user";
33
import { addMessage } from "../services/message";
44
import log from "../utils/log";
5+
import { getSetting } from "../services/settings";
56

67
/*
78
* TODO: Implement settings to enable/disable this event.
@@ -14,12 +15,17 @@ export default async function (
1415
) {
1516
if (msg.fromMe) return;
1617
const lid = (msg.author ?? msg.from).split("@")[0];
17-
// const isGroup = !!msg.author;
18-
// const user = await getUserbyLid(msg.from) || "Your";
19-
//
18+
19+
// log
2020
log.info("EditMessage", lid, prevBody);
2121
await addMessage(msg, prevBody, "edit");
22-
// await msg.reply(
23-
// `${isGroup ? user : "Your"} message was edited from "${prevBody}".`
24-
// );
22+
23+
const isMustResent = await getSetting("resent_edit");
24+
if (!isMustResent || isMustResent == "off") return;
25+
26+
const isGroup = !!msg.author;
27+
const user = (await getUserbyLid(msg.from)) || "Your";
28+
await msg.reply(
29+
`${isGroup ? user : "Your"} message was edited from "${prevBody}".`,
30+
);
2531
}

src/components/events/revoke.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "whatsapp-web.js";
22
import { getUserbyLid } from "../services/user";
33
import { addMessage } from "../services/message";
44
import log from "../utils/log";
5+
import { getSetting } from "../services/settings";
56

67
/*
78
* TODO: Implement settings to enable/disable this event.
@@ -10,11 +11,16 @@ import log from "../utils/log";
1011
export default async function (msg: Message, revoked_msg?: Message) {
1112
if (msg.fromMe || !revoked_msg) return;
1213
const lid = (msg.author ?? msg.from).split("@")[0];
13-
// const isGroup = !!msg.author;
14-
// const user = (await getUserbyLid(msg.from)) || "Your";
14+
const isGroup = !!msg.author;
15+
1516
log.info("RevokeMessage", lid, revoked_msg?.body);
1617
await addMessage(msg, revoked_msg?.body, "revoke");
17-
// await msg.reply(
18-
// `${isGroup ? user : "Your"} message "${revoked_msg?.body}" was deleted.`
19-
// );
18+
19+
const isMustResent = await getSetting("resent_unsent");
20+
if (!isMustResent || isMustResent == "off") return;
21+
22+
const user = (await getUserbyLid(msg.from)) || "Your";
23+
await msg.reply(
24+
`${isGroup ? user : "Your"} message "${revoked_msg?.body}" was deleted.`
25+
);
2026
}

0 commit comments

Comments
 (0)