How can I stop the interval? - javascript

I'm making a setInterval method in my discord bot, but I have an issue in stopping the Interval.
Look at my code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
console.log('Im online');
});
client.on('message', async msg => {
var interval;
if(msg.content == '!spam')
{
interval = setInterval(() => {
msg.channel.send('test');
}, 2000);
}
if(msg.content.startsWith('!stop'))
{
clearInterval(interval);
}
});
That should definitely work, right?
If you know the answer please help me!!

The problem is:
client.on('message', async msg => {
var interval;
This creates a new variable binding interval every single time the message handler runs. Inside any single message handler, either
interval = setInterval(() => {
msg.channel.send('test');
}, 2000);
will run, or
if (msg.content.startsWith('!stop')) {
clearInterval(interval);
}
will run (or neither will run). But the interval is not persistent outside of the function, so further messages will be attempting to clear a different interval binding.
Make interval persistent instead, and also check that you don't start an interval while one is already going on:
let interval;
client.on('message', (msg) => {
if (msg.content == '!spam' && !interval) {
interval = setInterval(() => {
msg.channel.send('test');
}, 2000);
}
if (msg.content.startsWith('!stop')) {
clearInterval(interval);
interval = null;
}
});

Related

Discord.js Ping Pong command with cooldown

I'm very new to coding so it's probably horrible. Here's the code
let userCooldown = {};
client.on("message", message => {
if (message.content.includes('ping'))
if (userCooldown[message.author.id]) {
userCooldown[message.author.id] = false;
message.reply('Pong');
setTimeout(() => {
userCooldown[message.author.id] = true;
}, 5000) // 5 sec
}
})
the plan would be for the bot not to respond to the message for 5 seconds until it's written again
Not sure why you set the cooldown to false if it's true when someone sends a message and why you set it to true after five seconds.
If your userCooldown includes the users who currently can't execute the function, you need to check if they are already on that list. If they are, don't execute the function. If they are not, execute the function, add them to the list and use setTimeout to remove them after five seconds. Try to run the snippet below:
let userCooldown = {};
function onMessage(message) {
console.log(`onMessage fired with message: "${message.content}"`);
if (message.content.includes('ping')) {
// if user can't execute the fucntion, just exit
if (userCooldown[message.author.id]) return;
// if they can, add them to userCooldown
userCooldown[message.author.id] = true;
console.log('Pong!');
// and remove them after 5 seconds
setTimeout(() => {
userCooldown[message.author.id] = false;
}, 5000);
}
}
// run it every second to test it :)
let message = { content: '!ping', author: { id: 'authorID' } };
setInterval(() => onMessage(message), 2000)

clearInterval in web worker not stopping timer

my question has been asked once here:
clearInterval in webworker is not working
The solution seems clear but for some reason it is not working for me yet. I have a web worker that is sending an interval back to the main thread. I want to be able to stop the interval with clearInterval, but it is not working.
I have it set up exactly the same as it suggests in the previous question, but still no luck. I have added some console.logs to verify I'm in the correct block. "Stop" logs to the console when it supposed to, but the timer doesn't stop posting to the main thread.
Can anyone spot what's going on here?
Thanks
worker.js
let mytimer;
self.onmessage = function(evt) {
if (evt.data == "start") {
console.log("start")
var i = 0;
mytimer = setInterval(function() {
i++;
postMessage(i);
}, 1000);
} else if (evt.data == "stop") {
console.log("stop")
clearInterval(mytimer);
}
};
Then I'm calling this from my React hook when timer.time is above or below a certain value (2000 in this case)
main.js
const worker = new myWorker()
useEffect(() => {
worker.addEventListener('message', function(e) {
//from interval in the worker
console.log('Message from Worker: ' + e.data);
})
if(timer.time > 2000){
worker.postMessage("start")
}else{
worker.postMessage("stop")
}
},[timer.time])
You should also clear the interval when you start a new interval. If you don't do it, your previous interval would keep running, and you'll lose the ability to clear it:
let mytimer;
self.onmessage = function(evt) {
console.log(evt.data)
if(evt.data === 'start' || evt.data === 'stop') {
clearInterval(mytimer);
}
if (evt.data == "start") {
var i = 0;
mytimer = setInterval(function() {
i++;
postMessage(i);
}, 1000);
}
};
You should create a single instance of the worker, and store it as ref:
const worker = useRef()
useEffect(() => {
worker.current = new myWorker()
return () => {
worker.current.terminate();
}
}, [])
Not related, but in addition, the useEffect adds a new event listener whenever timer.time changes, without clearing the previous one. I would split this into 2 useEffect blocks, one for sending (which can be combind with the creation of the worker), and the other for receiving.
useEffect(() => {
const eventHander = e => {
//from interval in the worker
console.log('Message from Worker: ' + e.data);
}
worker.current.addEventListener('message', eventHander)
return () => {
worker.current.removeEventListener('message', eventHander)
}
}, [])
useEffect(() => {
worker.current.postMessage(timer.time > 2000 ? 'start' : 'stop')
}, [timer.time])
I'm not sure about web workers but I am very familiar with using intervals in useEffect. Since useEffect is called everytime your dependencies change (timer.time), you need to store the interval in a useRef unless you are going to be clearing it before your dependency next changes

How to stop a setInterval function on discord.js? [duplicate]

This question already has answers here:
Stop setInterval call in JavaScript
(7 answers)
Closed 2 years ago.
I've been wanting to add a feature to my script, which can enable me to stop the setInterval function through a simple command, but the real problem is that I do not know how to do it.
client.on('message', async (message) => {
if (message.author.username === 'username')
if (message.content === '*shiba inu pupper') {
const attachment = new Discord.MessageAttachment('shiba doggo');
setInterval(() => {
message.channel.send(attachment);
}, 20000);
}
});
Ex: Your interval function:
let intervalFunc = setInterval(() => {
// code .....
}, 20000);
After that When you want to stop the interval time, use the below function:
clearInterval(intervalFunc);
Above function will clear your interval time
add my_interval global variable in your js file :
var my_interval;
then:
client.on('message', async (message) => {
if (message.author.username === 'username')
if (message.content === '*shiba inu pupper') {
const attachment = new Discord.MessageAttachment('shiba doggo');
my_interval = setInterval(() => {
message.channel.send(attachment);
}, 20000);
}
});
and whenever you went to stop that interval just call:
clearInterval(my_interval);

Get Timer ID in Node.js

I have a simple server-side timer in NodeJS application to send some data with socket to client. How do I manage to send Timer ID to client so it can be stopped from a client side?
I tried to pass returned class in socket payload, but got an error "Max call stack size exceeded.
socket.on('start', () => {
const timerID = setInterval(()=>{
socket.emit('data',someData)
},3000)
socket.emit('ID',timerId)
}
Also tried to map over class symbols, but nothing works.
const array = Object.getOwnPropertySymbols(timerID);
array.map((symbol) => {
clearInterval(timerID[symbol]);
});
Found in NodeJS Documentation this:
timeout[Symbol.toPrimitive]"()
Returns: number that can be used to reference this timeout
But it also didn't work.
The client cannot stop your timerID and, in nodejs a timerID is not a simple integer either or something that can be stringified so it can't effectively be sent to the client.
You could create your own integer identifier that is mapped to the actual timerID, send that to the client and then the client could send the server back a message that it wishes to cancel timer X. You would look up timerX in your Map, find the real timerID and stop the timer.
const timerMap = new Map();
const timerCntr = 0;
socket.on('start', () => {
const timerID = setInterval(()=>{
socket.emit('data', someData)
}, 3000);
// create an integer reference for the timer
// save a mapping between the integer reference and the actual timerID
// in a server-side Map object
const externalTimerID = timerCntr++;
timerMap.set(externalTimerID, timerID);
socket.emit('ID', externalTimerID);
}
// when client wants to stop the timer
socket.on('stop', externalTimerID => {
let timerID = timerMap.get(externalTimerID);
clearInterval(timerID);
// remove externalTimerID from the Map so we don't leak memory
timerMap.delete(externalTimerID);
});
Starting with node v14.9.0, you can get an integer version of the timerID that will work as a substitute for the actual timerID object.
socket.on('start', () => {
const timerID = setInterval(()=>{
socket.emit('data', someData)
}, 3000);
socket.emit('ID', timerID[Symbol.toPrimitive]());
}
// when client wants to stop the timer
socket.on('stop', externalTimerID => {
clearInterval(externalTimerID );
});
Here's a sample nodejs app where I verified that timerID[Symbol.toPrimitive]() is a number and will work as a substitute for the timerID object.
const timerID = setInterval(() => {
console.log(`hi`);
}, 500);
const integerID = timerID[Symbol.toPrimitive]();
console.log(typeof integerID, integerID); // shows it is an integer
setTimeout(() => {
console.log("clearing Interval with integerID");
clearInterval(integerID);
}, 2000);

Discord bot's no spamming feature isn't working

So I have been attempting to make an anti-spam function on a discord bot, but have look over my code and it all seems to work, but it doesn't. I am new to javascript, so I'm not sure whats wrong... Everything else works on my bot, just not this. I am only pasting the part of my code involving the antispam:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
function antispam() {
var spam = 0;
}
client.on('ready', () => {
setInterval(antispam(), 5000);
});
client.on('message', msg => {
if (spam > 10) {
client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!');
}
});
function antispam() {
var spam = 0;
}
client.on('message', msg => {
var spam = spam + 1;
});
client.login('token');
Help would be appreciated!
Hi and welcome to Stack Overfow!
Several issue here:
You're declaring antispam twice, declare functions always once
Your interval doesn't do anything. It calls antispam what creates spam inside the scope of antispam what gets deleted immediately after antispam finishes executing
You're declaring client.on('message'... twice. Again, just do it once
You're declaring `client.on('ready'...) twice.
If you increase spam inside client.on('message'...) that spam variable will be useless just as in antispam. You don't access it anywhere (you also couldn't if you would try to)
So here's a solution that should work:
// To count the "spam level"
const spam = 0;
// Increase spam everytime someone writes something
client.on('message', msg => {
// Check if spam level is already too high
if (spam > 10) {
client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!');
} else {
spam++;
}
});
// Decrease the spam level, otherwise nobody can't send message until the bot gets restarted
setInterval(() => {
if (spam > 0) {
spam--;
}
}, 10000); // Let's decrease it every 10 seconds
Your whole code:
const Discord = require('discord.js');
const client = new Discord.Client();
// To count the "spam level"
const spam = 0;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// Increase spam everytime someone writes something
client.on('message', msg => {
// Check if spam level is already too high
if (spam > 10) {
client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!');
} else {
spam++;
}
});
// Decrease the spam level, otherwise nobody can't send message until the bot gets restarted
setInterval(() => {
if (spam > 0) {
spam--;
}
}, 10000); // Let's decrease it every 10 seconds
client.login('token');
// To count the "spam level"
const spam = 0;
// Increase spam everytime someone writes something
client.on('message', msg => {
if(msg.author.bot) return
// Check if spam level is already too high
if (spam > 10) {
client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!');
} else {
spam++;
}
});
// Decrease the spam level, otherwise nobody can't send message until the bot gets restarted
setInterval(() => {
if (spam > 0) {
spam--;
}
}, 10000); // Let's decrease it every 10 seconds
The code provided by CodeF0x is good , but make sure to add:
if(msg.author.bot) return
If the message being sent was by a bot, it will ignore them. IF YOU DO NOT add this to your code, get prepared for your bot to spam. It will respond to itself due to that if statement missing (especially since every time one sends a message, the spam variable will increase). So yeah, happy coding!

Categories

Resources