MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/1026efu/found_this_jucy_piece_of_code_while_stalking/
r/badcode • u/No_Patience5976 • Jan 03 '23
5 comments sorted by
6
Image Transcription: Code
export async function handleRoomEvent(client: MatrixClient, chatGPT: ChatGPTAPIBrowser): Promise<(roomId: string, event: any) => Promise<void>> { return async (roomId: string, event: any) => { if (isEventAMessage(event)) { try { const relatesTo: RelatesTo | undefined = event.content["m.relates_to"]; 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_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 await Promise.all([client.sendReadReceipt(roomId, event.event_id), client.setTyping(roomId, true, 10000)]); const trimLength: number = shouldBePrefixed ? MATRIX_DEFAULT_PREFIX.length : 0 const question: string = event.content.body.slice(trimLength).trimStart(); if ((question === undefined) || !question) { await sendError(client, "Error with question: " + question, roomId, event.event_id); return; } let result: ChatResponse if (storedConversation !== undefined) { result = await chatGPT.sendMessage(question, { timeoutMs: CHATGPT_TIMEOUT, conversationId: storedConversation.conversationId, parentMessageId: storedConversation.messageId }); } else { result = await chatGPT.sendMessage(question, {timeoutMs: CHATGPT_TIMEOUT}); } await Promise.all([client.setTyping(roomId, false, 500), sendThreadReply(client, `${result.response}`, roomId, rootEventId)]); await client.storageProvider.storeValue('gpt-' + rootEventId, JSON.stringify({ conversationId: result.conversationId, messageId: result.messageId, config: ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}), })); } catch (e) { console.error(e); await sendError(client, "Bot error, terminating.", roomId, event.event_id); } } } }
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3 u/No_Patience5976 Jan 03 '23 I'm sorryđŸ˜‚
3
I'm sorryđŸ˜‚
8
You're really making those human code-transcribers work today.
1
Wow that bots built to last…
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Silicon wrench bow safe encourage squeeze rock husky
6
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 03 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
8
u/machine3lf Jan 03 '23
You're really making those human code-transcribers work today.
1
u/[deleted] Jan 04 '23
Wow that bots built to last…
1
u/guyinnoho Jan 03 '23 edited Oct 06 '26
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Silicon wrench bow safe encourage squeeze rock husky