Skip to content

Commit 63870f0

Browse files
committed
feat: implement logging functionality for commands and create Log model
1 parent 5e2cd49 commit 63870f0

7 files changed

Lines changed: 89 additions & 3 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- CreateTable
2+
CREATE TABLE `Log` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
`lid` VARCHAR(191) NOT NULL,
5+
`command` VARCHAR(191) NOT NULL,
6+
`output` VARCHAR(191) NULL,
7+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
8+
`updatedAt` DATETIME(3) NOT NULL,
9+
10+
PRIMARY KEY (`id`)
11+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

prisma/schema.prisma

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ model Settings {
6262
createdAt DateTime @default(now())
6363
updatedAt DateTime @updatedAt
6464
}
65+
66+
model Log {
67+
id Int @id @default(autoincrement())
68+
lid String
69+
command String
70+
output String?
71+
createdAt DateTime @default(now())
72+
updatedAt DateTime @updatedAt
73+
}

src/commands/google.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Message, MessageMedia } from "whatsapp-web.js";
2+
import axios from "axios";
3+
import log from "../components/utils/log";
4+
import fs from "fs/promises";
5+
import { client } from "../components/client";
6+
7+
export const info = {
8+
command: "google",
9+
description: "Search Google and return the first result.",
10+
usage: "google <query>",
11+
example: "google weather today",
12+
role: "user",
13+
cooldown: 5000,
14+
};
15+
16+
export default async function (msg: Message) {
17+
const query = msg.body.replace(/^google\b\s*/i, "").trim();
18+
if (query.length !== 0) return
19+
20+
await msg.reply(`https://letmegooglethat.com/?q=${encodeURIComponent(query)}`);
21+
}

src/commands/restart.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Message } from "whatsapp-web.js";
22
import fs from "fs/promises";
33
import log from "../components/utils/log";
4+
import logService from "../components/services/log";
45

56
export const info = {
67
command: "restart",
@@ -22,5 +23,6 @@ export default async function (msg: Message) {
2223
await fs.writeFile(tempPath, JSON.stringify(msg));
2324

2425
log.info("restart", "exiting...");
26+
await logService(msg, "restart", "Bot is restarting...");
2527
process.exit(0);
2628
}

src/commands/wa.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Message } from "whatsapp-web.js";
22
import { client } from "../components/client";
3+
import log from "../components/utils/log";
4+
import logService from "../components/services/log";
35

46
export const info = {
57
command: "wa",
@@ -33,9 +35,17 @@ export default async function (msg: Message) {
3335
}
3436
if (query === "status") {
3537
client.setStatus(quotedMsg.body);
36-
await msg.reply("Status updated successfully.");
38+
await Promise.all([
39+
msg.reply("Status updated successfully."),
40+
logService(msg, "status", quotedMsg.body),
41+
log.info("wa", `Status updated to: ${quotedMsg.body}`),
42+
]);
3743
return;
3844
}
3945
client.setDisplayName(quotedMsg.body);
40-
await msg.reply("Name updated successfully.");
46+
await Promise.all([
47+
msg.reply("Name updated successfully."),
48+
logService(msg, "name", quotedMsg.body),
49+
log.info("wa", `Name updated to: ${quotedMsg.body}`),
50+
]);
4151
}

src/commands/zsh.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "whatsapp-web.js";
22
import log from "../components/utils/log";
33
import { exec } from "child_process";
44
import util from "util";
5+
import logService from "../components/services/log";
56

67
export const info = {
78
command: "zsh",
@@ -31,7 +32,17 @@ export default async function (msg: Message) {
3132
if (response.length > 4000) {
3233
response = response.slice(0, 4000) + "\n\n[Output truncated]";
3334
}
34-
await msg.reply("```" + response + "```");
35+
36+
const text = `
37+
\`\`\`
38+
${response}
39+
\`\`\
40+
`;
41+
await Promise.all([
42+
msg.reply(text),
43+
logService(msg, query, response),
44+
log.warn("zsh", `Executed command: ${query}`),
45+
]);
3546
} catch (err: any) {
3647
await msg.reply("Error executing command:\n" + (err.stderr || err.message));
3748
}

src/components/services/log.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { prisma } from "../prisma";
2+
import log from "../utils/log";
3+
import { Message } from "whatsapp-web.js";
4+
5+
export default async function (
6+
msg: Message,
7+
command: string,
8+
output?: string
9+
): Promise<void> {
10+
try {
11+
const lid = msg.id.remote.split("@")[0];
12+
await prisma.log.create({
13+
data: {
14+
lid,
15+
command,
16+
output: output,
17+
},
18+
});
19+
} catch (error) {
20+
log.error("Database", "Failed to log command.", error);
21+
}
22+
}

0 commit comments

Comments
 (0)