summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthieu Pignolet <matthieu@matthieu-dev.xyz>2024-06-21 17:59:35 +0400
committerMatthieu Pignolet <matthieu@matthieu-dev.xyz>2024-06-21 17:59:35 +0400
commitd5b520a1bc9074ea3ca394f27eb90d33e246132b (patch)
tree207b9318aec62546c8550d5ef8a46bd40aa99f70
parent1a161774e641c42ca44c716ccff489458f5472a6 (diff)
Fix messages sanitizations
-rw-r--r--discordjs/src/index.mjs43
1 files changed, 12 insertions, 31 deletions
diff --git a/discordjs/src/index.mjs b/discordjs/src/index.mjs
index 3f0643e..63a5a1e 100644
--- a/discordjs/src/index.mjs
+++ b/discordjs/src/index.mjs
@@ -27,54 +27,35 @@ export const completeWord = (grapheme) => {
* @param sentence Raw discord sentence
* @returns The last word without any specials characters
*/
-const cutWord = (sentence) => {
+const sanitizeWord = (sentence) => {
let lastWord = sentence
- .replaceAll("?", "")
- .replaceAll("!", "")
- .replaceAll(".", "")
- .replaceAll(",", "")
- .replaceAll(";", "")
.trim()
.split(" ")
.slice(-1)[0]
+ .replaceAll(/(?>\?|\!|\.|\,|\;)/g, "")
.replaceAll(/(\s)?([^\x41-\x5A\s^\x61-\x7A^\xC0-\xFF])/g, "")
+ .replaceAll(/\<(?>[a-z]|[A-Z])+\:(?>[a-z]|[A-Z])+\:[0-9]+\>/g,)
.replaceAll(/(?:https?|ftp):\/\/[\n\S]+/g, '');
return lastWord;
};
+const re = /([a-z]|[A-Z])+/g;
+const countChars = (str) => ((str || '').match(re) || []).length;
-client.on("messageCreate", async (message) => {
- // we shall not repond to bots
+const messageAction = async (message) => {
if (message.author.bot) return;
+ const cleanText = sanitizeWord(message.cleanContent);
- try {
- // Get the completed word found by the db.
-
- let response = await completeWord(cutWord(message.cleanContent));
+ if (countChars(cleanText) > 0) {
+ let response = await completeWord();
// Ignore if there is no completion
if ((response || response === "") && (Math.random() > 0.98 || message.channelId == '1248226018406301696' || message.guild == null)) {
message.reply(response);
}
- } catch (e) {
- console.log(e);
}
-});
-
-client.on("messageUpdate", async (message) => {
- if (message.author.bot) return;
-
- try {
- // Get the completed word found by the db.
-
- let response = await completeWord(cutWord(message.cleanContent));
+};
- // Ignore if there is no completion
- if ((response || response === "") && (Math.random() > 0.98 || message.channelId == '1248226018406301696' || message.guild == null)) {
- message.reply(response);
- }
- } catch (e) {
- console.log(e);
- }
-});
+client.on("messageCreate", messageAction);
+client.on("messageUpdate", messageAction);
client.login(process.env.TOKEN);