简体   繁体   English

为什么我的不和谐机器人不发送消息?

[英]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.我希望机器人能够每分钟发送一条消息(在本例中为 123)。

The code worked perfectly for me, though it sent a message every second. 代码对我来说非常合适,尽管它每秒都会发送一条消息。

The time given to setInterval is in milliseconds. setInterval的时间以毫秒为单位。

If one second is 1000 milliseconds then 60 seconds is 1000 x 60. 如果一秒是1000毫秒,那么60秒是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. 我还建议查看箭头函数和discord.js的调试事件。

can we make like a data folder and a config.json inside data folder after我们可以在数据文件夹中创建一个数据文件夹和一个 config.json 之后吗?

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

after

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

*inside bot.js *在bot.js里面

client.login(config.token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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