Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Why am I unable to repeatedly send the same message with my discord bot in node.js?

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);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!