How to stop a message loop in discord.js?

by mallory_cormier , in category: Javascript , 8 days ago

How to stop a message loop in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , 7 days ago

@mallory_cormier 

To stop a message loop in Discord.js, you can use clearInterval() method. Here is an example of how to stop a message loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
const Discord = require('discord.js');
const client = new Discord.Client();

let interval;

client.on('message', message => {
    if (message.content === '!startloop') {
        interval = setInterval(() => {
            message.channel.send('This is a loop message.');
        }, 5000); // Sends a message every 5 seconds
    }

    if (message.content === '!stoploop') {
        clearInterval(interval);
    }
});

client.login('your-bot-token');


In this example, when you send !startloop command in the Discord server channel where the bot is running, it will start sending a message every 5 seconds. To stop the message loop, you just need to send !stoploop command. The clearInterval() method will stop the interval and the message loop.