]> git.puffer.fish Git - matthieu/gru.git/commitdiff
better sanitizing
authorMatthieu Pignolet <matthieu@matthieu-dev.xyz>
Fri, 21 Jun 2024 14:25:46 +0000 (18:25 +0400)
committerMatthieu Pignolet <matthieu@matthieu-dev.xyz>
Fri, 21 Jun 2024 14:25:46 +0000 (18:25 +0400)
discordjs/src/index.mjs

index 6021b0c6a9b6acb99ca4892e00518595743be2ce..1d5c81f0982761f8b28eba25a9b8423c59fd2b30 100644 (file)
@@ -32,24 +32,30 @@ const sanitizeWord = (sentence) => {
     .trim()
     .split(" ")
     .slice(-1)[0]
+    .normalize('NFKD')
+    .replace(/[\u0300-\u036f]/g, "")
+    .replaceAll(/(?:https?|ftp):\/\/[\n\S]+/g, "")
+    .replaceAll(/\:([a-z]|[A-Z])+\:/g, "")
     .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, '');
+    .replaceAll(/([^A-z])/g, "");
   return lastWord;
 };
 const re = /([a-z]|[A-Z])+/g;
 const countChars = (str) => ((str || '').match(re) || []).length;
 
+const specialChannels = [
+  "1248226018406301696"
+]
+
 const messageAction = async (message) => {
   if (message.author.bot) return;
   const cleanText = sanitizeWord(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)) {
+    const shouldReply = (Math.random() > 0.98 || specialChannels.includes(message.channelId) || message.guild == null);
+    if ((response || response === "") && shouldReply) {
       message.reply(response);
     }
   }