diff --git a/package-lock.json b/package-lock.json index 40153d4..c9808c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "dayjs": "^1.11.9", "discord.js": "^14.13.0", "dotenv": "^16.0.3", + "is-rtl-text": "^0.0.2", "mongoose": "^8.0.1" }, "devDependencies": { @@ -1284,6 +1285,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-rtl-text": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/is-rtl-text/-/is-rtl-text-0.0.2.tgz", + "integrity": "sha512-kd6yxXHPW12vAuNDP5dFy0gIWYaiDMoskIir0soyrLiEsnesWNKUABTPtHgiJ0tRuV3FHstm1UuH/q/C2c5n7Q==" + }, "node_modules/is-shared-array-buffer": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-shared-array-buffer/-/is-shared-array-buffer-1.0.2.tgz", diff --git a/package.json b/package.json index 79eefd5..5f647cd 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "dayjs": "^1.11.9", "discord.js": "^14.13.0", "dotenv": "^16.0.3", + "is-rtl-text": "^0.0.2", "mongoose": "^8.0.1" } } diff --git a/src/Commands/index.ts b/src/Commands/index.ts index 49cb84d..befb3be 100644 --- a/src/Commands/index.ts +++ b/src/Commands/index.ts @@ -13,6 +13,7 @@ import { onAskSucceededResponseMsg, } from "../Messages/index.js"; import { llmQueue, MongoQuery } from "../LLM/index.js"; +import { isRTLText, convertRtfToPlain } from "../Utils/text.js"; const PREFIX = "!"; @@ -148,7 +149,18 @@ const CommandAskTrigger: CommandTrigger = { cb: async (msg) => { const user = msg.author; let query = msg.content.split(Commands.Ask)[1]; - query = query.replace(/[\W_]+/g, " ").trim(); + + const name = (() => { + if (isRTLText(query)) { + console.log("Warning! RTL text detected"); + + // TODO: translate text to english, or + // reply in detected language + } + + return query; + })(); + const cacheQuery = await MongoQuery.findOne({ query, }); @@ -184,10 +196,16 @@ const CommandAskTrigger: CommandTrigger = { const thread = await sendCreateThreadMsg({ msg, - name: query, + name, message, }); + if (!thread) { + console.log("Oops! Missing thread"); + + return; + } + const job = await llmQueue .createJob({ channelId: msg.channelId, diff --git a/src/Utils/index.ts b/src/Utils/index.ts index 6c4290c..d89e815 100644 --- a/src/Utils/index.ts +++ b/src/Utils/index.ts @@ -99,12 +99,17 @@ export const sendCreateThreadMsg = async ({ message: string; duration?: ThreadAutoArchiveDuration; }) => { - const thread = await msg.startThread({ - name, - autoArchiveDuration: duration, - }); + try { + const thread = await msg.startThread({ + name, + autoArchiveDuration: duration, + }); - await thread.send(message); + await thread.send(message); - return thread; + return thread; + } catch (err) { + console.log("Oops! Failed to create thread"); + console.error(err); + } }; diff --git a/src/Utils/text.ts b/src/Utils/text.ts index f73dab1..84c6ddf 100644 --- a/src/Utils/text.ts +++ b/src/Utils/text.ts @@ -18,3 +18,13 @@ export const textTemplt = ({ return output; }; + +export const isRTLText = (text: string) => + text.match(/[\u04c7-\u0591\u05D0-\u05EA\u05F0-\u05F4\u0600-\u06FF]/gi); + +export const convertRtfToPlain = (rtf: string) => { + rtf = rtf.replace(/\\par[d]?/g, ""); + return rtf + .replace(/\{\*?\\[^{}]+}|[{}]|\\\n?[A-Za-z]+\n?(?:-?\d+)?[ ]?/g, "") + .trim(); +};