Skip to content

Commit 12d60b7

Browse files
committed
feat: add accounts command and root-aware logout handling
Store root client id in Redis at startup; add an accounts command to list connected clients; allow logout with an optional clientId and handle root client logout; change logout command role to admin
1 parent 7144b79 commit 12d60b7

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/commands/accounts.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Message } from "../types/message";
2+
import { download } from "../components/utils/download";
3+
import { getClientIds } from "../components/services/account";
4+
5+
export const info = {
6+
command: "accounts",
7+
description: "List all accounts currently connected in.",
8+
usage: "accounts",
9+
example: "accounts",
10+
role: "user",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message): Promise<void> {
15+
let accounts = "";
16+
const accountsIds = await getClientIds();
17+
await Promise.all(
18+
Array.from(accountsIds.entries()).map(([clientId, isRoot]) => {
19+
accounts += `\`${clientId}\` - ${isRoot ? "Root" : "User"}\n`;
20+
}),
21+
);
22+
23+
msg.reply(`
24+
\`Accounts\`
25+
26+
${accounts}
27+
`);
28+
}

src/commands/logout.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ import { Message } from "../types/message";
22
import { removeAccount } from "../components/client";
33
import log from "../components/utils/log";
44
import { deleteAccount } from "../components/services/account";
5+
import redis from "../components/redis";
56

67
export const info = {
78
command: "logout",
89
description: "Log out the client from WhatsApp.",
9-
usage: "logout",
10+
usage: "logout [clientId]",
1011
example: "logout",
11-
role: "super-admin",
12+
role: "admin",
1213
cooldown: 5000,
1314
};
1415

1516
export default async function (msg: Message): Promise<void> {
1617
try {
18+
const query = msg.body.replace(/^logout\b\s*/i, "").trim();
19+
if (query.length > 0) {
20+
const isRootAccount = await redis.get('root_client_id');
21+
const rootClient = isRootAccount ? JSON.parse(isRootAccount) : null;
22+
if (rootClient.clientId === query) {
23+
removeAccount(query);
24+
deleteAccount(query);
25+
log.info("Logout", "Root client logged out successfully.");
26+
return;
27+
}
28+
}
1729
removeAccount(msg.clientId);
1830
deleteAccount(msg.clientId);
1931
log.info("Logout", "Client logged out successfully.");

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
PROJECT_THRESHOLD_MEMORY,
1818
} from "./config";
1919
import { getClientIds, getRootAccount } from "./components/services/account";
20+
import redis from "./components/redis";
2021

2122
const monitor = new MemoryMonitor({
2223
interval: 60000,
@@ -43,6 +44,11 @@ async function main() {
4344
const accountsIds = await getClientIds();
4445
await Promise.all(
4546
Array.from(accountsIds.entries()).map(async ([clientId, isRoot]) => {
47+
if (isRoot)
48+
await redis.set(
49+
"root_client_id",
50+
JSON.stringify({ clientId, lid: null, date: Date.now() }),
51+
);
4652
await addAccount(clientId, undefined, isRoot);
4753
}),
4854
);

0 commit comments

Comments
 (0)