Skip to content

Commit 8f05116

Browse files
committed
feat: add CMD_PREFIX; fix reset cmd
1 parent 6499c17 commit 8f05116

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

src/clients/whatsapp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import qrcode from "qrcode";
22
import WAWebJS, { Message } from "whatsapp-web.js";
33
import { handleMessage } from "../handlers/message";
44
import { handleSelfMessage } from "../handlers/message/self";
5-
import { BOT_PREFIX } from "../constants";
5+
import { BOT_PREFIX, CMD_PREFIX } from "../constants";
66
import { handleCommand } from "../handlers/command";
77
import { shouldIgnore } from "../helpers/message";
88

@@ -53,7 +53,7 @@ whatsapp.on("ready", async () => {
5353
whatsapp.on("message", async (message) => {
5454
if (await shouldIgnore(message)) return;
5555

56-
const isCommand = message.body.startsWith("!");
56+
const isCommand = message.body.startsWith(CMD_PREFIX);
5757
if (isCommand) {
5858
return handleCommand(message);
5959
} else {

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dotenv from "dotenv";
22
import dotenvExpand from "dotenv-expand";
33
dotenvExpand.expand(dotenv.config());
44

5+
export const CMD_PREFIX = process.env.CMD_PREFIX?.trim() || "!";
56
export const REPLY_RRULES = process.env.REPLY_RRULES || "true";
67
export const ENABLE_REACTIONS = process.env.ENABLE_REACTIONS || "true";
78
export const ENABLE_SOURCES = process.env.ENABLE_SOURCES || "true";

src/crud/conversation.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// @ts-ignore
33
import { BingAIClientResponse } from "@waylaidwanderer/chatgpt-api";
44
import { prisma } from "../clients/prisma";
5-
import { Chat, Message } from "whatsapp-web.js";
65

76
export async function createConversation(
87
completion: BingAIClientResponse,
@@ -24,16 +23,23 @@ export async function createConversation(
2423
});
2524
}
2625

26+
export async function deleteAllConversations() {
27+
await prisma.cache.deleteMany();
28+
return await prisma.bingConversation.deleteMany();
29+
}
30+
2731
export async function deleteConversation(chatId: string) {
28-
const conversation = await prisma.bingConversation.delete({
29-
where: { waChatId: chatId },
30-
select: { jailbreakId: true },
31-
});
32+
const conversation = await getConversationFor(chatId);
33+
if (!conversation) return;
3234

3335
if (conversation.jailbreakId)
3436
await prisma.cache.delete({
3537
where: { key: `bing:${conversation.jailbreakId}` },
3638
});
39+
40+
return await prisma.bingConversation.delete({
41+
where: { waChatId: chatId },
42+
});
3743
}
3844

3945
export async function getConversationFor(chatId: string) {

src/handlers/command/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { Message } from "whatsapp-web.js";
22
import { setStatusFor } from "../../helpers/message";
33
import { log } from "../../helpers/utils";
4-
import { BOT_PREFIX } from "../../constants";
4+
import { BOT_PREFIX, CMD_PREFIX } from "../../constants";
55
import { handleJailbreak } from "./jailbreak";
66
import { handleReset } from "./reset";
77

88
export async function handleCommand(message: Message) {
9-
const [command, ..._args] = message.body.split(" ");
9+
const [command, ..._args] = message.body.split(CMD_PREFIX)[1].split(" ");
1010
const args = _args.join(" ");
1111
let reply: Message;
1212

1313
await log(message);
1414
await setStatusFor(message, "working");
1515

1616
switch (command) {
17-
case "!ping":
17+
case "ping":
1818
reply = await message.reply(BOT_PREFIX + "pong!");
1919
break;
20-
case "!reset":
20+
case "reset":
2121
reply = await handleReset(message, args);
2222
break;
23-
case "!jailbreak":
23+
case "jailbreak":
2424
reply = await handleJailbreak(message, args);
2525
break;
2626
default:

src/handlers/command/reset.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import { Message } from "whatsapp-web.js";
22
import { prisma } from "../../clients/prisma";
33
import { BOT_PREFIX } from "../../constants";
4-
import { deleteConversation } from "../../crud/conversation";
4+
import {
5+
deleteAllConversations,
6+
deleteConversation,
7+
getConversationFor,
8+
} from "../../crud/conversation";
59

610
type ResetArgs = "all" | (string & {});
711

812
export async function handleReset(message: Message, args: ResetArgs) {
913
let reply: Message;
1014

15+
const chat = await message.getChat();
16+
const waChat = await prisma.wAChat.findFirst({
17+
where: { id: chat.id._serialized },
18+
});
19+
1120
switch (args) {
12-
case "all":
13-
await prisma.bingConversation.deleteMany();
21+
case "all": // TODO: only superusers/bot owner should be able to do this
22+
await deleteAllConversations();
1423
reply = await message.reply(BOT_PREFIX + "deleted all conversations");
1524
break;
1625
default:
17-
const chat = await message.getChat();
18-
const conversation = await prisma.bingConversation.findFirst({
19-
where: { waChatId: chat.id._serialized },
20-
});
21-
22-
if (conversation) {
23-
await deleteConversation(chat.id._serialized);
26+
if (waChat) {
27+
await deleteConversation(waChat.id);
2428
reply = await message.reply(BOT_PREFIX + "deleted this conversation");
2529
break;
2630
}

0 commit comments

Comments
 (0)