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";
2
2
import WAWebJS , { Message } from "whatsapp-web.js" ;
3
3
import { handleMessage } from "../handlers/message" ;
4
4
import { handleSelfMessage } from "../handlers/message/self" ;
5
- import { BOT_PREFIX } from "../constants" ;
5
+ import { BOT_PREFIX , CMD_PREFIX } from "../constants" ;
6
6
import { handleCommand } from "../handlers/command" ;
7
7
import { shouldIgnore } from "../helpers/message" ;
8
8
@@ -53,7 +53,7 @@ whatsapp.on("ready", async () => {
53
53
whatsapp . on ( "message" , async ( message ) => {
54
54
if ( await shouldIgnore ( message ) ) return ;
55
55
56
- const isCommand = message . body . startsWith ( "!" ) ;
56
+ const isCommand = message . body . startsWith ( CMD_PREFIX ) ;
57
57
if ( isCommand ) {
58
58
return handleCommand ( message ) ;
59
59
} else {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import dotenv from "dotenv";
2
2
import dotenvExpand from "dotenv-expand" ;
3
3
dotenvExpand . expand ( dotenv . config ( ) ) ;
4
4
5
+ export const CMD_PREFIX = process . env . CMD_PREFIX ?. trim ( ) || "!" ;
5
6
export const REPLY_RRULES = process . env . REPLY_RRULES || "true" ;
6
7
export const ENABLE_REACTIONS = process . env . ENABLE_REACTIONS || "true" ;
7
8
export const ENABLE_SOURCES = process . env . ENABLE_SOURCES || "true" ;
Original file line number Diff line number Diff line change 2
2
// @ts -ignore
3
3
import { BingAIClientResponse } from "@waylaidwanderer/chatgpt-api" ;
4
4
import { prisma } from "../clients/prisma" ;
5
- import { Chat , Message } from "whatsapp-web.js" ;
6
5
7
6
export async function createConversation (
8
7
completion : BingAIClientResponse ,
@@ -24,16 +23,23 @@ export async function createConversation(
24
23
} ) ;
25
24
}
26
25
26
+ export async function deleteAllConversations ( ) {
27
+ await prisma . cache . deleteMany ( ) ;
28
+ return await prisma . bingConversation . deleteMany ( ) ;
29
+ }
30
+
27
31
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 ;
32
34
33
35
if ( conversation . jailbreakId )
34
36
await prisma . cache . delete ( {
35
37
where : { key : `bing:${ conversation . jailbreakId } ` } ,
36
38
} ) ;
39
+
40
+ return await prisma . bingConversation . delete ( {
41
+ where : { waChatId : chatId } ,
42
+ } ) ;
37
43
}
38
44
39
45
export async function getConversationFor ( chatId : string ) {
Original file line number Diff line number Diff line change 1
1
import { Message } from "whatsapp-web.js" ;
2
2
import { setStatusFor } from "../../helpers/message" ;
3
3
import { log } from "../../helpers/utils" ;
4
- import { BOT_PREFIX } from "../../constants" ;
4
+ import { BOT_PREFIX , CMD_PREFIX } from "../../constants" ;
5
5
import { handleJailbreak } from "./jailbreak" ;
6
6
import { handleReset } from "./reset" ;
7
7
8
8
export async function handleCommand ( message : Message ) {
9
- const [ command , ..._args ] = message . body . split ( " " ) ;
9
+ const [ command , ..._args ] = message . body . split ( CMD_PREFIX ) [ 1 ] . split ( " " ) ;
10
10
const args = _args . join ( " " ) ;
11
11
let reply : Message ;
12
12
13
13
await log ( message ) ;
14
14
await setStatusFor ( message , "working" ) ;
15
15
16
16
switch ( command ) {
17
- case "! ping" :
17
+ case "ping" :
18
18
reply = await message . reply ( BOT_PREFIX + "pong!" ) ;
19
19
break ;
20
- case "! reset" :
20
+ case "reset" :
21
21
reply = await handleReset ( message , args ) ;
22
22
break ;
23
- case "! jailbreak" :
23
+ case "jailbreak" :
24
24
reply = await handleJailbreak ( message , args ) ;
25
25
break ;
26
26
default :
Original file line number Diff line number Diff line change 1
1
import { Message } from "whatsapp-web.js" ;
2
2
import { prisma } from "../../clients/prisma" ;
3
3
import { BOT_PREFIX } from "../../constants" ;
4
- import { deleteConversation } from "../../crud/conversation" ;
4
+ import {
5
+ deleteAllConversations ,
6
+ deleteConversation ,
7
+ getConversationFor ,
8
+ } from "../../crud/conversation" ;
5
9
6
10
type ResetArgs = "all" | ( string & { } ) ;
7
11
8
12
export async function handleReset ( message : Message , args : ResetArgs ) {
9
13
let reply : Message ;
10
14
15
+ const chat = await message . getChat ( ) ;
16
+ const waChat = await prisma . wAChat . findFirst ( {
17
+ where : { id : chat . id . _serialized } ,
18
+ } ) ;
19
+
11
20
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 ( ) ;
14
23
reply = await message . reply ( BOT_PREFIX + "deleted all conversations" ) ;
15
24
break ;
16
25
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 ) ;
24
28
reply = await message . reply ( BOT_PREFIX + "deleted this conversation" ) ;
25
29
break ;
26
30
}
You can’t perform that action at this time.
0 commit comments