简体   繁体   中英

Auto delete messages in the specified channel by time? Discord.js

I did an automatic-channel cleaning at the time I needed (Monday 15:00)

But my program does not work as it should. The countdown starts when a message appears in the channel.

I need the channel to be cleared without new messages in channel.id

CODE

const schedule = require("node-schedule");

client.on("message", async (message) => {
  if (client.channels.cache.get("829042005236645900")) {

    const job = schedule.scheduleJob('30 * * * * *', function () {
      console.log('Delete');
      message.channel.bulkDelete(20)
    });
  }
});

It looks like you're creating the schedule inside the event handler, and that may be what's causing the issue. Here's an alternative to that:

const schedule = require("node-schedule");
const channel = client.channels.cache.get("829042005236645900");
const job = schedule.scheduleJob('30 * * * * *', function () {
  console.log('Delete');
  channel.bulkDelete(20);
});

However, it also looks like you want to make the event fire every 30 seconds? node-schedule has a system for that called Recurrence Rule Scheduling :

const schedule = require("node-schedule");
const channel = client.channels.cache.get("829042005236645900");
const job = new schedule.scheduleJob({ second: new schedule.Range(0, 59, 30) },
  function () {
    console.log('Delete');
    channel.bulkDelete(20);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM