Skip to content

Commit

Permalink
refactor: 💡 improve support for RTL text
Browse files Browse the repository at this point in the history
  • Loading branch information
heldrida committed Dec 14, 2023
1 parent 71b6b1f commit 74cc221
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
22 changes: 20 additions & 2 deletions src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "!";

Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
10 changes: 10 additions & 0 deletions src/Utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

0 comments on commit 74cc221

Please sign in to comment.