fra*_*le 4 javascript node.js discord
您好,我正在尝试发送自动消息以引起不和,但我不断收到以下错误:
bot.sendMessage is not a function
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会出现此错误,下面是我的代码;
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);
}
});
Run Code Online (Sandbox Code Playgroud)
Lennart是正确的,您不能使用,bot.sendMessage因为它bot是一个Client类,并且没有该sendMessage功能。那就是冰山一角。您正在寻找的是send(或旧版本sendMessage)。
这些函数不能直接在ClientClass中使用(也就是说bot,它们是在TextChannel类中使用的。那么,如何获取TextChannel呢?您是从Message类中获取的。在示例代码中,您实际上并没有得到Message对象从您的bot.on('message'...听众,但您应该!
的回调函数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);
}
});
Run Code Online (Sandbox Code Playgroud)
您还会注意到我.catch(console.error);在使用后添加了内容,message.channel.send("123")因为Discord希望它们的Promise-returning函数可以处理错误。
我希望这有帮助!