#disable setInterval created in another file of my bot
8 messages · Page 1 of 1 (latest)
node : v16.14.0
discord.js : v14.10.2
Here are my two codes in question:
The code setBotStatus.js (located in my ready folder of my handler):
const Discord = require('discord.js');
const BotStatus = require('../../models/BotStatus');
module.exports = async (bot) => {
const statusData = await BotStatus.findOne();
if (!statusData) return;
const { activity, motd, url, status, customInterval } = statusData;
let currentIndex = 0;
const updatePresence = async () => {
const currentMotd = motd[currentIndex];
if (activity === Discord.ActivityType.Streaming) {
await bot.user.setPresence({
activities: [{ name: currentMotd, type: activity, url: url }],
status: status,
});
} else {
await bot.user.setPresence({
activities: [{ name: currentMotd, type: activity }],
status: status,
});
}
currentIndex = (currentIndex + 1) % motd.length;
};
if (motd.length > 1) {
const rotationInterval = customInterval || 30;
setInterval(updatePresence, rotationInterval * 1000);
} else {
const currentMotd = motd[0];
if (activity === Discord.ActivityType.Streaming) {
await bot.user.setPresence({
activities: [{ name: currentMotd, type: activity, url: url }],
status: status,
});
} else {
await bot.user.setPresence({
activities: [{ name: currentMotd, type: activity }],
status: status,
});
}
}
}
And my code for the set-status.js command:
Store the return value of that setInterval call somewhere and pass it to clearInterval in your command file when starting the new interval
I already tried to do this, I may have done it wrong but I didn't succeed
Then maybe show how you tried to do it, because the code you showed doesn’t
I tried all day yesterday to find solutions, I did a lot of different things but basically I stored the interval in a variable :
let PresenceInterval
if (motd.length > 1) {
const rotationInterval = customInterval || 30;
PresenceInterval = setInterval(updatePresence, rotationInterval * 1000);
And then I made sure to export it to my other command file.