Javascript Discord.js 每隔 1 分钟发送一条消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44030492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:17:56  来源:igfitidea点击:

Discord.js sending a message in 1 minute intervals

javascriptnode.jsdiscord

提问by fragile

Hello I'm trying to send out an automated message to discord but I keep getting the following error:

您好,我正在尝试向不和谐发送自动消息,但我不断收到以下错误:

bot.sendMessage is not a function

I'm unsure as to why I'm getting this error, below is my code;

我不确定为什么我会收到这个错误,下面是我的代码;

var Discord = require('discord.js');
var bot = new Discord.Client()

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

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

回答by Blundering Philosopher

Lennart is correct, you can't use bot.sendMessagebecause botis a Clientclass, and doesn't have the sendMessagefunction. That's the tip of the iceberg. What you're looking for is send(or the old version, sendMessage).

Lennart 是正确的,您不能使用,bot.sendMessage因为bot是一个Client类,并且没有该sendMessage功能。这就是冰山一角。您正在寻找的是send(或旧版本,sendMessage)。

These functions can't be used directly from the ClientClass (which is what botis, they are used on a TextChannelclass. So how do you get this TextChannel? You get it fromthe Messageclass. In your sample code, you aren't actually getting a Messageobject from your bot.on('message'...listener, but you should!

这些函数不能直接从ClientClass 中使用(也就是说bot,它们被用在一个TextChannel类上。那么你怎么得到这个TextChannel?你这个Message类中得到它。在你的示例代码中,你实际上并没有得到一个Message来自你的bot.on('message'...听众的反对,但你应该!

The callback function to bot.on('...should look something like this:

to 的回调函数bot.on('...应该是这样的:

// add message as a parameter to your callback function
bot.on('message', function(message) {
    // Now, you can use the message variable inside
    if (message.content === "$loop") { 
        var interval = setInterval (function () {
            // use the message's channel (TextChannel) to send a new message
            message.channel.send("123")
            .catch(console.error); // add error handling here
        }, 1 * 1000); 
    }
});

You'll also notice I added .catch(console.error);after using message.channel.send("123")because Discord expects their Promise-returning functions to handle errors.

您还会注意到我.catch(console.error);在使用后添加了,message.channel.send("123")因为 Discord 期望它们的Promise返回函数来处理错误。

I hope this helps!

我希望这有帮助!

回答by Lennart

Your code is returning the error, because Discord.Client()doesn't have a method called sendMessage()as can be seen in the docs.

您的代码正在返回错误,因为Discord.Client()没有sendMessage()可以在docs 中看到的调用方法。

If you would like to send a message, you should do it in the following way;

如果您想发送消息,您应该通过以下方式进行;

var Discord = require('discord.js');
var bot = new Discord.Client()

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

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

I recommend familiarising yourself with the documentation for discord.js which can be found here.

我建议您熟悉 discord.js 的文档,可以在此处找到。