How do you use regex With a Discord.js Counting system

Viewed 16

So I want to use regex to detect the last umber sent in a discord channel then the bot will only allow the next number to be sent So I dont have to keep updating the code when the bot gets restarted. I have added lines to help better see the counting part of the code

my Code

const Discord = require(`discord.js`);

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

require('dotenv').config();

const fs = require(`fs`);
const memberCounter = require(`./counters/member-counter`);

//-------------------------------------------------------------

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

let count = 481;
let timeout;

client.on('message', (message) => {
    let { channel, content, member } = message
  if (channel.id === '951944076641591356') {
    if (member.user.bot) return;

    if (Number(content) === count + 1) {
      count++;

      if (timeout) clearTimeout(timeout);

      timeout = setTimeout(
        () => channel.send(++count).catch(console.error),

        1800000
      );
    } else if (member.id !== client.user.id) {
      
      channel.send(`${member} messed up!`).then(msg => msg.delete({timeout: 1000}));
      message.delete({timeout: 1000})


      if (timeout) clearTimeout(timeout);
      
    }
  }
});


//-------------------------------------------------------------

['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord);
})

client.on("ready", () => {
    
    client.user.setActivity('HMMM', { type: "WATCHING"}).catch(console.error)
});

client.login(process.env.TOKEN);
1 Answers

the way you would need to do it would be to see if count is updated to the current count, if not, cache the last 50 messages as the bot had just started and get its content.

Firstly, set the count to -1

let count = -1;

This will allow us to see whether the count has been updated yet.

Next between the lines: if (member.user.bot) return; and if (Number(content) === count + 1) { add the following:

if(count === -1){
    // Get the last 50 messages
    const channelMessages = await message.channel.messages.fetch();
    
    // Get the second last messages content, the last message is the one the user just sent    
    const lastCount = channelMessages.at(1)?.content;
    
    // If it doesnt exist, or its not a number return (you could do something such as delte it)
    if (!lastCount || !/^[0-9]+$/.test(lastCount)) return;
    
    //Finally update the current count
    count = Number(lastCount);
}

That code should only run once, after there is a message sent and the bot has just started up, as the count will then be updated to something other than -1, so the code block in the if statement will not run.

The final code will look something like:

let count = -1;
let timeout;

client.on('message', (message) => {
    let { channel, content, member } = message
  if (channel.id === '951944076641591356') {
    if (member.user.bot) return;

if(count === -1){
    // Get the last 50 messages
    const channelMessages = await message.channel.messages.fetch();
    
    //Get the second last messages content    
    const lastCount = channelMessages.at(1)?.content;
    
    // If it doesnt exist, or its not a number return (you could do something such as delte it)
    if (!lastCount || !/^[0-9]+$/.test(lastCount)) return;
    
    //Finally update the current count
    count = Number(lastCount);
    }

    if (Number(content) === count + 1) {
      count++;

      if (timeout) clearTimeout(timeout);

      timeout = setTimeout(
        () => channel.send(++count).catch(console.error),

        1800000
      );
    } else if (member.id !== client.user.id) {
      
      channel.send(`${member} messed up!`).then(msg => msg.delete({timeout: 1000}));
      message.delete({timeout: 1000})


      if (timeout) clearTimeout(timeout);
      
    }
  }
});
Related