summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Charrier <max@puffer.fish>2024-11-04 20:49:54 +0100
committerMax Charrier <max@puffer.fish>2024-11-04 20:49:54 +0100
commited6e4797a1c8c7d12efeb34d287c601d7ff261fa (patch)
treedfcbed64ce80e7506c85a478ddc976014546f5e6
parentc8e3a5516497b10904543478f8314735dcc4678e (diff)
New message reply algorithm based on time duration or random counter
Signed-off-by: Max Charrier <max@puffer.fish>
-rw-r--r--discordjs/src/index.mjs48
1 files 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;
}
}