From ed6e4797a1c8c7d12efeb34d287c601d7ff261fa Mon Sep 17 00:00:00 2001 From: Max Charrier Date: Mon, 4 Nov 2024 20:49:54 +0100 Subject: [PATCH] New message reply algorithm based on time duration or random counter Signed-off-by: Max Charrier --- discordjs/src/index.mjs | 48 +++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/discordjs/src/index.mjs b/discordjs/src/index.mjs index da33d32..80c943d 100644 --- a/discordjs/src/index.mjs +++ b/discordjs/src/index.mjs @@ -3,7 +3,14 @@ import { Client, GatewayIntentBits } from 'discord.js'; import { request } from "undici"; // Create a new client instance -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages] }); +const client = new Client({ intents: + [ + GatewayIntentBits.Guilds, + GatewayIntentBits.MessageContent, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.DirectMessages + ] +}); // `autofeur_db` service export const DB = process.env.DB || "http://localhost:3000"; @@ -49,22 +56,41 @@ const specialChannels = [ ] const ignoredEveryoneChannels = [ - "1055130476395909210" + "1055130476395909210" ] -let counter = 0; +let messageReplyCounter = 0; const messageAction = async (message) => { if (message.author.bot) return; - counter += 1; - console.log("counter is at", counter); - let shouldReplyByCounter = counter >= 60; - let shouldReply = (shouldReplyByCounter || specialChannels.includes(message.channelId) || message.guild == null); + messageReplyCounter += 1; + console.log("counter is at", messageReplyCounter); + + let currentTimestamp = Date.now(); + let lastMessageTimestamp = await message + .channel + .messages + .fetch({ + limit: 2, + cache : false + }) + .last() + .createdTimestamp; + + let shouldReplyByTimestamp = currentTimestamp - lastMessageTimestamp >= 3600; + let shouldReplyByCounter = + messageReplyCounter >= Math.floor(Math.random() * 75) + 35; + let shouldReply = ( + shouldReplyByTimestamp || + shouldReplyByCounter || + specialChannels.includes(message.channelId) || + message.guild == null + ); if (shouldReply) { - let oltCounter = counter; - if (shouldReplyByCounter) { - counter = 0; + let oldCounter = messageReplyCounter; + if (shouldReplyByTimestamp || shouldReplyByCounter) { + messageReplyCounter = 0; } const cleanText = sanitizeWord(message.cleanContent); if (countChars(cleanText) > 0) { @@ -75,7 +101,7 @@ const messageAction = async (message) => { message.reply(response); } } else if (shouldReplyByCounter) { - counter = oltCounter; + messageReplyCounter = oldCounter; } } -- 2.39.5