1
votes

I coded a part of a bot for discord that supposed to send a message every minute, but after launching the bot, and waiting one minute, the bot still has not sent a message.

I have not tried anything, because I do not know how to fix this problem.

const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', function() {
    console.log(client.user.username);
});

client.on('message', function(message) {
    if (message.content === "$loop") { 
      var interval = setInterval (function () {
        message.channel.send("123")
      }, 1 * 1000); 
    }
});

// token taken out of question for privacy

I expect the bot to be able to send a message (123 in this case) every one minute.

2
1 * 1000 means it will send a message every one secondBrandon Dyer
According to the docs, there is an error event and a debug event. I would start there: discord.js.org/#/docs/main/stable/class/ClientMax Baldwin
@BrandonDyer so what would be every one minute?amateur_coder
@amateur_coder see the answer I postedBrandon Dyer

2 Answers

3
votes

The code worked perfectly for me, though it sent a message every second.

The time given to setInterval is in milliseconds.

If one second is 1000 milliseconds then 60 seconds is 1000 x 60.

This worked for me:

const Discord = require('discord.js')
const client = new Discord.Client()

client.on('ready', function() {
    console.log(client.user.username);
});

client.on('message', function(message) {
    if (message.content === "$loop") {
        var interval = setInterval(function () {
            message.channel.send("123");
        }, 60 * 1000);
    }
});

client.login(process.env.TOKEN);

I would also recommend looking into arrow functions, and discord.js's debug events.

0
votes

can we make like a data folder and a config.json inside data folder after

const config = ('./Data/Config.json');

after

*inside config.json
 
{
    "token": "ur token here"
}

*inside bot.js

client.login(config.token);