2017-05-17 77 views
1

你好,我想发出的自动消息不和谐,但我不断收到以下错误发送消息:Discord.js以1个分钟的间隔

bot.sendMessage is not a function 

我不能确定,为什么我”米得到这个错误,下面是我的代码;

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); 
    } 
}); 
+0

我刚刚意识到 - 你在看'discord.io'文档吗? –

回答

0

您的代码返回错误,因为Discord.Client()没有一个叫sendMessage()如可以在docs可以看出方法。

如果您想发送消息,您应该按照以下方式进行操作;

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

我推荐用它可以发现here为discord.js文档熟悉自己。

0

Lennart是正确的,您不能使用bot.sendMessage,因为botClient类,并且没有sendMessage函数。这是冰山一角。你要找的是send(或旧版本,sendMessage)。

这些功能不能从Client类(这是bot是什么,他们是在一个TextChannel类使用直接使用。那么,你如何得到这个TextChannel?你把它从的Message。在示例代码,你是不是真正从bot.on('message'...听众得到一个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); 
    } 
}); 

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

我希望这有助于!