简体   繁体   中英

Why isn't my discord bot sending messages?

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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM