I am trying to code a discord spam bot as a challenge since I am a beginner coder and for use on my friend's private server. Everything works up until I use a either a while or for loop (I tried both). It worked when I only sent the message once but when I put message.channel.send(spamWord) inside a for loop that runs as many times as the user requests it just doesn't send. An issue I was running into was a deprecation warning but I fixed that. I expected that to be the problem but apparently there is another. Maybe my syntax is wrong but when it was wrong in different parts of the code I got an error, this time nothing appears in the console. If I put the message.channel.send(spamWord) after the for loop it executes it like the for loop was never there. Here is my code.
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Project is Running!");
})
app.get("/", (req, res) => {
res.send("Hello World!");
})
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.on("messageCreate", message => {
//* ----------------Spam Code--------------*/
if (message.content === "Spam") {
message.channel.send("I will spam the contents of the last message in this channel after 10 seconds. Then after I send a message type a number and that will be how many times that message is spammed.");
var delayInMilliseconds = 10000; //1 second
let i = 0
setTimeout(function() {
spamWord = message.channel.lastMessage
message.channel.send("OK, now how many times will I spam it? You have 10 seconds to type you number")
setTimeout(function() {
let spamNum = message.channel.lastMessage
for (i in parseInt(spamNum)) {
message.channel.send(spamWord);
}
}, delayInMilliseconds)
}, delayInMilliseconds);
}
})
client.login(process.env.token);
It looks like because you are looping in the for-in loop using int
for (i in parseInt(spamNum)) {
message.channel.send(spamWord);
}
Change to this.
for (var i = 0; i < parseInt(spamNum); i++) {
message.channel.send(spamWord);
}