File tree Expand file tree Collapse file tree 5 files changed +33
-22
lines changed Expand file tree Collapse file tree 5 files changed +33
-22
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import qrcode from "qrcode";
22import WAWebJS , { Message } from "whatsapp-web.js" ;
33import { handleMessage } from "../handlers/message" ;
44import { handleSelfMessage } from "../handlers/message/self" ;
5- import { BOT_PREFIX } from "../constants" ;
5+ import { BOT_PREFIX , CMD_PREFIX } from "../constants" ;
66import { handleCommand } from "../handlers/command" ;
77import { shouldIgnore } from "../helpers/message" ;
88
@@ -53,7 +53,7 @@ whatsapp.on("ready", async () => {
5353whatsapp . 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 {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import dotenv from "dotenv";
22import dotenvExpand from "dotenv-expand" ;
33dotenvExpand . expand ( dotenv . config ( ) ) ;
44
5+ export const CMD_PREFIX = process . env . CMD_PREFIX ?. trim ( ) || "!" ;
56export const REPLY_RRULES = process . env . REPLY_RRULES || "true" ;
67export const ENABLE_REACTIONS = process . env . ENABLE_REACTIONS || "true" ;
78export const ENABLE_SOURCES = process . env . ENABLE_SOURCES || "true" ;
Original file line number Diff line number Diff line change 22// @ts -ignore
33import { BingAIClientResponse } from "@waylaidwanderer/chatgpt-api" ;
44import { prisma } from "../clients/prisma" ;
5- import { Chat , Message } from "whatsapp-web.js" ;
65
76export 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+
2731export 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
3945export async function getConversationFor ( chatId : string ) {
Original file line number Diff line number Diff line change 11import { Message } from "whatsapp-web.js" ;
22import { setStatusFor } from "../../helpers/message" ;
33import { log } from "../../helpers/utils" ;
4- import { BOT_PREFIX } from "../../constants" ;
4+ import { BOT_PREFIX , CMD_PREFIX } from "../../constants" ;
55import { handleJailbreak } from "./jailbreak" ;
66import { handleReset } from "./reset" ;
77
88export 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 :
Original file line number Diff line number Diff line change 11import { Message } from "whatsapp-web.js" ;
22import { prisma } from "../../clients/prisma" ;
33import { BOT_PREFIX } from "../../constants" ;
4- import { deleteConversation } from "../../crud/conversation" ;
4+ import {
5+ deleteAllConversations ,
6+ deleteConversation ,
7+ getConversationFor ,
8+ } from "../../crud/conversation" ;
59
610type ResetArgs = "all" | ( string & { } ) ;
711
812export 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 }
You can’t perform that action at this time.
0 commit comments