From 0c4f3303a9362bb651c4715bacf906b2a7db1ff9 Mon Sep 17 00:00:00 2001 From: bertybuttface <110790513+bertybuttface@users.noreply.github.com> Date: Thu, 5 Jan 2023 16:54:37 +0000 Subject: [PATCH] Make MATRIX_PREFIX configurable at conversation level At the conversation level. --- src/handlers.ts | 12 ++++++++---- src/interfaces.ts | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/handlers.ts b/src/handlers.ts index fd33887..0e36e12 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -17,12 +17,16 @@ export async function handleRoomEvent(client: MatrixClient, chatGPT: ChatGPTAPIB const rootEventId: string = (relatesTo !== undefined && relatesTo.event_id !== undefined) ? relatesTo.event_id : event.event_id; const storedValue: string = await client.storageProvider.readValue('gpt-' + rootEventId) const storedConversation: StoredConversation = (storedValue !== undefined) ? JSON.parse(storedValue) : undefined; + const config: StoredConversationConfig = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}; + const MATRIX_PREFIX: string = (config.MATRIX_PREFIX === undefined) ? MATRIX_DEFAULT_PREFIX : config.MATRIX_PREFIX const MATRIX_PREFIX_REPLY:boolean = (config.MATRIX_PREFIX_REPLY === undefined) ? MATRIX_DEFAULT_PREFIX_REPLY : config.MATRIX_PREFIX_REPLY - const shouldBePrefixed: boolean = ((MATRIX_DEFAULT_PREFIX) && (relatesTo === undefined)) || (MATRIX_PREFIX_REPLY && (relatesTo !== undefined)); - if (event.sender === MATRIX_BOT_USERNAME) return; // Don't reply to ourself - if (Date.now() - event.origin_server_ts > 10000) return; // Don't reply to old messages - if (shouldBePrefixed && !event.content.body.startsWith(MATRIX_DEFAULT_PREFIX)) return; // Don't reply without prefix if prefixed + + const shouldBePrefixed: boolean = ((MATRIX_PREFIX) && (relatesTo === undefined)) || (MATRIX_PREFIX_REPLY && (relatesTo !== undefined)); + + if (event.sender === MATRIX_BOT_USERNAME) return; // Don't reply to ourself + if (Date.now() - event.origin_server_ts > 10000) return; // Don't reply to old messages + if (shouldBePrefixed && !event.content.body.startsWith(MATRIX_PREFIX)) return; // Don't reply without prefix if prefixed await Promise.all([client.sendReadReceipt(roomId, event.event_id), client.setTyping(roomId, true, 10000)]); diff --git a/src/interfaces.ts b/src/interfaces.ts index 4d9883f..b927bfb 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -69,6 +69,7 @@ export type MatrixInviteEvent = CommonMatrixEventFields & { export type MembershipType = 'leave' | 'invite' | 'join' export type StoredConversationConfig = { + MATRIX_PREFIX?: string; MATRIX_PREFIX_REPLY?: boolean; }